Ammo Physics Question

I’d like to render a simple path/ indicator that shows the path that a rigid body/Collision object would take if it’s ‘thrown’ and also where it might land. The idea is to give users a visual indicator/UI that helps them aim physics objects. Think angry birds but 3D.

IIRC bullet is non deterministic, so it’s not possible to know ahead of time where something might land, but could you potentially run the physics loop ahead of time to find the first intersection? Or potentially, is there a more crude way of just stepping through a trajectory and applying gravity along the route? I suspect there’s an analytical way of doing this, but I also want to make sure it lines up with the actual physics engine to a reasonable degree, so that thrown objects come close to where the indicator shows

Hi @Mark_Lundin,

You can’t do that out of the box with the rigidbody system class, but if you study its source code you can see that you can easily replicate this in a class of your own.

Basically implement a dynamics world spawner, that allows you to add bodies, set their state and run the physics loop as many times as you like in a single frame.

That way you can record the positions for each frame and then use them for a future projection.

It sounds like you should perhaps not use physics for this thrown object at all … and just use parabolic curve to both predict where it lands, and then also move the object along this. I’ve done this in multiple games in the past. I had 100s of projectiles moving this way. At the time the projectile is thrown you can even do some raycasting along the last 20% of the path or something similar, estimate where it hits … and then adjust your target point and the curve to exactly pass through this point. This makes per frame cost very cheap … just move along curve, and you already know ahead of time the exact hit point.

3 Likes

Yes, I’m actually leaning towards this route. I think looking ahead per frame using Bullet could be quite unnecessarily expensive, and as you say a parabolic would be a much simpler check.

Good to know about the implementation though @Leonidas, there’s probably a few good use cases where this would be really handy

1 Like

I would also go with an analytical approach. If you know the gravity, the impulse vector and the object’s mass, you can calculate a trajectory parabola. It will be the same as Ammo physics will come up with during the simulation.

Another option is to go from the other way around. Knowing the target point, you can find a needed impulse to send the object there. If you want to limit a max height, then the gravity would be variable. A player would then move the target cursor, instead of changing the starting angle/power. I have a small library that can help with calculating the impulse in that case: https://github.com/LeXXik/ballistics

1 Like

Nice! That’s a great little library @LeXXik! Nice work

1 Like

I want same game physics mechanism while Swiping on screen parabolic or trajectory path in my Game
Here is the Game reference link

Hi, and welcome to PlayCanvas!

What is your question exactly? If you want the analytical approach, it is shown in this example:

https://playcanvas.com/project/802204/overview/ball-trajectory

1 Like

Thanks,
Ok, I will try it.