Box2D Forums

It is currently Thu May 23, 2013 9:46 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Mon Jul 19, 2010 4:08 am 
Offline

Joined: Mon Jul 19, 2010 3:36 am
Posts: 7
hey, guys:
I want to show the movement path of the body to the screen before the body moves.
I tried to calculate the movement path by myself,but i failed.The path i calculated doesn't match the actual path the body moves.
how can i get it?

Thanks.
cruel


Top
 Profile  
 
PostPosted: Mon Jul 19, 2010 5:07 am 
Offline

Joined: Fri Apr 23, 2010 6:15 am
Posts: 73
It depends on what you actually mean by "path", and what things you want to include in the prediction of it. Many things can affect a body, most of which you wont know until the frame it actually affects the body, such as collisions and other forces acting upon it. Everything is calculated at real time and box 2D doesn't know any thing other that what's "on the screen".
However, there's a couple of easier predictions to make, such as just showing the vector to where it'll be in two seconds if it continues at the same velocity. You could step that one up and assume the target is going in a curve by calculating three points in the near past to get a fourth curve point in the future.
There's a ray casting function that you can use as a "laser sight" that'll bounce of the surfaces. Many old pool games has this feature to let the player aim the pool balls with a bit more ease.
Collision prediction might be a tough one, and it would probably require a whole lot more resources than it's worth.
I could see it plausible to use both velocity/curve and laser sight prediction for a player object, but much beyond that would start getting quite complex.
If it is turned based, these calculations may take as much resources they like, practically. You may use a copy of the game world some how, that calculates several steps ahead and shows the result? That would double all calculations, and would probably NOT be recommended for real time games, unless very few bodies are used.

What is it exactly you're trying to do? What's the game about and what is the use of this feature?


Top
 Profile  
 
PostPosted: Mon Jul 19, 2010 9:24 pm 
Offline

Joined: Mon Jul 19, 2010 3:36 am
Posts: 7
Thanks for replying.

Zoomulator wrote:
...Many old pool games has this feature to let the player aim the pool balls with a bit more ease...

What is it exactly you're trying to do? What's the game about and what is the use of this feature?


It is very simple, just like you said "the pool games", i just want to let the player throw some bodies to a basket, once time a body, all other things are static.The bodies have different attributes, such as initial position,velocity,mass etc.So I want to give the player some clue to adjust the direction and aim at the basket before he throw the body.

The body simulate parabolic movement, I give it a horizontal velocity and a vertical velocity, then I calculate the movement path.The result is that the path I got doesn't match the actual one.Then I print the actual path, got the curve by calculating three points.I found the horizontal velocity kept the same as I assumed, but the vertical velocity changed.

The formulas as fallowed:
x = vx * t
y = vy * t - 1/2 * g * t^2
vx is horizontal velocity, vy is vertical velocity, g is gravity.
replace the t in the 2nd formula, then i got:
y = vy/vx * x - 1/2 * g *x^2 / vx^2

I assume vx = -50, vy = 40, g=10

then i use the actual path's three points:(318,142),(130.5,217),(30.5,199.5)
and the general parabolic curve:y = a * x^2 + b * x + c, i got
y = -0.002 * x^2 + 0.497 * x + 186.202
so i got:
-0.002 = -1/2 * g / vx^2 ---> vx^2 = g / 0.004 = 2500 ---> vx = 50
0.497 = vy / vx ---> vy = 0.497 * vx ---> vy = 24.75

the actual vertical velocity doest't equal the one i assumed.

I don't know what's wrong.
is there air damp or something else?


Last edited by cruel on Mon Jul 19, 2010 11:15 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Mon Jul 19, 2010 10:55 pm 
Offline

Joined: Fri Apr 17, 2009 9:01 pm
Posts: 143
Although I've not tried it myself, I imagine it might be difficult to accurately predict the path that a body in Box2D will take, especially one affected by gravity, drag, or other forces.

If you really need this feature though, here's an idea (this is entirely speculative, but it might at least be worth considering).

Create a second physics world that is an exact duplicate of the first (i.e. exact same setup with respect to bodies and so on). In the 'virtual' physics world, throw the object, and then run the 'step' function as fast as possible (i.e. not in real time) until the body collides with something, leaves the play area, or whatever. With each step, record the position of the body, and store the sequence of positions in an array.

Now, if the conditions between the two physics worlds are exactly the same, the path traced out by the 'virtual' body should be the same as the path that would be traced out by the actual body were it propelled in the same manner. (Since you're running the simulation as part of the same application, there shouldn't be any issues with floating-point representation or anything of that sort.)

I don't know enough about the context to say for sure, but I think it's possible that the 'virtual throw' could execute quickly enough that the 'projected path' visual could be a real-time effect. That's just a guess though; it would depend on quite a few factors. If it *didn't* turn out to be fast enough, you might be able to optimize by only simulating the thrown object (or something of that sort).

Again, this is just speculative, but it's probably what I'd try first.


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 4:33 am 
Offline

Joined: Mon Jul 19, 2010 3:36 am
Posts: 7
Jesse wrote:
...Create a second physics world that is an exact duplicate of the first (i.e. exact same setup with respect to bodies and so on). In the 'virtual' physics world, throw the object, and then run the 'step' function as fast as possible (i.e. not in real time) until the body collides with something, leaves the play area, or whatever. With each step, record the position of the body, and store the sequence of positions in an array...


that's a good idea.

but i want to let the player set the parabolic movement path by himself. the body's initial position is random generated by the game, the player could specify the parabolic top point by mouse, and then i could get the symmetry point of the body's initial position by the vertical axis and the parabolic top point.using the three points, i would get the parabolic curve.

