Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Raycasting Help (Part Two)

I was just wondering if there was a way to find the name of a physics body a 2D raycast is hit by??
For my game. It’s supposed to be one of the mechanics that I wanna implement.

Thanks in advance :)

Comments

  • edited September 1 Posts: 2,364
    @Creator27 - just out of curiosity - is this project of yours a target in a course or your own personal project?
  • Posts: 172

    i had this problem too, but there is an info prop on body that you can use to put a unique name

  • dave1707dave1707 Mod
    Posts: 9,441

    @skar That works for physics.raycast, but so far I can’t get it to work with Craft physics.raycast. Do you have an example for Craft.

  • Posts: 172

    sorry i don’t mess with craft, i’m all 2D

  • dave1707dave1707 Mod
    Posts: 9,441

    @Creator27 Here’s an example of 2 circles with raycast using information. I’m using “name” and “yPos” to show the name at a specific y position.

    viewer.mode=STANDARD
    
    
    function setup()
        p1=physics.body(CIRCLE,40)
        p1.type=STATIC
        p1.x=WIDTH/2
        p1.y=300
        p1.name="circle at 300" 
        p1.yPos=p1.y 
    
        p2=physics.body(CIRCLE,60)
        p2.type=STATIC
        p2.x=WIDTH/2-60
        p2.y=600
        p2.name="circle at 600"  
        p2.yPos=p2.y 
    
        x=0
        v=1
    end
    
    function draw()
        background(40, 40, 50)
        noStroke()
        fill(255)
        tab=physics.raycastAll(vec2(WIDTH/2,0),vec2(x,HEIGHT))
        if tab~=nil then
            for a,b in pairs(tab) do
                text(b.body.name,60,b.body.yPos)
            end
        end
        ellipse(p1.x,p1.y,80)
        ellipse(p2.x,p2.y,120)
    
        x=x+v
        if x>WIDTH or x<1 then
            v=-v
        end
        stroke(255)
        strokeWidth(2)
        line(WIDTH/2,0,x,HEIGHT)
    end
    
  • Posts: 104

    @dave1707, I forgot to mention that I’m trying to figure out if you can still find out the object’s name hit by the raycast, even if the physics body of the object is not currently active.

  • dave1707dave1707 Mod
    Posts: 9,441

    @Creator27 Not sure what you mean by not currently active.

  • Posts: 104

    @dave1707, I got mixed up with what I was thinking, sorry about that :) However, when I tried your code it did not work. Do you think you might know why??

    Thanks in advance :)

  • dave1707dave1707 Mod
    Posts: 9,441

    @Creator27 It works for me. What’s not working. There should be a line that sweeps back and forth and when it intersects a circle, it will display the circles height.

Sign In or Register to comment.