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
 Thrust and Gravity...  (Read 3110 times) 0 Members and 1 Guest are viewing this topic.
RageMatrix

Senior Newbie

Java games rock!

 « Posted 2005-02-25 07:41:51 »

Hello there,

First post at these 'ere forums, so hi everyone.

I'm trying to implement the following and have no real idea on how to do this, especially because of the lack of floating point under J2ME and also because my math sucks.

What I want to do is have a sprite at the bottom of the screen that when you press UP thrusts into the air up the Y axis up to a maximum acceleration. When letting go of the UP button, I'd like him to fall back down again until he either hits a platform or the "ground" which is (getHeight()).

Any pointers, hints, advice on how to achieve this? This is actually (from my point of view) the hardest part of my game.....after this, its plain sailing.

Thanks.
shmoove

Junior Devvie

Doh!

 « Reply #1 - Posted 2005-02-25 08:38:51 »

As far as no floats, used fixed number math:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `final int PRESICION = 16;// convert int(5) to fix point:int fixedFive = 5 << PRESICION;// convert fix point to int:int fiveAgain = fixedFive >> PRESICION;// 1final int FIXED_ONE = 1 << PRESICION;// addition and subtraction of fixed points:int fixedSix = fixedFive + FIXED_ONE;int fixedFour = fixedFive - FIXED_ONE;// for multiplication you need to downshift the result (watch for overflow too)int fixedTwenty = (fixedFive * fixedFour ) >> PRESICION;// the shift accounts for moving the point. It's kinda like//   5.0// * 4.0// -----// 20.00 (only hear presicion = 10 instead of 2^16)// for division you have to upshiftint fixedFourAgain = (fixedTwenty / fixedFive) << PRESICION;`

Now you can do the acceleration using the equations you learn in physics:
 1  2 `y(t) = a*t^2 + v0*t + y0v(t) = a*t + v0`

or use integration:
 1  2  3 `int dt = timePassedSinceLastFrame();v += a * dt;y += v * dt;`

shmoove
davidaprice

Junior Devvie

 « Reply #2 - Posted 2005-02-25 12:08:56 »

Or if you just want to keep it really simple, just have the sprite's y-velocity be a certain number of pixels-per-frame, e.g. -1 goes up slowly, or 1 goes down slowly (since MIDP's Graphics object has its origin at top left).

Then your acceleration is in (pixels-per-frame)-per-frame. The code for each frame 'tick' is now really simple:

 1  2  3 `if (keyPressed) vy -= KEY_PRESS_ACCELERATION;vy += GRAVITY;y += vy;`

For instance, GRAVITY could be 1, and KEY_PRESS_ACCELERATION could be 2 (1 to counter gravity plus 1 to accelerate the other way).

Instead of 'y += vy', you might want:
 1  2  3  4  5  6 `int abs = Math.abs(vy);int sgn = (vy > 0 ) ? 1 : ((vy < 0) ? -1 : 0)for (int i = 0; i < abs; ++i) {    y += sgn;    // detect collisions}`

...so that your sprite doesn't jump through objects.
RageMatrix

Senior Newbie

Java games rock!

 « Reply #3 - Posted 2005-02-26 11:56:54 »

Thanks
wooD

Senior Newbie

J2ME Developer

 « Reply #4 - Posted 2005-02-28 16:33:37 »

For the fixed floating point, I've used ShiftFP2D in the past and been happy with the results. It helps manage the data and makes the code a little easier, but as you can see above, it's still pretty simple to do by hand.

I'm not sure of the status on ShiftFP2D as the author said he would post it on sourceforge/java.net last year. here is a link to a hopefully working version.

Wood

RageMatrix

Senior Newbie

Java games rock!

 « Reply #5 - Posted 2005-03-01 06:17:21 »

Thanks. At the moment, I'm trying to get MathFP to work with EclipseME, but had no luck. Is this FP lib easier to work with?

Junior Devvie

m3g is the truth

 « Reply #6 - Posted 2005-03-01 12:22:05 »

