Questions about predicting trajectory with moving objects

Godot Version

Godot version 4.4

Question

I am trying to make a game where the bullets can bounce off each other while moving. How should I implement the code so that it can predict the trajectory correctly? Thank you so much!

What do you mean by predicting the trajectory? Do you want to show the future trajectory with a line or something?

1 Like

Yes exactly. Right now the best I can do is to draw the trajectory line if everything else is frozen in place. But this isn’t accurate because there are other stuff that are moving as well. I am wondering if there is any way for me to draw the accurate trajectory considering all the other moving collision bodies that will eventually come to collision at some time in the future.

You’re going to have to predict a bullet’s path by using the standard formulas, like they explain here : https://stackoverflow.com/questions/153507/calculate-the-position-of-an-accelerating-body-after-a-certain-time#153517

In your case, the acceleration will be negative as the bullet loses speed over time
You can then draw line segments between the predicted positions.
Have fun,
Cheers !

Thanks for the reply! I’ve tried the method and I realized that my problem is a bit more complex than I first thought. The trajectory needs to predict bounces off other moving objects (like bullet-on-bullet collisions). It feels like the pure formula approach breaks down when the targets’ paths are also dynamic and can change unexpectedly. Say I have 6 bullets bouncing in the room right now, I first need to have the formula for all 6 of them, and then I need to solve for the time when each of them are going to collide (if bullet A collide with B, C, and etc. which have 15 difference combinations, not accounting for the fact that the bullets can bounce off the walls and change their paths completely. I did add a maxium bounce count of 2 so they dont bounce forever). How would you even approach that with the equation in this case? Thank you so much!

1 Like

I don’t think calculating it with formula is the best approach here, because you’d need to invent your own collision detection, bounce, etc. That’d be a bit too complex for your case.

Unfortunately, as far as I know, there is no built in way of simulating physics forward in time in Godot.
What you could possibly do is to make 1 copy of each of your bullets, hide any visual Nodes like Sprite2D so that only the bare physics Nodes exist for these copies. Then, you can put these copies in separate collision layers and masks, so they interfere only with each other. And finally, you can process their physics normally, as you would with your real bullets, with the only difference that you shoot them right away. You can then trace their position and draw a line between their positions over each frame. Or you can make a ghost transparent version of the bullet to show instead of a simple trajectory line. Or maybe do both.
Whenever you change aim with you cannon/gun, you reset the simulation back to the start.
Once you get that going, you can think about speeding up the simulation, if you decide that you need it.

1 Like

I took it up as a challenge for myself and came up with a semi-working solution, although with some caveats.
Here’s a quick demo, there are 2 guns that are simulating shooting the bullet every second. The gray bullets are simulated bullets, that spawn every second and collide only with themselves and the environment, while red ones are actual real bullets, that are spawned when I click LMB and collide only with themselves and the environment.
The caveat I mentioned is that the physics in Godot is not deterministic, meaning your simulation might be different from what will actually happen when you shoot the bullet. There are apparently some external projects that enforce determinism, but I haven’t tested them. If that slight inaccuracy from time to time is fine for you, then my solution should be ok. You can see the issue in the second part of the video, where bullets behave differently on every shot even though I haven’t moved the gun anywhere.
You can find the project files here:

2 Likes

Thank you so much! I will delve into this and investigate!

1 Like

There’s an asset for this in Godot 3. You could probably translate it with a bit of effort.

1 Like