Ray Casting
by Lucas Shinkovich · in Torque X 2D · 07/12/2009 (12:43 pm) · 5 replies
Hey guys,
I was wondering if anyone has any information on ray casting in TX2D?
The only thread I was able to fine about this was the following: http://www.garagegames.com/community/forums/viewthread/68520
This code works great, but unfortunetly it doesn't take into account any collision image you have setup on your obstacles. So the ray cast that is performed must go exactly over the obstacle's position to work, but my objects are obviously larger then just a point so the ray cast won't pick it up.
I'm attempting to use the ray cast to determine a clear line of sight to a target object so the AI can determine if it needs to shoot a projectile.
If anyone has any information on how to do this (or if there is some new functionality to perform this in v3.0) I'd appreciate any help :)
I was wondering if anyone has any information on ray casting in TX2D?
The only thread I was able to fine about this was the following: http://www.garagegames.com/community/forums/viewthread/68520
This code works great, but unfortunetly it doesn't take into account any collision image you have setup on your obstacles. So the ray cast that is performed must go exactly over the obstacle's position to work, but my objects are obviously larger then just a point so the ray cast won't pick it up.
I'm attempting to use the ray cast to determine a clear line of sight to a target object so the AI can determine if it needs to shoot a projectile.
If anyone has any information on how to do this (or if there is some new functionality to perform this in v3.0) I'd appreciate any help :)
About the author
#2
Sorry, let me be a little clearer on my issue.
I'm stating your interpretation A above. What I'm seeing from running John's code is the following:
If I call the pick line and the line intersects an object's origin (i.e. a T2DSceneObject's position) then the pick line picks it up as an contact.
However if the pick line does not intersect the object's origin but it intersects some part of the collision image it is not picked up as a contact.
I'm looking for some code or a solution where both situations have the outcome of being a contact.
Looking at the code John posted, it doesn't look like it takes into account the collision images of the contacts any only takes into account contacts that have the same angle as the ray angle (the angle between the two points).
If I'm missing something here, please let me know.
07/12/2009 (6:03 pm)
Hi Jason,Sorry, let me be a little clearer on my issue.
I'm stating your interpretation A above. What I'm seeing from running John's code is the following:
If I call the pick line and the line intersects an object's origin (i.e. a T2DSceneObject's position) then the pick line picks it up as an contact.
However if the pick line does not intersect the object's origin but it intersects some part of the collision image it is not picked up as a contact.
I'm looking for some code or a solution where both situations have the outcome of being a contact.
Looking at the code John posted, it doesn't look like it takes into account the collision images of the contacts any only takes into account contacts that have the same angle as the ray angle (the angle between the two points).
If I'm missing something here, please let me know.
#3
07/14/2009 (7:53 pm)
i think you'd have to fire a blank scene object and use that as a ray cast
#4
The nice thing about this problem is that no sweeping is required. Pickline is strictly an "instantaneous collision" which is much, much simpler.
Anyway, the work to take John's code and rebuild it this way should not be that much work.
07/14/2009 (10:45 pm)
I think the solution is actually really simple, I just haven't looked at it much more. Basically, you need to intersect the AABB of each object with the ray and for those that pierce, you need to rotate the collision image and intersect that with the ray. That will result in the set of collisions.The nice thing about this problem is that no sweeping is required. Pickline is strictly an "instantaneous collision" which is much, much simpler.
Anyway, the work to take John's code and rebuild it this way should not be that much work.
#5
Thanks for the tips. After a few days of looking up some computational geometry algorithms (note the few days part, I'm no expert in this area or in vector math for that matter :)), I was finally able to figure this out.
I understood the first test you mentioned, but not the second. For the first test I just ended up doing a line segment - AABB test and if the collision occured there then I did a segment - polygon test with each collision image to see if it actually collided with any of those.
Anyways, I would post the code here for others to use but unfortunatly it is too much to post. If anyone else is interested in doing something like this here are some good resources I found:
Explaination and implementation of line segment - axis aligned rectangle intersection
Explaination and C++ implementation of intersection of a line segment with a convex polygon
Explaination and C++ implementation of point in polygon winding number inclusion
Take note of Torque's Y axis coordinates when you implement these algorithms, some of them need to be slightly modified to take that into account.
07/17/2009 (5:25 pm)
Hey Jason,Thanks for the tips. After a few days of looking up some computational geometry algorithms (note the few days part, I'm no expert in this area or in vector math for that matter :)), I was finally able to figure this out.
I understood the first test you mentioned, but not the second. For the first test I just ended up doing a line segment - AABB test and if the collision occured there then I did a segment - polygon test with each collision image to see if it actually collided with any of those.
Anyways, I would post the code here for others to use but unfortunatly it is too much to post. If anyone else is interested in doing something like this here are some good resources I found:
Explaination and implementation of line segment - axis aligned rectangle intersection
Explaination and C++ implementation of intersection of a line segment with a convex polygon
Explaination and C++ implementation of point in polygon winding number inclusion
Take note of Torque's Y axis coordinates when you implement these algorithms, some of them need to be slightly modified to take that into account.
Torque Owner Jason Cahill
Default Studio Name
A) Are you arguing that the pickline that John wrote doesn't test against the collision images of all of the objects along the way?
B) Or, are you saying that you want to cast the collision image of a source object in the direction of the destination?
I don't see how his implementation doesn't solve A.