I would suggest to use Float & FLoat11 class from:

http://henson.newmail.ru/j2me/Float11.htm
http://henson.newmail.ru/j2me/Float.htm

Everything is there. Small, simple, fast, easy to use.

Franck

Mobile Entertainment Studio: M-E-S
http://sourceforge.net/projects/cazapool3d/
Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #7 - Posted 2005-03-01 12:45:57 »

Quote
fast

it is anything *but* fast =|

Simulating floating point maths is complete overkill for nearly all games.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
RageMatrix

Senior Newbie

Java games rock!

 « Reply #8 - Posted 2005-03-01 12:53:37 »

Okay. Could you please explain how I can get my little spaceman to move more slowly around the screen whilst using a speed that is less than 1 pixel per cycle?

If I can avoid FP, I certainly will.

Junior Devvie

m3g is the truth

 « Reply #9 - Posted 2005-03-01 13:34:26 »

use counter,  and limit counter patter, so you update your stuff every 'limit counter' is reach
but this is not good at all if you use integer counter, because you'll have different behavior with different CPU cell phone...

so the best is to use System.currentTimeMillis() (as a counter)

Franck

Mobile Entertainment Studio: M-E-S
http://sourceforge.net/projects/cazapool3d/
wooD

Senior Newbie

J2ME Developer

 « Reply #10 - Posted 2005-03-01 16:29:22 »

Quote
Okay. Could you please explain how I can get my little spaceman to move more slowly around the screen whilst using a speed that is less than 1 pixel per cycle?

Using the fixed floating point math (two methods listed above), you are really only using the upper 16 bits for your current position. Shifting the number down before using it as the current position. The lower 16 bits are the fractional component. So your thrust would be less than 32000 and you add it to your position everytime through your loop. If the thrust is around 8000, your position would move by 1 pixel every four ticks.

You should also multiply your thrust by the number of milliseconds since the last tick before adding it to the position. This will allow you to base your movement on pixels per millisecond and the game will have similar performance across different platforms. Your thrust values will have to be adjusted down accordingly, but this is a very important aspect in order to make the game work better across different speed phones.

Wood

Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #11 - Posted 2005-03-01 16:59:01 »

Quote

Using the fixed floating point math

Its either fixed point, or floating point, "fixed floating point" is a contradiction.

Basing your animation loop on time deltas has a number of problems.

1) Many phones (in particular all Samsung phones) do not handle call interupts correctly.
Consequently, there is often no way for an app. to know a call interupt has occured.
If the game relies on time deltas, a huge time delta (the length of the phone call) can accumulate between concurrent frames.
There are hacky work-arounds (check for time deltas in excess of some constant value), however they often have unwanted side effects.

2) The precision of System.currentTimeMillis() is not specified in cldc - therefor you cannot rely on its accuracy being 1ms.
It so happens that nearly all phones I have come across *do* report timings to 1ms accuracy, however im quite sure there will be exceptions. (im still waiting to find a phone with a win98 accuracy of 50~60ms )

3) time based animation is NOT suitable for 2D animation.

Due to the way the human brain percieves motion, time based animation loops are best suited to 3D animation.
In this situation the brain interpolates discrete positions to give a smoother perception of motion.

In 2D the brain does not do this, so sustained, fixed rate, smooth animation is preferable.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
RageMatrix

Senior Newbie

Java games rock!

 « Reply #12 - Posted 2005-03-01 18:05:17 »

>sustained, fixed rate, smooth animation is preferable.

Sorry....you've lost me slightly. What are you saying is the best way to handle this?
Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #13 - Posted 2005-03-01 18:37:49 »

Much knowledge, this forum has!

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1040163227;start=

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
RageMatrix

Senior Newbie

Java games rock!

 « Reply #14 - Posted 2005-03-01 19:10:09 »

Thanks
Pages: [1]
 ignore  |  Print

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

 MrMapcom (16 views) 2015-05-23 20:26:16 MrMapcom (22 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 (56 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 (207 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x KevinWorkman 9x Drenius 9x princec 8x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x
 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