Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  [2D Jumping Sprite Animation [FIXED]]  (Read 2921 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2012-01-11 20:43:30 »

So as the title says i'm triyng to create a simple jumping animation for my 2d RPG, instead of just having left,right,up,down movements i'd like left, right, and jump.

At the moment the only variables to tell/change a players position are:
public int absX;
public int absY;

Now I know i'm going to need some other variables + forumlas, such as Dx, Dy, but if someone could point me in the direction on how to go on this, it would be much appreciated.
At the moment i just made a simple method called jump(), which (absY--) = (Up actually), 15 times, and when that characters height has raised 15 times, than i would lower it back to -15, (flat at the floor), so it looks like a animation.
But I really don't like the way that that looks O_o.
If someone could help me please! Shocked

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-11 22:43:55 »

Well the variables that are needed are gravity and yVelocity.
Gravity is a constant which you should tweak to your heart's content to get it right. Let's make gravity 500 pixels per second.
The Y velocity will be 0 when the player is not jumping, but as soon as you hit the spacebar, you set it to a desired vertically moving speed. Let's go with -300 pixels per second as the initial velocity.

Now every tick, you will add the yVelocity multiplied with the delta to the Y variable. Then you will add the gravity multiplied with the delta to the yVelocity. This is applying gravity your velocity and then applying the velocity to your position.

BTW: You should change absX and absY to doubles.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private final double gravity = 500;

public void update(long deltaTime) {
    double delta = deltaTime / 1000.0;
   
    if(spaceBarIsPressed)
        yVelocity = -300; //Going up 300 pixels per second.
   
    absY += yVelocity * delta;
    yVelocity += gravity * delta;
}

Offline GabrielBailey74
« Reply #2 - Posted 2012-01-11 22:56:46 »

Ah, I figured it out, perfectly.
Thanks m8, thread closed.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2012-01-22 00:16:02 »

Thanks about the BTW on turning my ints to doubles for player positioning, just now looking into deeper detail on a Double, thanks m8.

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-01-22 00:18:20 »

Glad to help 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.

Gibbo3771 (12 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!