Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  Jump physics don't work right.  (Read 2740 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2009-04-27 20:11:19 »

I can't get my player to go in a arc while jumping it always turns into a ^ motion. Here's what I have for physics.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public void update(Map m, Enemy[] e1, int max_enemies, Item[] i1, int max_Items){
      //System.out.println("x: "+(int) (x/m.tileWidth));
      //System.out.println("y: "+(int) ((y/m.tileHeight) - 0.1));
      if(state != 2)
      {
         if(jumping){
            if(jumpdist <= jumpheight && m.tiles[(int) (x/m.tileWidth)][(int)  ((y/m.tileHeight))] == 0 &&
               jumpdist <= jumpheight && m.tiles[(int) (x/m.tileWidth)][(int)  ((y + height)/m.tileHeight)] == 0 &&
               jumpdist <= jumpheight && m.tiles[(int) ((x + width)/m.tileWidth)][(int)  ((y/m.tileHeight))] == 0 &&
               jumpdist <= jumpheight && m.tiles[(int) ((x + width)/m.tileWidth)][(int)  ((y + height)/m.tileHeight)] == 0 &&
               !colbottom(i1, max_Items)){
               velo += jumpspeed;
               jumpdist++;
            }
           
            else
               jumping = false;
               
         }
   
         if(m.tiles[(int) ((x/m.tileWidth) + 0.1)][(int) ((y/m.tileHeight) - 0.0005)] == 0 &&
            m.tiles[(int) ((((x + width)/m.tileWidth)) - 0.1)][(int) ((y/m.tileHeight) - 0.0005)] == 0 &&
               !coltop(i1, max_Items) && !jumping){
            falling = true;
            velo -= grav;
         }
         
         else falling = false;
         
         y = velo;
         
         if(collision(e1,max_enemies))
         {
            state = 2;
            frame = 0;
         }
      }
   }
Offline fireside

Senior Newbie





« Reply #1 - Posted 2009-04-27 20:22:44 »

When I did something like this, I started with a certain jump height and subtracted a little off it every game loop.  Eventually it became negative and the player dropped.  I was working with a y positive, of course.
Offline Darrin

Junior Duke


Projects: 1



« Reply #2 - Posted 2009-04-27 20:39:10 »

I'll take a stab at the pseudo code as I'm going to write something similar in my next game I think.

Basically you want to accelerate at a decreasing rate.  X axis does not matter. 

speedY = 12;   //pixels  speed or delta or acceleration whatever you want to call it.
VELOCITY_Y = 5;   //base movement speed

//jump loop
if (timer > 20)
--speedY;  //Every 20 miliseconds your speed decreases by 1. 

//set max fall speed
if (speedY < -12)
speedY = -12;

velocity = VELOCITY_Y + speedY;

// modify velocity by timelapse frame rate move.

setY(velocity)


Should be something like that.  You could get more complex I'm sure.




Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #3 - Posted 2009-04-28 01:49:56 »

When I did something like this, I started with a certain jump height and subtracted a little off it every game loop.  Eventually it became negative and the player dropped.  I was working with a y positive, of course.
This worked, thanks!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2009-04-28 04:15:31 »

When I did something like this, I started with a certain jump height and subtracted a little off it every game loop.  Eventually it became negative and the player dropped.  I was working with a y positive, of course.

In other words, you're giving the player a velocity and then adjusting it. Thinking of it using velocity is a lot nicer and more robust. If you've learned any sort of physics, you should understand that position is the derivative of velocity which is the derivative of acceleration, i.e. position is changed by velocity and velocity is changed by acceleration. Gravity constantly applies downward acceleration, when you jump you apply a very immediate and large upwards acceleration, which will be much greater than gravity initially, but because it is only applied once gravity will eventually overcome that boost.

ex.
while (gameIsRunning)
{
     //Initial jump.
     if (jumping && isOnGround)
     {
          velocity.y += 100;
     }

     //Gravity.
     velocity.y -= 9.8;

     //Adjust position by velocity.
     position += velocity;
}

See my work:
OTC Software
Offline fireside

Senior Newbie





« Reply #5 - Posted 2009-04-28 14:45:28 »

That's a good explanation of it.  I think there is value, also, in doing things intuitively.  When I was doing this, I didn't think about gravity or velocity or anything.  I only thought about the result I wanted and what would bring it.  Sometimes doing things that way can give out of the box solutions that can be  interesting.   
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2009-04-28 22:29:23 »

That's a good explanation of it.  I think there is value, also, in doing things intuitively.  When I was doing this, I didn't think about gravity or velocity or anything.  I only thought about the result I wanted and what would bring it.  Sometimes doing things that way can give out of the box solutions that can be  interesting.   
Well, I don't think I've ever done a project where I was able to get away with just using accurate physics, anyway, but it's logical to keep things constrained in a pos/vel/acc system because you can re-use it in every single game. Much more importantly, however, it's very very clear exactly how and why the character is moving a certain way. If you want him to hang in the air longer, you reduce gravitational acceleration. If you want him to jump higher, you give a higher jump velocity.

It's simple and compartmentalized. Intelligently encapsulated. Multi-use. Good things in an OOP environment.

See my work:
OTC Software
Offline fireside

Senior Newbie





« Reply #7 - Posted 2009-04-28 23:38:18 »

Yes, that's true.  It's kind of like path finding.  You can reuse it in a lot of programs.  I think a full blown physics system is overkill for many games, so something like that would be very useful.  I might work something like that out the next time I need it.  I'd like to have some base code, eventually, so I could put a game together in a lot less time.  Right now, I'm just getting used to things in Java.  A simple physics class would be nice to have.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #8 - Posted 2009-04-29 02:13:00 »

If you've learned any sort of physics, you should understand that position is the derivative of velocity which is the derivative of acceleration, i.e. position is changed by velocity and velocity is changed by acceleration.

Minor correction Smiley
acceleration is the derivative of velocity, which is the derivative of position, "i.e. position is changed by velocity and velocity is changed by acceleration"

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2009-04-29 03:14:27 »

Minor correction Smiley
acceleration is the derivative of velocity, which is the derivative of position, "i.e. position is changed by velocity and velocity is changed by acceleration"
Ha ha true. :-P

See my work:
OTC Software
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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!