# Raycasting Is Broken!

**URL:** <https://forums.solar2d.com/t/raycasting-is-broken/312423>\
**Category:** Physics\
**Created:** [April 12, 2013, 12:05pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423 "2013-04-12T12:05:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [April 12, 2013, 12:05pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/1 "2013-04-12T12:05:45Z")

</div>

If you run the code below as per the raycasting example it all works fine:

```
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.newImage( "crate.png" ) crate1.x = 120 crate1.y = 0 physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } ) local crate2 = display.newImage( "crate.png" ) crate2.x = 200 crate2.y = 120 physics.addBody( crate2, { density=3.0, friction=0.5, bounce=0.3 } ) local ray = nil local from\_x = 0 local from\_y = 0 local to\_x = 320 local to\_y = 500 local unbroken\_line = display.newLine( from\_x, from\_y, to\_x, to\_y ) unbroken\_line.width = 2 unbroken\_line:setColor( 255, 0, 0 ) local gameloop = function(event) &nbsp;&nbsp; &nbsp;if ray then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; display.remove( ray ) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; local hits = physics.rayCast( from\_x, from\_y, to\_x, to\_y, 3 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if hits then &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Hit count: ", #hits ) &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for i,v in ipairs(hits) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local object\_name &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if v.object == ground then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;object\_name = "ground" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif v.object == crate1 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;object\_name = "crate1" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif v.object == crate2 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;object\_name = "crate2" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;object\_name = "unknown" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Hit: ", i, object\_name, v.x, v.y) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ray = display.newLine( from\_x, from\_y, hits[1].x, hits[1].y ) &nbsp;&nbsp; &nbsp;else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ray = display.newLine( from\_x, from\_y, to\_x, to\_y ) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;ray.width = 2 end Runtime:addEventListener("enterFrame", gameloop)

```

However if you change either from\_x or from\_y to anything other than zero it doesn’t work.

If you change from\_x from 0 to 1 you get some rather odd results.

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [April 12, 2013, 1:42pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/2 "2013-04-12T13:42:36Z")

</div>

Also rayCast does not adhere to its set length.

If you set the to\_x to 160 and to\_y to 240 as per above example the raycast will continue on and hit the ground, which is further away then the rayCast end point.&nbsp;

Again this is a major bug!

I have submitted a bug for each of these issues.&nbsp; I’m very surprised that rayCasting was released without picking up on both of these very obvious bugs.&nbsp;

---

<div class="post-metadata">

**Author:** ![bjsorrentino](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bjsorrentino](https://forums.solar2d.com/u/bjsorrentino)\
**Post date:** [April 12, 2013, 5:52pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/3 "2013-04-12T17:52:16Z")

</div>

Hi @Icy Spark,

I pushed both of these issues to the front since (obviously) people want to start using raycasting. They should be fixed in a daily build in the next few days or so.&nbsp;Thanks for reporting them and the details behind each.

Brent

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [April 13, 2013, 3:05pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/4 "2013-04-13T15:05:26Z")

</div>

> [@bjsorrentino](#):
>
> Hi @Icy Spark,
> 
> I pushed both of these issues to the front since (obviously) people want to start using raycasting. They should be fixed in a daily build in the next few days or so.&nbsp;Thanks for reporting them and the details behind each.
> 
> Brent

Many thanks Brent.&nbsp; All perfect now!&nbsp; Happy Face 🙂

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [April 12, 2013, 1:42pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/5 "2013-04-12T13:42:36Z")

</div>

Also rayCast does not adhere to its set length.

If you set the to\_x to 160 and to\_y to 240 as per above example the raycast will continue on and hit the ground, which is further away then the rayCast end point.&nbsp;

Again this is a major bug!

I have submitted a bug for each of these issues.&nbsp; I’m very surprised that rayCasting was released without picking up on both of these very obvious bugs.&nbsp;

---

<div class="post-metadata">

**Author:** ![bjsorrentino](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bjsorrentino](https://forums.solar2d.com/u/bjsorrentino)\
**Post date:** [April 12, 2013, 5:52pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/6 "2013-04-12T17:52:16Z")

</div>

Hi @Icy Spark,

I pushed both of these issues to the front since (obviously) people want to start using raycasting. They should be fixed in a daily build in the next few days or so.&nbsp;Thanks for reporting them and the details behind each.

Brent

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [April 13, 2013, 3:05pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/7 "2013-04-13T15:05:26Z")

</div>

> [@bjsorrentino](#):
>
> Hi @Icy Spark,
> 
> I pushed both of these issues to the front since (obviously) people want to start using raycasting. They should be fixed in a daily build in the next few days or so.&nbsp;Thanks for reporting them and the details behind each.
> 
> Brent

Many thanks Brent.&nbsp; All perfect now!&nbsp; Happy Face 🙂

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 12:56pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/8 "2013-05-03T12:56:21Z")

</div>

Has this been resolved yet?

I’m also wondering if raycast has any other bugs in it, since started using raycasting, i’m getting frequent simulator crashes (i’m using build .1093).

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [May 3, 2013, 1:01pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/9 "2013-05-03T13:01:01Z")

</div>

Yes, it has been resolved now.

The only other bug that I can think of is that rayCast does not always return the nearest item first,&nbsp; though I have programmed round that.

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [May 3, 2013, 1:21pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/10 "2013-05-03T13:21:30Z")

</div>

Here is an example for a game I’m working on at the minute.

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 1:24pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/11 "2013-05-03T13:24:55Z")

</div>

Yes, I’m using your little bit of code and it works well, thanks for that 🙂

Just seems like i’m coming across some sort of bug that is repeatedly crashing the simulator and very hard to figure out if its raycast, build .1093 or perhaps even mountain lion. However, every time, raycast is mentioned near the top of the crash thread (altho i’m no expert as to whether it will always shows near the top regardless of the crash reason):

_Thread 0 Crashed:: Dispatch queue: com.apple.main-thread_

_0 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x00000001000ab058 luaH\_getnum + 11_

_1 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x00000001000ab5d3 luaH\_setnum + 31_

_2 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x000000010009868b lua\_rawseti + 43_

_3 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x0000000100067102 0x100000000 + 422146_

_4 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x00000001002694e9 b2WorldRayCastWrapper::RayCastCallback(b2RayCastInput const&, int) + 159_

_5 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x00000001002693f2 void b2DynamicTree::RayCast\<b2WorldRayCastWrapper\>(b2WorldRayCastWrapper\*, b2RayCastInput const&) const + 968_

_6 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x0000000100268633 b2World::RayCast(b2RayCastCallback\*, b2Vec2 const&, b2Vec2 const&) const + 57_

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 1:30pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/12 "2013-05-03T13:30:29Z")

</div>

> [@Icy\\\_Spark](#):
>
> Here is an example for a game I’m working on at the minute.
> 
> ![attachicon.gif](http://forums.coronalabs.com/public/style_images/coronalabs/attachicon.gif)[forum pic.png](http://forums.coronalabs.com/index.php?app=core&module=attach&section=attach&attach_rel_module=post&attach_id=149)

Nice use of the reflect ray. Are you using it for pathfinding or just as part of our game mechanics?

I’m still figuring out the best way to use raycast for pathfinding.

Currently I’ve settled on dynamically building a grid map over my box2d world, ie; setting nodes that are filled with objects ‘blocked’ for calculating the basic a\* path, then running my characters along the path nodes while using raycast to refine their actual paths. You heard of any better way?

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 12:56pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/13 "2013-05-03T12:56:21Z")

</div>

Has this been resolved yet?

I’m also wondering if raycast has any other bugs in it, since started using raycasting, i’m getting frequent simulator crashes (i’m using build .1093).

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [May 3, 2013, 1:01pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/14 "2013-05-03T13:01:01Z")

</div>

Yes, it has been resolved now.

The only other bug that I can think of is that rayCast does not always return the nearest item first,&nbsp; though I have programmed round that.

---

<div class="post-metadata">

**Author:** ![Icy\_Spark](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/i/dc4da7/32.png) [@Icy\_Spark](https://forums.solar2d.com/u/Icy_Spark)\
**Post date:** [May 3, 2013, 1:21pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/15 "2013-05-03T13:21:30Z")

</div>

Here is an example for a game I’m working on at the minute.

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 1:24pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/16 "2013-05-03T13:24:55Z")

</div>

Yes, I’m using your little bit of code and it works well, thanks for that 🙂

Just seems like i’m coming across some sort of bug that is repeatedly crashing the simulator and very hard to figure out if its raycast, build .1093 or perhaps even mountain lion. However, every time, raycast is mentioned near the top of the crash thread (altho i’m no expert as to whether it will always shows near the top regardless of the crash reason):

_Thread 0 Crashed:: Dispatch queue: com.apple.main-thread_

_0 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x00000001000ab058 luaH\_getnum + 11_

_1 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x00000001000ab5d3 luaH\_setnum + 31_

_2 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x000000010009868b lua\_rawseti + 43_

_3 &nbsp; com.coronalabs.Corona\_Simulator&nbsp;&nbsp;&nbsp;&nbsp;0x0000000100067102 0x100000000 + 422146_

_4 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x00000001002694e9 b2WorldRayCastWrapper::RayCastCallback(b2RayCastInput const&, int) + 159_

_5 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x00000001002693f2 void b2DynamicTree::RayCast\<b2WorldRayCastWrapper\>(b2WorldRayCastWrapper\*, b2RayCastInput const&) const + 968_

_6 &nbsp; org.Box2D.Box2D &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;0x0000000100268633 b2World::RayCast(b2RayCastCallback\*, b2Vec2 const&, b2Vec2 const&) const + 57_

---

<div class="post-metadata">

**Author:** ![jacques1](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jacques1](https://forums.solar2d.com/u/jacques1)\
**Post date:** [May 3, 2013, 1:30pm UTC](https://forums.solar2d.com/t/raycasting-is-broken/312423/17 "2013-05-03T13:30:29Z")

</div>

> [@Icy\\\_Spark](#):
>
> Here is an example for a game I’m working on at the minute.
> 
> ![attachicon.gif](http://forums.coronalabs.com/public/style_images/coronalabs/attachicon.gif)[forum pic.png](http://forums.coronalabs.com/index.php?app=core&module=attach&section=attach&attach_rel_module=post&attach_id=149)

Nice use of the reflect ray. Are you using it for pathfinding or just as part of our game mechanics?

I’m still figuring out the best way to use raycast for pathfinding.

Currently I’ve settled on dynamically building a grid map over my box2d world, ie; setting nodes that are filled with objects ‘blocked’ for calculating the basic a\* path, then running my characters along the path nodes while using raycast to refine their actual paths. You heard of any better way?
