Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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  
  Academic Java Knowledge, Problems with Movement in Games  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
Offline boom_box

Junior Newbie





« Posted 2014-03-10 01:01:08 »

I took a year of Java in college, but it was mostly academic/business-oriented coding. I am trying to make my first game, and I am at the point of having 2D graphics working in Swing and a character I can move back and forth, but I am having problems with 'jumping'. Intending to make a 2D platformer, it is integral that I figure out vertical movement. As it stands I am using a KeyListener on my canvas to move the Entity object I have created, but my control scheme seems to be very primitive. I can make the Entity move left or right or jump, but it is unable to 'jump' and move a direction at the same time. I am using a switch statement to control the movement as such:

public void movement(KeyEvent e) {
      
      switch(e.getKeyCode()) {
      
      case KeyEvent.VK_RIGHT:
         
         if(myX >= 940) {
         
            break;
            
         }
         
         myX += 10;
         break;
         
      case KeyEvent.VK_LEFT:
         
         if(myX <= 10) {
            
            break;
            
         }
         
         myX -= 10;
         break;
      
      case KeyEvent.VK_SPACE:
         
                       while(myY < 400) {
            
            myY += 20;

         }

             while(myY > 300) {

                                myY -= 20;

                       }

         break;
         
      }
      
   }


What is the standard method of creating vertical/arcing movement of an Entity in 2D space? I apologize if this information is posted elsewhere, I couldn't find it.
Thanks in advance,

boom_box
Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2014-03-10 01:44:19 »

The easiest way to get an arc is to accelerate the player downwards by a constant factor when the player is not standing on a blocking tile (e.g. ground)

To perform a jump, you would then add an initial impulse upwards and allow the downwards acceleration to erode the effect of this impulse. Eventually the impulse is countered entirely... this would be the top of the arc. Then the downwards acceleration will dominate and start to move the player back down.

i.e.

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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
double myX,myY;
double accX,accY;

double ACCELERATION_INCREMENT=0.1;
double GRAVITY_ACCELERATION=0.1;
double MAX_ACCELERATION_X=5;
double JUMP_ACCELERATION=10;


while (gameIsAlive)
{
      myX+=accX;
      myY+=accY;

         if (!player.isOnGround)
         {
             accY-=GRAVITY_ACCELERATION;
         }

      // draw player
     
      // sleep
}

public void movement(KeyEvent e) {
     
      switch(e.getKeyCode()) {
     
      case KeyEvent.VK_RIGHT:
         accX+=ACCELERATION_INCREMENT;
         if(accX >= MAX_ACCELERATION_X) {
         accX=MAX_ACCELERATION_X;
           
         }
         
      case KeyEvent.VK_LEFT:
           accX-=ACCELERATION_INCREMENT;
         if(accX <= -MAX_ACCELERATION_X) {
         accX=-MAX_ACCELERATION_X;
           
         }
     
      case KeyEvent.VK_SPACE:
         if (player.isOnGround)
         {
             accY+=JUMP_ACCELERATION;
         }
         
         
      }
     
   }

Java4k RIP 2014
Offline boom_box

Junior Newbie





« Reply #2 - Posted 2014-03-10 03:11:46 »

Thank you.
Pages: [1]
  ignore  |  Print  
 
 

 
ivj94 (586 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (383 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (110 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (704 views)
2018-02-28 16:59:18

buddyBro (93 views)
2018-02-28 16:45:17

xxMrPHDxx (494 views)
2017-12-31 17:17:51

xxMrPHDxx (734 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!