[math] Calculate velocity

I honestly feel so stupid asking this, I know the usual formula which is v = d/t.

However I do not have or know how to calculate the time.

I have an object that moves towards the mouse cursor and uses this formula to smooth its way across the screen:

		getBounds().x += (pos.x - getBounds().x) * 0.05f;

I can get the distance needed to travel by doing:

d = Math.abs(getBounds().x - pos.x)

How would I get the speed of the object with this information?

For what do you want to use the velocity?

In your game you don’t have the usual units that we use in “every-day-physics”. That means instead of meters you have pixels as a measurement of length and instead of seconds you probably have simulation ticks (unless you use delta time, then you really have seconds or milliseconds). In the end you can convert pixels into real-world-meters by knowing how dense the display is and you can convert simulation ticks into real-world-seconds by knowing how fast your game loop is running.

If you want the velocity in pixels per simulation-tick you can simply take the ammount you move (‘d’ in your formula) and divide it by 1, since it’s the amount you move in one simulation-tick.

So your x-velocity becomes:
[icode]
xvelocity = (pos.x - getBounds().x) * 0.05f;
[/icode]

If you want the overall velocity, you need to apply the rule of pythagoras:
[icode]
xvelocity = (pos.x - getBounds().x) * 0.05f;
yvelocity = (pos.y - getBounds().y) * 0.05f;
velocity = Math.sqrt(xvelocity * xvelocity + yvelocity * yvelocity); // This will always be positive
[/icode]

If you want to convert that velocity into real-world-centimeters per real-world-seconds you need to first find out other things like pixel density and game loop speed.

Does this help? Again, for what do you want to use this velocity?

I need the velocity to calculate simple physics, its for a breakout clone, to be able to control the ball using paddle movement.

I am using LibGDX so I have access to delta time.

I am using a coordinate system, kind of. I have a camera that is set to 64x36 which you can probably translate to meters.

Your post does help, currently I stuck this in and it “kind of works”.

v = (Math.abs(getBounds().x - pos.x) * delta);

I do get a number between 0 and 0.8 depending how fast I swing the mouse about.

Always run simulations at a constant time step.

So for anything physics based never use delta time? Use a fixed time step?

Yes. Lower complexity for computation and you’re deterministic.

You seriously think that multiplying by delta time will affect the performance? It’s 2014, you can do millions of multiplications in just a single frame, doing one or two won’t hurt performance at all.
I don’t see how NOT multiplying by delta time would be useful. :persecutioncomplex:

At no point he said “performance”.

I wasn’t really talking about that, but yes. If you don’t think so then you don’t understand hardware.

Your math here is exponentially off here. Unless maybe you’re talking about your state-of-the-art toaster.

So your state-of-the-art toaster is running a 2D game with one entity? Again your math needs revision.

Okay we’re now out of the realm of basic arithmetic. The short answer is: you can’t do it right (where you is pretty much everybody). Fixed time steps for simulation and everybody can do it right.

I didn’t try to find any good sites, just grabbed a couple from a quick search:

http://lonesock.net/article/verlet.html
http://lolengine.net/blog/2011/12/14/understanding-motion-in-games

You do all kinds of fancy integration techniques which exponentially explode the cost of the simulation so you can end up being “closer” to deterministic. Waste of time (CPU and coding).