Raycasting. Thank You Corona!

Ive just tried the raycasting physics example and it is absolutely perfect for a game concept I’ve been sitting on for nearly a year.  I’m sure many developers will be able to use this in future games.

Now to get to work on that new game!!!

Thank you Corona.

Hi @Icy Spark,

Do you mind if I ask how you’ve utilized this in the game? Somebody was asking me yesterday “what are some use-cases for raycasting?”, and while I can think of a few immediate examples, I’m curious what you’re doing with it. :slight_smile:

Brent

Hi Brent,

Only just started working on it earlier, but it involves getting a laser to a goal.  This is perfect for being able to draw the laser to the exact point it hits an object.  

Can also use the exact contact point to bounce the laser off a mirror etc.  Even better it doesn’t require a collision listener, which is great.

One question though. 

Say I want to refract the laser light through a prism to bend the laser light.  Using raycasting I can get the initial contact point that it hits the prism, so I can also change the angle as it goes through the prism. 

But, is it possible to get the hit ended point, so that I can bend the light again as it exits the prism?

I still don’t get it.  Any pertinent Google searches on raycasting bring up Wolfenstein 3D.  I guess I’ll have to wait until I get home to download the latest daily build to try it out, but if someone can be so kind as to explain its usefulness in game development, especially in regards to physics, I’d appreciate it.

Hi @BeyondtheTech,

It think the common confusion is “raycasting” as a term in computer graphics versus the physics raycasting that this topic is about. Here’s more info on physics-based raycasting:

http://www.iforce2d.net/b2dtut/raycasting

Brent

Using the example of a laser for Raycasting I suppose can confuse you.

What it effectively does is it draws an imaginary line between two points, similar to the drawline function.

However, it counts the amount times it “hits” an object and passes the co-ordinates of the intersection point.

You can specify how many hits you would like to count.

A good example would be for line of sight in a top down game.  You set the hit count maximum to one. 

Then raycast from the baddies point of view, ie in which way they are looking.

If the raycast hits you the player then they can “see” you and act accordingly, for example start shooting at you.

Hope this helps

Holy toledo, I think that’s fantastic!

As some of you know, I’m building a remake of my space game Archangel, and I had to come up with some (what I thought was) clever thinking on how to detect if a passing asteroid or ship was on a collision course.  I set up a separate large “proximity” sensor around the ship which had to follow the ship around.  When an asteroid/ship it crosses (collides) with the proximity sensor, I had to determine the trajectories of both the player and the asteroid/ship see if they were crossing (intersecting) each other, allowing for some angular leeway.  It was quite a challenge, and it might have taken more CPU cycles than I would have wanted to allocate for it, so perhaps replacing it with this new feature will do it better justice.  Thanks!

Hi Brent,

In that example box2D exposes the normal.  Is this accessable in Corona as it would make my life so much easier.

Hi @Icy Spark,

I already asked the engineering team and I should have an answer fairly soon. Thanks for bringing this up!

Brent

Hi @Icy Spark,

The “normal” isn’t currently reported, but engineering is fixing that now and it should be exposed for your use in a daily build upcoming. Just keep an eye out. :slight_smile:

Brent

Like IcySpark says, this would be excellent for an Metal Gear Solid inspired stealth game. Raycasting to create Wolfenstein/Doom uses the technique to calculate how high a wall is based on the distance to the player.

It’s all really great that this functionality is here.

so this would mean, a NPC “raycasts” in a zelda like game my hero, and if there is no intersection point with another object in-between (like tree, unless the tree allows you to shoot through), the NPC will fire at me?

If I understood that right… this is awesome!

yep that’s pretty much it.  great news, though i found a major bug that renders raycasting useless in its current state.

great news! and I am sure they will fix it soon…

Hi @IcySpark,

What’s the bug? I can request that it gets investigated so people can start implementing ray casting a.s.a.p.

Brent

Hi Brent,

I have posted now 2 bugs in this thread http://forums.coronalabs.com/topic/33714-raycasting-is-broken/

I have also submitted the two bug reports 22591 and 22593.

First bug:  If you set from_x and/or from_y to anything other than 0 the raycasting fails to work properly or at all.

Second bug:  rayCasting doesnt adhere to its length set by from_x, from_y, to_x and to_y. 

can be obtained dynamic shadow using the ray casting?

http://www.youtube.com/watch?v=C-ScURIRTGA

Yes no problem.  Here is a quick example that I just created in Corona.  Had to make the source 0,0 due to the current bug in raycasting though.

raytracing-corona.png

Please make the code

Its not perfect but here it is.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() local ground = display.newImage( "ground.png" ) ground.x = 160 ground.y = 445 physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) local crate1 = display.newImageRect( "crate.png",20,20 ) crate1.x = 30 crate1.y = 110 physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } ) local crate2 = display.newImageRect( "crate.png",20,20 ) crate2.x = 90 crate2.y = 170 physics.addBody( crate2,"static", { density=3.0, friction=0.5, bounce=0.3 } ) local crate3 = display.newImageRect( "crate.png",20,20 ) crate3.rotation = 45 crate3.x = 25 crate3.y = 280 physics.addBody( crate3,"static",{ density=3.0, friction=0.5, bounce=0.3 } ) local crate4 = display.newImageRect( "crate.png",30,30 ) crate4.x = 280 crate4.y = 395 physics.addBody( crate4,"static",{ density=3.0, friction=0.5, bounce=0.3 } ) local from\_x = 0 local from\_y = 0 local to\_x =400 local to\_y = 480 local hits = {} local ray = {} local gameloop = function(event)     for i = 1, 200 do     if ray[i] then         -- Update the ray by removing the old one and creating a new one.         display.remove( ray[i] )     end     hits[i] = physics.rayCast( from\_x, from\_y, to\_x-(i\*2), to\_y, 1 )     if hits[i] then         -- Draw a line to the first hit.         ray[i] = display.newLine( from\_x, from\_y, hits[i][1].x, hits[i][1].y )     else         -- no hit.         ray[i] = display.newLine( from\_x, from\_y, to\_x, to\_y )     end     ray[i].width = 2     end end Runtime:addEventListener("enterFrame", gameloop)