Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) 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 1881 times) 0 Members and 1 Guest are viewing this topic.
Gibbo3771

JGO Kernel

Medals: 128
Projects: 5
Exp: 1 year

Currently inactive on forums :(

 « 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: 138
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

JGO Kernel

Medals: 128
Projects: 5
Exp: 1 year

Currently inactive on forums :(

 « 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

JGO Kernel

Medals: 517

 « Reply #3 - Posted 2014-06-17 10:55:38 »

Always run simulations at a constant time step.
Gibbo3771

JGO Kernel

Medals: 128
Projects: 5
Exp: 1 year

Currently inactive on forums :(

 « 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

JGO Kernel

Medals: 517

 « 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: 138
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

JGO Kernel

Medals: 517

 « 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

 nelsongames (20 views) 2018-04-24 18:15:36 nelsongames (17 views) 2018-04-24 18:14:32 ivj94 (608 views) 2018-03-24 14:47:39 ivj94 (54 views) 2018-03-24 14:46:31 ivj94 (402 views) 2018-03-24 14:43:53 Solater (66 views) 2018-03-17 05:04:08 nelsongames (111 views) 2018-03-05 17:56:34 Gornova (177 views) 2018-03-02 22:15:33 buddyBro (747 views) 2018-02-28 16:59:18 buddyBro (94 views) 2018-02-28 16:45:17
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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