Calculate height of projectile

Hello.

I know Box2d is a 2d engine (as the name suggests 😉 ) but I'd like to fake a third dimension (height). I've got a catapult projectile launching from top to bottom (birds eye view) and everything works fine. What I'd like to is calculate the theoretical height of the object after launch. Usually, I'd calculate a 3D trajectory like so:
Code:
float zVal= Math.Cos(angle);
launch.X = (float)(Math.Cos(angle) * zVal);
launch.Y = (float)(Math.Sin(angle) * zVal);
launch.Z = (float)Math.Sin(angle);
launch.Normalize();


How can I convert this into Box2D? At the moment, the X axis is left-> right, Y is top->bottom and the theoretical Z-axis will be height (coming out of the screen). Since I'm launching my projectile via ApplyLinearImpulse, I'm not sure what the best way to calculate the Z-axis value would be and how to tie it in with the simulation. I'd be grateful is anyone had any suggestions on how to do this (or a better way)?

Thanks in advance
You can create a specialized contact filter for this purpose I think.
There's two options that should work.

    1. Using the b2ContactListener and on the PreSolve callback to cancel any contacts where the objects don't match in height and altitude. b2Contact->SetEnabled(false)

    2. The b2ContactFilter is probably the most sensible thing to use, it's ShouldCollide callback can be overridden to do the same thing as with the PreSolve suggestion, but here it simply needs to return a bool.

I shouldn't be quoted on it, but I think the PreCollision callback can be called several times on solving collisions, while the ContactFilter is called once, so I believe the best suggestion to be option 2. The contact filter is only called once when the AABBs starts to overlap, according to the API. Overriding the ContactFilter also removes the collision masking, so you should base your new filter on the one currently existing in box2D if you're using that feature
Thanks very much for your reply.

I'll use the second method you suggested to ensure that there is no collision after a certain height, but how would I calculate the theoretical height each frame (but remain in sync with the simulation)? Do I just continuously add the Z-velocity (minus gravity) each loop, or is there a more accurate way to use the built in integrator?
I'd think that be the most efficient way yes. Do all of the Z calculation each update iteration before the box2D world step. I'm sure it'll be satisfactory.
Thanks for the help! 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=6684