Is it possible to calculate the next speed of a ball knowing only dx and dy of the ball? For example if x = 120, y = 0, dx = -10, dx = 10, gravity = 0.05. So the next position of a ball (next update) is x = 110, y = 10. But a ball will be fall faster and faster under the influence of gravity. So is it possible to calculate next speed based on current x, y, dx and dy? My equation on next update of a ball x position should be x = x + dx * speed * gravity, but I haven't information about current speed, so I must calculate it every update.
-
\$\begingroup\$ Possible duplicate of Ball Physics : Smoothing the final bounces as the ball comes to rest \$\endgroup\$ – Nathaniel D. Hoffman Nov 6 '16 at 21:57
-
\$\begingroup\$ Look at the answer I just posted here: gamedev.stackexchange.com/questions/130961/… You would use the Pythagorean Theorem. \$\endgroup\$ – Krythic Nov 7 '16 at 2:12
-
1\$\begingroup\$ What is 'dx' if not (some form of) the speed? It sounds like you've got several different representation issues in your data right now. \$\endgroup\$ – Steven Stadnicki Nov 7 '16 at 2:50
Consider this image, which uses Pythagoras Theorem:
Here is an example of the formula:
/// <summary>
/// https://en.wikipedia.org/wiki/Pythagorean_theorem
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static float Pythagorean(float a, float b)
{
return (float)Math.Sqrt((a * a) + (b * b));
}
First, sorry for my bad English...
If the speed or velocity is changing over time then yes, you have to update the speed or velocity vector each time step.
I suggest to brake down the problem into peaces that lie on the physics. You have a Display object that has a Position in the world and that object has a current speed applied in a Velocity vector. And then you have Force(s) that changes the speed in the Velocity vector during time. Gravity is a Force but there can also be others forces applied to the object. Think of jumping, this will just be another force to the object that overcomes Gravity for a amount of time, getting smaller in time.
The classes below should give you an idea how to brake the problem in to peaces in an object oriented way.
// Position point
public class Position {
float x;
float y;
}
// Velocity vector
public class Velocity {
float dx;
float dy;
}
// Force vector with update to change the force over time
public class Force {
float fx;
float fy;
public void update( long timeStep, Velocity velocity ) {
// first remove the previous force vector form Velocity to reset
velocity.dx -= fx;
velocity.dy -= fy;
// calculate the new force vector with a specific equation of your need
if ( fy > -6f ) {
fy = fy - 1.0f;
}
// add the new force vector to the Velocity
velocity.dx += fx;
velocity.dy += fy;
}
}
// This is a display object that has a position, a velocity vector and List of forces that applies to the object
// forces can change and also the list of forces can change
public class SomeDisplayObject {
Position position;
Velocity velocity;
List<Force> forces;
public void update( long timeStep ) {
// update the velocity on all forces that act on the object
for ( Force force : forces ) {
force.update( timeStep, velocity );
}
// apply the velocity to the position
position.x += velocity.dx;
position.y += velocity.dy;
}
public void addForce( Force force ) {
forces.add( force );
velocity.dx += force.fx;
velocity.dy += force.fy;
}
public void removeForce( Force force ) {
forces.remove( force );
velocity.dx -= force.fx;
velocity.dy -= force.fy;
}
}