Ray casting collisions

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Ray casting collisions

Post by Rusty8521 »

Is there a way to make ray casts ignore entities in a certain collision group?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Ray casting collisions

Post by Norbo »

There are overloads of ray cast functions that accept a filter predicate (Func<BroadPhaseEntry, bool>) which can include any custom filtering logic, including looking at a broad phase entry's collision group.
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Re: Ray casting collisions

Post by Rusty8521 »

I'm not quite sure how to do that. I've located the ray cast function that I want, but I'm not sure how to pass in a delegate, or what should be in the function for that matter.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Ray casting collisions

Post by Norbo »

Here's an example using a lambda expression for the delegate:

Code: Select all

            Space.RayCast(ray, entry => entry.CollisionRules.Group != ignoredGroup, out result);
Any BroadPhaseEntry with a collision group equal to the ignored group will result in that expression evaluating to false.

Another example can be found in the usage of the grabber in the BEPUphysicsDemos StandardDemo.cs.
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Re: Ray casting collisions

Post by Rusty8521 »

Okay, that kinda works. What I'm trying to do is cast a ray from an enemy to a character. The RayCastResult.hitObject is always null until the character is right next to the enemy. Why is the ray not traveling farther? This is my raycast
=

Code: Select all

space.RayCast(new Ray(enemy.body.Position, destination), 200, entry => entry.CollisionRules.Group != enemyGroup, out result);

Is it a problem that the destination vector is not normalized? Is there anyway to visually see where the rays are being cast?
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Re: Ray casting collisions

Post by Rusty8521 »

Okay, that kinda works. What I'm trying to do is cast a ray from an enemy to a character. The RayCastResult.hitObject is always something other than the character until the character is right next to the enemy. Here is my raycast

Code: Select all

space.RayCast(new Ray(enemy.body.Position, destination), 200, entry => entry.CollisionRules.Group != enemyGroup, out result);
Would it be a problem that the destination vector isn't normalized?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Ray casting collisions

Post by Norbo »

Is it a problem that the destination vector is not normalized?
The length is in terms of the ray direction length. So if the length of the ray direction is 0.01 and the maximum length is 200, the ray will actually be 2 units long.

Also, the function returns true if it hits anything and false otherwise. Checking this is a better approach than looking at the result, since its contents are technically undefined if the ray cast returns false.
Is there anyway to visually see where the rays are being cast?
There's nothing special for space ray casts in particular in the BEPUphysicsDrawer, but visualizing a ray is pretty easy. Just draw a line from ray.Position to ray.Position + ray.Direction * maximumLength (or to the hit point, if one exists).
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Re: Ray casting collisions

Post by Rusty8521 »

This still isn't working. I decided to try casting a ray from vector3(0,0,0) toward the character. If the character is directly below (0,0,0) (or very close to) they ray hits. If the character moves away from that spot then it stopped hitting him. I then tried setting they ray to begin from the same Y location as the character and then direct it to the character's X,Z location. (I have a completely flat ground) They ray hits the ground with a T of .069. If the the ray and the character are at the same height shouldn't the ray be perfectly horizontal?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Ray casting collisions

Post by Norbo »

Sorry, I don't have enough information to know exactly what's going on.

Can you recreate a simple version of the problem in a BEPUphysicsDemos demo so that I can take a closer look?

Are you using the last stable version v1.2.0, or are you using the development fork version?
Rusty8521
Posts: 10
Joined: Mon Jul 02, 2012 9:10 pm

Re: Ray casting collisions

Post by Rusty8521 »

I got it fixed I wasn't understanding how the vectors and the rays interacted.
Post Reply