Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [math] Calculate velocity  (Read 305 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline matheus23

JGO Kernel


Medals: 106
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.

So your x-velocity becomes:
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
Offline 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 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".

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Roquen
« Reply #3 - Posted 2014-06-17 10:55:38 »

Always run simulations at a constant time step.
Offline 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
Online Roquen
« Reply #5 - Posted 2014-06-18 19:20:39 »

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

JGO Coder


Medals: 29
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. persecutioncomplex

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! Pointing
Offline matheus23

JGO Kernel


Medals: 106
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. persecutioncomplex
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
Online 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,
Your math here is exponentially off here.  Unless maybe you're talking about your state-of-the-art toaster.

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!