Creating an accurate "aiming line"

Hey, guys.

So I've got this problem where I'm trying to predict where an object is going to land in my Box2D world after applying a certain angle and velocity to it. Think of the aiming line in a pool game, or Peggle, or one of those artillery games. Any idea how this could be done?

I can think of two ways of doing it...

1. The "close enough" way: Estimate how my object is going to travel in the Box2D world using some simple "object accelerating in one dimension" math.

Pros: Would probably work pretty well for simple cases where gravity travels straight down and I want to see where my object travels after being launched.
Cons: Predicting anything after the first collision would be nearly impossible to match up with what Box2D actually does.

2. The "Totally insane but I kinda want to try it" way: Create a second b2World object that's a clone of the first world. Run it for several steps and just observe where my object would travel in that parallel world.

Pros: Would be very accurate. Plus, I like the idea of creating parallel dimensions
Cons: Seems like a lot of work. And I'm lazy.

3. Some much simpler way that I haven't thought of.

Any ideas?
Hi there,
I had the same question a few days ago for my current game.
But all I needed to do was simulate the arch until the first collision.
It came out amazingly accurate considering how long it actually took me to make.(like an hour)
I also had a complex issue of the object force changing during the arch so I needed to simulate that as well but thats another story. Theres probably much better way to do this but i don't care because it didnt take long and it works.
The key variables :
Code:
velX
velY
X=body.GetPosition().x
Y=body.GetPosition().y
gravity
for(i=0;i<50;i++){
velY-=gravity
X+=velX
Y+=velY
}


Run a for loop for a set amount . The more iterations the more detail. decide this number before you decide for the gravity.

To get the gravity number drop a ball from a height and trace position each frame.
Keep this data so you can compare with your simulated positions.
Adjust your gravity and trace your simulated position and compare.

You could try and simulate a collision as well might not be as hard as you think.
Could be quite easy for just a moving object colliding with static ones.

But do you need to or want to ?
I think if you need to be simulating this arch once every frame or more the parallel world would be too slow.

But if its just once everynow and then and if its turn based like pool or an artillery game like worms the best idea would be a
parallel world but dont just copy the whole world just the area needed.
If the user has control of any of the physics objects after they "launch" then you can't predict accurately at all. No way to read the users mind of course.

If i was doing a pool game I would use a bouncing raycast with a fixed length to show some of the initial motion of the ball and the collisions against objects across that fixed length. Peggle likely works in a similar way.

All pool games I have played don't show a 100% accurate result line for the entire simulation sequence, just a little preview of your shot, leaving the rest up to the player. Accuracy at that level just isn't necessary. My advice here would be to design around the problem. Create a simple approximation and design your game such that the simple approximation is all you need to have fun playing it.

Mark Cooke
Others have already said it: if you need real high quality, you're going to need to spawn another world and do the actual simulation; if you don't, you might be able to get away with a simple model, but you're never going to get the same dynamics that Box2d does without using Box2d for real!

What is your exact situation like? Creating a separate world might be feasible or not depending on a few things, namely how many bodies you have, how far ahead you need to predict, how fast the calculation has to be (can you accept a second of lag, or only a thirtieth?), etc.
Mark wrote:
If the user has control of any of the physics objects after they "launch" then you can't predict accurately at all. No way to read the users mind of course.


Yes that's true.
but I was talking about an AI character.
in which case I do predict his arch 😁
Thanks for your thoughts, everybody.

It sounds like in my case, the estimating approach is probably the way to go. Part of me still wants to try the clone world, but only because the idea of it sounds kinda neat. I just wanted to make sure there wasn't some completely obvious, say, b2PredictiveBody class that I was overlooking.

--T
Hello Todd, I am just watching your box2d flash tutorial, Lesson 2.35: Creating the aiming line, part 1.
http://www.kerp.net/box2d/displaylesson ... &lesson=35

I cannot really understand that the line
velocityVect.y += gravityInPixels / granularity;

It works, but what is the logic behind it?

Great tutorial, anyways.... simply amazing!

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