3D Projectile Tracking

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
cincoutprabu
Posts: 15
Joined: Mon Jan 12, 2015 11:59 pm

3D Projectile Tracking

Post by cincoutprabu »

Hi, I would like to find the projected path of a moving object and wondering whether this is possible before the object starts moving! Projected path means the list of all 3D points when forces are applied to an object (until the object goes to a rest position). I am aware that the object movements can be captured using Entity.PositionUpdated event, but the question is whether the object movement path can be deduced before the object starts moving; and whether we can find the point of impact or collision with an invisible plane in the scene. The object in question is just a primitive entity (sphere with PositionUpdateMode set to Continuous) that moves when linear velocity is applied to it.

If I'm correct, this could be something related to ray tracing and 3D inverse kinematics, but looks like the BEPUik library deals more on joints and bones which are more useful for character controlling (not sure whether I'm missing anything here). And wondering if this is related to ray tracing, then whether BEPUphysics has related math libraries to achieve this. I can see there are Ray and RayHit classes in BEPUutilities namespace and their usage in InverseKinematicsTestDemo code, but in this demo the math are performed while dragging the object and not sure what is the purpose of doing it while dragging.
Last edited by cincoutprabu on Mon Feb 09, 2015 7:49 pm, edited 1 time in total.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: 3D Path Finding and Ray Tracing

Post by Norbo »

In general, it's not possible to get a perfect prediction of simulation without actually running the simulation (and even then, the prediction would only be perfect if the engine was configured to be deterministic), but it is possible to approximate simple unconstrained motion.

For example, you could model an entity as a point with a linear velocity and position. To integrate the position forward in time, you could do something simple like:
velocity1 = velocity0 + acceleration0 * dt
position1 = position0 + velocity1 * dt

This be repeated as many times as desired to approximate the desired time period. If you wanted rudimentary collision testing, you could perform raycasts between position0 and position1.
If I'm correct, this could be something related to ray tracing and 3D inverse kinematics, but looks like the BEPUik library deals more on joints and bones which are more useful for character controlling (not sure whether I'm missing anything here). And wondering if this is related to ray tracing, then whether BEPUphysics has related math libraries to achieve this. I can see there are Ray and RayHit classes in BEPUutilities namespace and their usage in InverseKinematicsTestDemo code, but in this demo the math are performed while dragging the object and not sure what is the purpose of doing it while dragging.
This path projection is forward dynamics. It takes some initial states and just figures out where they go step by step. BEPUik (and inverse kinematics in general) does the opposite: it takes a goal state, and works out how to set stuff up to reach that goal state. It's useful for figuring out the state of the arm needed to put a character's hand on a doorknob without having to define every joint angle yourself as a special purpose animation, for example.

The BEPUutilities library provides a variety of primitive intersection routines, like ray-plane, ray-box, ray-sphere, and so on. You could use these if you wanted. BEPUphysics also supports various kinds of ray queries, too. For example, collision shapes, the broad phase, and the Space itself all have some kind of ray test.
cincoutprabu
Posts: 15
Joined: Mon Jan 12, 2015 11:59 pm

Re: 3D Path Finding and Ray Tracing

Post by cincoutprabu »

Hi Norbo, thanks for the reply and for the formulas too!

But can you clarify whether the above formulas apply for a sphere that has bounces and downward forces (Y component of linear velocity) applied to it. Leaving aside the path tracking/finding, the simple question would be how to find the point of impact of a bouncing sphere against a known plane. The scenario is very simple, if there is a sphere at (x1,y1,z1) and a linear velocity of (xL,yL,zL) is applied to it, how can we find the time taken T and point of impact (x2,y2,z2) with a known plane. Imagining like throwing a ball in hand against a distant wall in front with different speeds (linear velocities), the objective is to find the time taken and point of impact (point at wall). RayCast method from InverseKinematicsTestDemo throws some light on this; it tries to calculate the time of impact which is then used to compute the 3D hit location, but I don't think bounciness factors are ever considered there.

