# Raycasting help (FIXED)

According to the description, raycasts, upon detecting a body, will return a table with certain key value pairs. the question I have is: Which table will the raycast actually return?
if you do:

``````local i=physics.raycast(vec2(start coords),vec2(end coords))
for a,o in pairs(i) do

end
``````

Codea will see i as a nil value, not a table. How can I access the table so that I can manage collisions through my created raycasts?

if raycast finds anything then the table “i” will contain information. if it doesn’t then table “i” will be nil. You have to check for nil. if it’s not nil then read the table.

@Paintcannon Here’s an example showing raycast and raycastAll. raycast returns 1 table. raycastAll returns a table of tables. One for each item it finds. I didn’t show an example of them not finding anything. In those cases, table t will be nil.

``````
function setup()
b=physics.body(CIRCLE,10)
b.x=200
b.y=200
b.type=STATIC

b1=physics.body(CIRCLE,10)
b1.x=200
b1.y=400
b1.type=STATIC

print("raycast")
t=physics.raycast(vec2(200,100),vec2(200,500))
for a,b in pairs(t) do
print(a,b)
end

t=physics.raycastAll(vec2(200,100),vec2(200,500))
print("\
raycastAll")
for a,b in pairs(t) do
for c,d in pairs(b) do
print(c,d)
end
end
end

function draw()
background(40, 40, 50)
end

``````

@Paintcannon - my experience with raycasting is that it slows your program down too much, and that it’s better to use some other method of detecting objects. But I guess it depends what you’re using it for.

@Ignatz well I’m trying to make a doodle jump copy only with scrolling and for the platforms when I use regular physics bodies I have to “teleport” each of them off screen if the character is below them (so that they can’t bump him from above). And with this many physics bodies, the game freaks out and crashes. I can’t use if statements to test if he’s near any of the platforms either because the character will usually go through them too quickly for the statements to detect if he’s on them. Ray casts are perfect because they automatically detect the character yet they don’t bump into him or affect his linear Velocity directly, so I can have an if statement test if my characters vertical velocity is less than zero and if he is above the platform (when he bumps into a ray cast), and he will be bumped up if so.

@Paintcannon I don’t know whether I understood your explanation, but if you set a body’s sensor value to true, it will generate collision detection information, but without the bodies physically affecting one-another. It could be an alternative worth investigating

@Ignatz that used to be the case, now its possible to have 60fps with over 20 raycasts and over 50 objects. I think some work was done on it, can’t be sure.