Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [math] Calculate velocity  (Read 472 times) 0 Members and 1 Guest are viewing this topic.
Gibbo3771
 « Posted 2014-06-17 10:27:34 »

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:

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

I can get the distance needed to travel by doing:

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

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

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #1 - Posted 2014-06-17 10:44:31 »

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.

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

If you want the overall velocity, you need to apply the rule of pythagoras:
 `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`

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?

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Gibbo3771
 « Reply #2 - Posted 2014-06-17 10:53:35 »

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 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".

 1 `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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Roquen
 « Reply #3 - Posted 2014-06-17 10:55:38 »

Always run simulations at a constant time step.
Gibbo3771
 « Reply #4 - Posted 2014-06-17 10:56:30 »

Always run simulations at a constant time step.

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

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Roquen
 « Reply #5 - Posted 2014-06-18 19:20:39 »

Yes.  Lower complexity for computation and you're deterministic.
PandaMoniumHUN

JGO Coder

Medals: 32
Exp: 3 years

White-bearded OGL wizard

 « Reply #6 - Posted 2014-06-18 22:57:43 »

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.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down!
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #7 - Posted 2014-06-19 08:42:41 »

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.
Yes.  Lower complexity for computation and you're deterministic.

At no point he said "performance".

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Roquen
 « Reply #8 - Posted 2014-06-19 10:56:56 »

You seriously think that multiplying by delta time will affect the performance?
I wasn't really talking about that, but yes.  If you don't think so then you don't understand hardware.

Quote
It's 2014, you can do millions of multiplications in just a single frame,

Quote
doing one or two won't hurt performance at all.
So your state-of-the-art toaster is running a 2D game with one entity?  Again your math needs revision.

Quote
I don't see how NOT multiplying by delta time would be useful.
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).

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (12 views) 2015-05-23 20:26:16 MrMapcom (19 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (35 views) 2015-05-20 03:42:21 BurntPizza (70 views) 2015-05-10 15:53:18 FrozenShade (55 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (206 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x BurntPizza 16x Riven 13x ra4king 12x Husk 11x EgonOlsen 11x theagentd 11x DavidBVal 11x KevinWorkman 9x princec 8x scanevaro 8x opiop65 7x revers 6x KaiHH 6x SauronWatchesYou 6x MrPork 5x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org