As a side note, (xL,yL,zL) is non-zero, which means the sphere is not moving in a straight line on any axis, and considering the fact that the sphere has some bounciness too means wondering whether the problem is something complex and beyond the scope of ray hit testing.

EDIT: Looks like this is similar to hawk-eye algorithm, but the objective is not to find the full trajectory of the sphere, but if we can find the time-taken and target point-of-impact with 3d math using BEPUphysics library, it would be very helpful!!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: 3D Path Finding and Ray Tracing

Post by Norbo »

The scenario is very simple, if there is a sphere at (x1,y1,z1) and a linear velocity of (xL,yL,zL) is applied to it, how can we find the time taken T and point of impact (x2,y2,z2) with a known plane.
This has a simple analytic form, provided that the acceleration on the ball is constant and there is no damping. It is equivalent to projectile motion. You can compute the initial vertical velocity (velocity on the plane normal) and the initial height (offset from ball to plane point on the plane normal). With gravity, you've got everything needed to use one of the equations of projectile motion.

Bounces would involve computing the time of impact and the vertical velocity, reversing the vertical velocity, and computing the next time of impact.

To compute the point of impact, multiply the initial velocity by the time of impact, add it to the initial position, and project it onto the plane.

You could also modify the incremental approach mentioned in the previous post to take into account collisions. That's just moving closer to what the engine itself does- it takes individual time steps and analyzes the state of the world each frame to compute collisions and changes in velocity.

Note that it is quite unlikely that a real BEPUphysics simulation will match up to the predictions of either of the above approaches after interacting with other objects. Collisions are chaotic, so if it isn't exactly the same simulation, there is going to be divergence.
cincoutprabu
Posts: 15
Joined: Mon Jan 12, 2015 11:59 pm

Re: 3D Path Finding and Ray Tracing

Post by cincoutprabu »

Thanks for the reply. I'm able to find the time-of-flight and target hit-point of a sphere using projectile motion formulas when the sphere is dropped from a height (top to bottom) or after it bounced back from the floor (bottom to top). The formulas require initial velocity (iv) in order to calculate both time-of-flight and target hit-point. The very first iv is based on user input and subsequent iv's are taken from sphere's current velocity computed automatically by BEPUphysics whenever ball changes vertical direction (bottom to top after bouncing or top to bottom after reaching peak height). But the question is how to find or predict the sphere's velocity in both these cases beforehand. In other words, I wanted to find the bounce back velocity of the sphere (in all axes x, y, z) before it actually bounces and free fall velocity of the sphere before it actually reaches the peak height after bouncing. Basically the question is to find how BEPUphysics calculates/changes the velocities when vertical direction of the sphere changes after bouncing or after reaching peak height. It looks like only if I have these information beforehand (while applying very first initial velocity), I will be able to find the final time taken and final target hit point on a distant wall/plane after it has traveled a distance with few bounces.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: 3D Projectile Tracking

Post by Norbo »

If you know the time of impact and the accelerations applied to the body over that time period, then the vertical velocity at the time of impact is InitialVerticalVelocity + TimeOfImpact * VerticalAcceleration.

To approximate the vertical bounce velocity that BEPUphysics computes, take the estimated vertical velocity on impact and multiply it by the negative bounciness of the collision pair.
cincoutprabu
Posts: 15
Joined: Mon Jan 12, 2015 11:59 pm

Re: 3D Projectile Tracking

Post by cincoutprabu »

Thanks for the reply. However, it is still a nightmare to calculate the change in velocity because of bounce (collision).
Norbo wrote:To approximate the vertical bounce velocity that BEPUphysics computes, take the estimated vertical velocity on impact and multiply it by the negative bounciness of the collision pair.
Not sure what is mean by negative bounciness!! Everything else like time-of-flight, point-of-impact and target velocity were calculated for both directions (ball falls down from peak height or moves up after bouncing). The only missing link in predicting the ball trajectory (i.e. final target hit-point of the ball) is how BEPUphysics changes the velocity during bounces. By hooking up to CreatingContact, ContactCreated and PositionUpdated events, velocity just before and just after bouncing are captured, but the question is how this calculation is done. To give examples, the following is an excerpt from the log written by the app, with ball-start-position as (-40, 90, 110) and initial velocity applied to ball is (40, -30, -130).

