Raycasting?

Apr 13, 2009 at 5:19 PM
Is it possible with farseer?
Is it possible without farseer?
Is it possible?
roguecommander suggested i use this for a some enemy AI i was trying to achieve,
he said it might be possible in farseer but he was unsure,
so this is for matt or genbox or anyone who knows anything about Raycasting in general.
And if you do know, please share
Coordinator
Apr 13, 2009 at 5:28 PM
Raycasting is bascially where you make a ray with a starting point and an angle. Farseer Physics currently can make line to line (and line to AABB and line to Geom) testing. This is simple done by creating two lines, using linear model functionality and set the two functions to equal each other (F(x) = G(x)). This will give you the point of crossing of the two lines.

line to line checking is what people want in most cases, it depends on what you are going to use it for.
It is possible with Farseer (as described above) and it is also possible without. Lines are just two points in space and you don't need Farseer to have that.

True raycasting is where you actually use rays. Also, the way it is currently implemented into Farseer, lines/rays are not used in the broadphase (or narrow phase). Using them in the broadphase can increase performance of line checking. I hope we will be able to do that in the future. (Matt is actually working on this as we speak.)
Apr 13, 2009 at 5:58 PM
I just had a look and it seems that you would have to manually iterate all of the Geoms in the simulation.

So, for each Geom, call the LineSegmentGeomIntersect function (located in Collisions.CollisionHelper), and store the closest Geom and the closes intersect point on that Geom. That should give you the visibility information you need. I think that this is pretty much the same as ray casting, except that rays would normally use an infinitely long line segment starting at some point, as opposed to a starting point and an ending point.

For your visibility purposes tlegion (I read your other thread about this), line segments are probably perfectly sufficient as you only really care about Geoms between the player and the enemy. Sound strength is probably not so simple - you may be able to approximate it with multiple segment tests to see if the enemy is just around the corner from the player.

Coordinator
Apr 13, 2009 at 6:09 PM
I'm not aware of any additional info but what is provided in this thread, but if line-of-sight is what is needed, then it's quite easy.

For AI that needs to traverse a world of obstacles I would recommend a graph search algorithm such as A* or Dijkstra's algorithm. They can prove to be hard to implement unless you use a game engine that prove the implementation for you.

Anyway, if you want to check if there is a direct line between two geometries, you just use the geometries positions as the two line points and check if there is more than two intersections (the 2 geometries would be the 2 intersections).
For this you would need to iterate all the geometries in the world. This is an O(n^2) operation and can be quite slow. (just see the post I made about the brute force broad phase collider versus the other colliders: http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=44448)

To improve performance, we would like the raytesting to be combined with the broadphase algorthms. This improves performace a lot by narrowing down the amount of geometries checked.
For an example on how to use the raycasting functionality of Farseer Physics, just download the latest source code checkin and have a look at AdvancedSamples new demo. (can remember the number...)
Apr 13, 2009 at 8:20 PM
Edited Apr 13, 2009 at 8:48 PM
THank you so much guys for helping me out and being quick with the responses,
I think i will do the method roonda suggested simply because it has a max and min parameter,
If i were to just do a line between the geometries(player and enemy) the field of vision would be way to far(unlimited sight is a bit to far)
I am going to check out the sample now and see how it works,
is there a way other then roondas way to do a raycast type method but have a limit?
And are there any dissadvantages to roondas?

edit:
I just checked out the line demo and am not sure what to say,
I am not sure what was going on,
i sa the square and circle,
then in debug view i saw a line near the angle border but when things colided with it nothing happened.
Then for some reason when i play with the xbox controllers thumbsticks it would say iether 1 geom collides or 0, but i didnt see anything hapeing onscreen.
Apr 14, 2009 at 4:30 AM
I'm not sure but I think genbox is talking about pretty much the same approach that I was. Definitely use the latest checkin code as I suspect that CollisionHelper code has changed since my version (2.0.1 release). Maybe put a breakpoint in LineSegmentGeomIntersect and run the new demo to see how it's being used.