use this formula
y = vy/vx * x - 1/2 * g *x^2 / vx^2
i could get horzontial velocity and vertical velocity, and apply them to the body.the body would follow the movement path the player specified to move.

that is what i want.

but i don't know what's wrong with my calculating.

and i don't konw how to deal with the body's parabolic movement in the box2d.
besides the gravity, taking account of the air damping or buoyancy force or anything else?

anybody can help me?

again, only one body moves once a time and all other bodies are static when the body moving.


Top
 Profile  
 
PostPosted: Tue Jul 20, 2010 9:01 am 
Offline

Joined: Fri Apr 17, 2009 9:01 pm
Posts: 143
Quote:
but i want to let the player set the parabolic movement path by himself.

Ah, I see. My suggestion probably isn't applicable then.

To be honest, I can't think of a way off the top of my head to do what you're wanting with Box2D. (That doesn't mean there isn't a way - it just means I can't think of one :)

It looks to me as if you want to make use of an analytical solution, but like all physics engines (more or less), Box2D uses an integrator. As such, the path of the object is an approximation of the 'actual path' given the same parameters.

To allow the user to specify a parabolic path for the object, it seems you would have to 'reverse engineer' the path to yield a set of starting parameters that, given an application of the integrator over some number of steps, would yield something very close to the specified parabola. This sounds somewhat difficult to me, but maybe there's some way it could be done.

Given your requirements though, it seems the easiest solution would be not to use the physics engine and to move the object yourself; that way, you could compute the current position of the object analytically to yield a path that exactly matched the specified parabola (within the limits of machine precision, at least). You might also be able to use a kinematic body and set the velocity manually so as to create a piece-wise approximation of the parabola.


Top
 Profile  
 
PostPosted: Fri Jul 23, 2010 12:43 pm 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
cruel wrote:
and i don't konw how to deal with the body's parabolic movement in the box2d.
besides the gravity, taking account of the air damping or buoyancy force or anything else?


See http://en.wikipedia.org/wiki/Ballistic_ ... resistance

IIRC, in Box2d air damping is a linear drag proportional to velocity, so you should be able to solve the equations. You may need to do some numerical approximation to invert the equations (you're really asking an inverse problem, "What's the velocity that I would need to make a projectile peak at a certain point?", which is a more difficult problem than just solving for a trajectory based on the initial conditions), since I doubt if they can be inverted by hand - give it a try, though, I could be wrong...

Personally, I'd probably just figure out a way to store and reset the state of Box2d (if all your objects are standing still and are not stacked or anything, this could be as easy as storing away the positions), do a trial and error search (binary search, maybe) over your parameters, keep simulating test runs until you match the desired trajectory within a certain precision, and then once you've found the right parameters, start the "for real" simulation running again.

Not the prettiest solution, but what you're trying to do is not easy, so it's going to take some effort.

FWIW, the problem is easier if you turn off damping, then you should be able to find an explicit formula - I'm not sure that "y = vy/vx * x - 1/2 * g *x^2 / vx^2" is right, though, where did that come from?

My quick back-of-the-envelope math (which is quite possibly wrong, buyer beware!) says, assuming x(0) and y(0) are both 0 (adjust accordingly, also vx and vy are the initial velocities):
Code:
x(t) = vx*t
y(t) = vy*t - .5gt^2
=> t(peak) = vy/g =>

x(peak) = vx*vy/g
y(peak) = vy^2/g - .5*vy^2 / g = .5 vy^2 / g

You would first solve for vy, then use that to get vx. Again, though, that requires no damping. I'm also not 100% positive that Box2d will exactly match the analytical solutions, it does discrete stepping so there's always going to be a bit of error.


Top
 Profile  
 
PostPosted: Wed Jul 28, 2010 5:48 am 
Offline

Joined: Mon Jul 19, 2010 3:36 am
Posts: 7
ewjordan wrote:
FWIW, the problem is easier if you turn off damping, then you should be able to find an explicit formula - I'm not sure that "y = vy/vx * x - 1/2 * g *x^2 / vx^2" is right, though, where did that come from?

My quick back-of-the-envelope math (which is quite possibly wrong, buyer beware!) says, assuming x(0) and y(0) are both 0 (adjust accordingly, also vx and vy are the initial velocities):
Code:
x(t) = vx*t
y(t) = vy*t - .5gt^2
=> t(peak) = vy/g =>

x(peak) = vx*vy/g
y(peak) = vy^2/g - .5*vy^2 / g = .5 vy^2 / g

You would first solve for vy, then use that to get vx. Again, though, that requires no damping. I'm also not 100% positive that Box2d will exactly match the analytical solutions, it does discrete stepping so there's always going to be a bit of error.


A valuable way you supply!It's worth to try.

How to turn off air damping?
just like this:
bodyDef.linearDamping = 0;

From the two formula
x = vx * t
y = vy * t - 1/2 * g * t^2
I get the forumla "y = vy/vx * x - 1/2 * g *x^2 / vx^2".


Top
 Profile  
 
PostPosted: Wed Jul 28, 2010 11:44 am 
Offline

Joined: Fri Jan 23, 2009 12:53 pm
Posts: 177
Why can't you just simulate it and save the path? You can use another world for this :-\


Top
 Profile  
 
PostPosted: Wed Jul 28, 2010 11:55 am 
Offline

Joined: Fri Apr 17, 2009 9:01 pm
Posts: 143
Neosano wrote:
Why can't you just simulate it and save the path? You can use another world for this :-\

If you read the whole thread, you'll see that this has already been suggested, and that the OP has already stated that this solution won't work for him (or is at least under the impression that it won't work for him).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group