12-02-2015 06:30:55 CreatingContact Ball is about to hit floor when ball is at {-13.8366, 53.1597, 24.9689} with velocity {40.0000, -83.6280, -130.0000}
12-02-2015 06:30:55 PositionUpdated Ball direction changed to UP when ball is at {-13.57345, 54.22475, 24.11376} with velocity {15.78728, 63.90438, -51.30866}
12-02-2015 06:30:57 CreatingContact Ball is about to hit floor when ball is at {12.1319, 53.1781, -59.4286} with velocity {15.7873, -65.5876, -51.3087}
12-02-2015 06:30:57 PositionUpdated Ball direction changed to UP when ball is at {12.36016, 54.01344, -60.17044} with velocity {13.69534, 50.11945, -44.50981}
12-02-2015 06:31:01 CreatingContact Ball is about to hit floor when ball is at {64.9413, 1.0568, -231.0367} with velocity {12.6297, -81.2443, -41.0358}
12-02-2015 06:31:01 PositionUpdated Ball direction changed to UP when ball is at {65.10074, 2.091417, -231.555} with velocity {9.569519, 62.07996, -31.09526}
12-02-2015 06:31:02 CreatingContact Ball is about to hit floor when ball is at {80.2406, 1.0542, -280.7500} with velocity {9.5695, -63.4880, -31.0953}
12-02-2015 06:31:02 PositionUpdated Ball direction changed to UP when ball is at {80.37773, 1.862704, -281.1955} with velocity {8.227367, 48.51278, -26.73221}

Wondering whether we have a quick and dirty formula or technique to apply on v1 to get v2, if v1 is velocity just before bounce and v2 is velocity just after bounce. The colliding objects are a simple sphere (with bounciness=0.9) and a static mesh (floor with bounciness=1.0). Friction coefficients are left to default values and no special MaterialBlenders are used.

I have spent hours on trying to understand the BEPUphysics classes (like SphereCharacterController, SpherePairHandler and Toolbox) where I can see calculations related to linear velocity, relative velocity, etc, but to no avail. Any help is greatly appreciated and would be much helpful!!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: 3D Projectile Tracking

Post by Norbo »

Wondering whether we have a quick and dirty formula or technique to apply on v1 to get v2, if v1 is velocity just before bounce and v2 is velocity just after bounce. The colliding objects are a simple sphere (with bounciness=0.9) and a static mesh (floor with bounciness=1.0). Friction coefficients are left to default values and no special MaterialBlenders are used.
With the default multiplicative blending, a pair of objects with bouncinesses of 0.9 and 1 will have a pair coefficient of 0.9 * 1 = 0.9. So, the approximate bounce calculation would be:
PostBounceVelocityAlongCollisionNormal = -0.9 * ImpactVelocityAlongCollisionNormal

The negation is there because the bounce velocity is going to point in the opposite direction from the impact velocity. In other words, the object is changing from moving towards the surface to moving away from the surface.

Note that these are not the full 3d velocities, these are only velocities along the collision normal. So, if the surface the ball is hitting is a horizontal plane, the collision normal is just (0, 1, 0), whch means the 'velocity along collision normal' is just the Y component of the velocities.

(The components of the velocity that are not aligned with the collision normal are not changed by the bounce itself. Friction would affect those velocities, but modeling the effect of friction correctly is trickier, especially with objects that can rotate. Ignore it if you can. You can simplify things by giving the ball zero friction.)
I have spent hours on trying to understand the BEPUphysics classes (like SphereCharacterController, SpherePairHandler and Toolbox) where I can see calculations related to linear velocity, relative velocity, etc, but to no avail.
Bounciness is handled in the ContactPenetrationConstraint. Search for Bounciness within the Update function. It's more complicated than what I mentioned above, but it boils down to the same thing in the average case.
Post Reply