Trouble intercepting a moving entity from behind

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
BrianL
Posts: 54
Joined: Wed Jun 25, 2008 4:41 pm
Location: Panama City Beach, FL

Trouble intercepting a moving entity from behind

Post by BrianL »

I have a moving entity that I'm shooting a projectile (another moving entity) at. The desired behavior is "homing". I'm using the ContactCreated event to be notified when the two entities collide.

However, the problem arises when the tracking entity tries to close in on the target from behind. It closes the gap, but never completely arrives. It just sits right behind the target and trails it. I've tried this two different ways:

1. Manually adjusting the LinearVelocity of the tracking entity such that --> TrackingEntity.LinearVelocity = TrackingEntity.LinearVelocity + (TargetPos - TrackingPos) * SomeFractionOfThatDelta;
2. Using an EntityMover. This results in exactly the same behavior as #1.

I think I understand what is happening. The tracking entity is trying to move to the location where the target entity is THIS frame. However, by the time the tracking entity updates and moves, the target object has done the same. For case #1, I tried computing where the target entity should be one frame in the future based off its current position and velocity, but it didn't seem to help.

Any ideas? Thanks.
BrianL
Posts: 54
Joined: Wed Jun 25, 2008 4:41 pm
Location: Panama City Beach, FL

Re: Trouble intercepting a moving entity from behind

Post by BrianL »

BrianL wrote:1. Manually adjusting the LinearVelocity of the tracking entity such that --> TrackingEntity.LinearVelocity = TrackingEntity.LinearVelocity + (TargetPos - TrackingPos) * SomeFractionOfThatDelta;
Even if I go:

Delta = (TargetPos - TrackingPos);
Delta.Normalize();
TrackingEntity.LinearVelocity = TrackingEntity.LinearVelocity + Delta * SomeConstantGreaterThanOne;

It still doesn't close the gap. It's strange. If I increase the size of the tracking entity (which is currently a sphere) it seems like the gap widens, as if there is some kind of margin or boundary that's stopping it, but not registering a collision. Is that possible?

Edit #1: Remaining points of interest: All damping values are set to 0, the sphere has a positive mass, and is not affected by gravity.

Edit #2: I've discovered that setting the collision margin on the tracking entity (sphere) to something positive (I don't know what the default is) seems to really help. It feels dirty to me though, but maybe that's the right way to go?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Trouble intercepting a moving entity from behind

Post by Norbo »

I've discovered that setting the collision margin on the tracking entity (sphere) to something positive (I don't know what the default is) seems to really help. It feels dirty to me though, but maybe that's the right way to go?
Collision margins must be positive; in fact, for spheres, the collision margin is the radius of the sphere. If the radius is negative, weird things will happen. Though, if you didn't explicitly set the radius/collision margin to a negative value before, it isn't the source of the issue.

A better targeting algorithm might help assuming there isn't something else going on. Compute the target location based on the target entity's position and velocity, and the time it will take the missile to reach the previous targeted position. Multiply the target's velocity by the time until intercept and add it to the target entity's position. The linear velocity of the interceptor can be any value above that of the target entity. If it's not a constant speed, you'll need to change the time until intercept estimate to take it into account.

Assuming that the algorithm isn't to blame, could the entities have collision rules such that they cannot collide?
BrianL
Posts: 54
Joined: Wed Jun 25, 2008 4:41 pm
Location: Panama City Beach, FL

Re: Trouble intercepting a moving entity from behind

Post by BrianL »

Norbo wrote:
Collision margins must be positive; in fact, for spheres, the collision margin is the radius of the sphere.
BTW, I'm using version 0.14.3.0.

Are collision margins something that are automatically set internally for entities like spheres, boxes, etc? Or do they need to be set manually? I'm asking because since you mentioned it, I took a look at the Sphere after constructing it with the (Position, Radius) constructor and the CollisionMargin still read 0.

I also tried assigning the Radius manually after construction, and the CollisionMargin still read 0. If I manually set the CollisionMargin = Radius, then it works as expected.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Trouble intercepting a moving entity from behind

Post by Norbo »

Are collision margins something that are automatically set internally for entities like spheres, boxes, etc?
Yes, in v0.14.3 they start with 0 and then inherit the default margin (Space.SimulationSettings.CollisionDetection.DefaultMargin) when added to the space. The sphere's radius is NOT the collision margin in v0.14.3 either. In v0.15.0 and above, the margins are set on creation automatically to the appropriate value (and they should be mostly ignored).

Knowing it's v0.14.3, I'm going to guess it's CCD. It can be hard for two moving objects to actually create contacts with each other if CCD starts jumping in hyperactively. Try disabling continuous collision detection and seeing if the problem goes away. Having a large margin could help since contacts are created using the margin, while CCD uses the non-margin expanded shape (if I remember correctly).

v0.15.0+ does not have that issue; CCD was rewritten from scratch, along with most of the rest of the engine. I'd recommend moving to the new version if you are able to do so.
Post Reply