How to draw a preview of an object move?

Hi everyone,

Do you know if its possible (and desirable in term of performance) to draw the path that will take a physics object once a force will be applied on it ?

To be more specific, I’m trying to code a small basketball game in which you have to shoot to the goal from a fixed position. The player can only adjust the scale of a vector that point toward the goal by pressing the space bar and the height of the y axis by pressing it again. Then this vector is used to apply an impulse on the ball ( an exemple is available here Babylon Template :slight_smile: )

What I want to do is to give to the player a live preview of where his shot’s adjustments will lead the ball by displaying a curve that will show the path of the ball if the applyImpulse was applied with the current user defined parameters.

Hi @paulbonneau ,

Take a look at:

Seems that @Drigax didn’t get around to making that playground unfortunately…

Hi @Gijs,

First, thanks for your answer ! Could you help me to understand the formula shown in the post you link ? I have trouble to find how to transpose this formula into my code with Babylon JS Vector3 functions.

Here is what I understand of it :
p(t) = p0 + v t + 1/2a t^2

p0 --> that’s ok it is the Vector3 position of my ball, the origin
v * t --> I think v is the Vector 3 I use in applyImpulse() in order to move my basketball. What I don’t understand is how I could mutliply my Vector3 by a single float/integer t ? With scale() maybe ?
1/2a* t^2 --> Here I’m really lost. How could I multiply my Vector3 gravity by 2 then by t (time) then use this value to divide 1 by this Vector3. I think the most confusing thing for me here is the division of 1 by a Vector3. Should I create a vector3 [1,1,1] then .divide() it by my value ?

Sorry if it is obvious, I’m really bad at Math but trying to learn :slight_smile:

Sorry no, v is the velocity with which the ball leaves the origin. The velocity v is calculated from the impulsive force and the mass of the ball.

1/2at^2 means 0.5at^2 not 1/(2a*t^2)

You need

p = p0.add(v.scale(t)).add(a.scale(0.5 * t * t))

When J is the impulsive force and m the mass of the ball the impulse acts on then the velocity of the ball leaving the origin is J / m, ie J.scale(1 / m)

However, unfortunately, it might not be that straightforward. It depends on the mapping between real world units and the physics engine units and Babylon units. You may have to adjust v and a for the path to follow the ball. Once such an adjustment is found this should be the same adjustment for all v and a.

3 Likes

@JohnK Thanks a lot for your explanations ! It work perfectly for me ( updated demo with the preview implemented Babylon Template space bar to shoot )

3 Likes