Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Thrust and Gravity...  (Read 2698 times)
0 Members and 1 Guest are viewing this topic.
Offline RageMatrix

Senior Newbie




Java games rock!


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

Hello there,

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

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

Thanks.
Offline shmoove

Junior Member




Doh!


« Reply #1 - Posted 2005-02-25 09: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;

// 1
final 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 upshift
int 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 + y0
v(t) = a*t + v0

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


shmoove
Offline davidaprice

Junior Member





« Reply #2 - Posted 2005-02-25 13: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RageMatrix

Senior Newbie




Java games rock!


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

Thanks Smiley
Offline wooD

Senior Newbie




J2ME Developer


« Reply #4 - Posted 2005-02-28 17: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.

http://209.145.252.10/other/newsletter/newslett132.txt

Wood

Offline RageMatrix

Senior Newbie




Java games rock!


« Reply #5 - Posted 2005-03-01 07: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?
Offline calzada

Junior Member




m3g is the truth


« Reply #6 - Posted 2005-03-01 13: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/
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #7 - Posted 2005-03-01 13: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
Offline RageMatrix

Senior Newbie




Java games rock!


« Reply #8 - Posted 2005-03-01 13: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.
Offline calzada

Junior Member




m3g is the truth


« Reply #9 - Posted 2005-03-01 14: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/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wooD

Senior Newbie




J2ME Developer


« Reply #10 - Posted 2005-03-01 17: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

Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #11 - Posted 2005-03-01 17: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 Cheesy)

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
Offline RageMatrix

Senior Newbie




Java games rock!


« Reply #12 - Posted 2005-03-01 19: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?
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #13 - Posted 2005-03-01 19: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
Offline RageMatrix

Senior Newbie




Java games rock!


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

Thanks Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!