Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
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  
  2D Platformer(answered)  (Read 1103 times)
0 Members and 1 Guest are viewing this topic.
Offline Zerolife

Senior Newbie





« Posted 2010-07-06 23:57:38 »

Im attempting to make a 2D platformer, and i really have no idea how to work out the physics and movement completely. I can get my player to move side to side with the arrow keys, and he stops fine, but when i start implementing physics in anyway into it, it doesnt work as well. Does any have a simple platformer code theyve seen/ have on hand? i've been looking for awhile, and i just keep finding overly complicated things.
Offline Jacob_

Junior Member


Projects: 3



« Reply #1 - Posted 2010-07-07 05:07:18 »

I don't know if there's a platform engine already, but it's not too hard to make. Since you said you were having problems with physics, here's some pseudocode (adapted from my game, may have bugs):

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  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
class Character
{
   int x;
   int y;
   int vx;
   int vy;

   boolean leftCollision = false;
   boolean rightCollision = false;
   boolean bottomCollision = false;
   boolean topCollision = false;

   void processPhysics()
   {
      if(vx > 0 && !rightKeyPressed)
         vx -= 0.025;
      else if(vx < 0 && !leftKeyPressed)
         vx += 0.025;
         
      //Gravity  
     if(!bottomCollision)
         vy -= 0.050;
         
      //Where we move if no collisions
     double maxX = x + vx;
      double maxY = y + vy;
     
      double distance = Math.sqrt(vx * vx + vy * vy);
                if(distance == 0) //Keep character from getting stuck, this gets it to reset the collision variables
                  distance = 0.005;
     
      double i = 0;
      double moveX = x;
      double moveY = y;
     
      while(i < distance)
      {
         double pos = i/distance;
         //Interpolate between the current and maximum position
        moveX = x + dx * pos;
         moveY = y + dy * pos;
         
         ///
        //check for collisions here, this depends on your game
        ///
       
         //Handle hits
        if(collisionLeft && vx < 0)
         {
            vx = 0;
            leftCollision = true;
            break;
         }
         else
            leftCollision = false;
         if(collisionRight && vx > 0)
         {
            vx = 0;
            rightCollision = true;
            break;
         }
         else
            rightCollision = false;
         if(collisionBottom && vy < 0)
         {
            vy = 0;
            bottomCollision = true;
            break;
         }
         else
            bottomCollision = false;
         if(topCollision && vy > 0)
         {
            vy = 0;
            topCollision = true;
            break;
         }
         else
            topCollision = false;
         i += 0.005;
      }
      //Move to the new position
     x = moveX;
      y = moveY;
   }
}


Basically you just use a velocity vector, then move the character along that vector until it hits something.

For your controls, just change the x/y velocity appropriately.
Offline Zerolife

Senior Newbie





« Reply #2 - Posted 2010-07-07 13:10:33 »

That surprizingly makes sense. Alright, Ill work with that and see what I can get. thanks so much!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zerolife

Senior Newbie





« Reply #3 - Posted 2010-07-08 01:28:53 »

i've looking through, and while a majority of it makes sense, there are some points i dont get because of undeclared variables that are referenced.

moveX = x + dx * pos;
moveY = y + dy * pos;

dx and dy were never initiallized, so im not sure what to do with that. i think you ment vx and vy, but i want to be sure.

if(collisionLeft && vx < 0)
{
     vx = 0;
     leftCollision = true;
     break;
}
else
     leftCollision = false;

collisionLeft was never initiallized. I think you wanted the variable leftCollision, but that wouldnt really make sense to have it be true to get into the if, and then set it true in the loop. The rest of the if statements are like that as well except the last one, which is the way i think this should be.
Offline Cero
« Reply #4 - Posted 2010-07-08 02:10:16 »

well, he assumed you would create 4 booleans or methods with boolean returns, called
collisionLeft, collisionRight, collisionTop and collisionBottom

somewhere, you obviously have check whether or not there was/is an collision somewhere

Offline Zerolife

Senior Newbie





« Reply #5 - Posted 2010-07-08 03:07:34 »

ya know, i thought that too at first, but that renders the whole leftCollision, rightCollision ect variables useless. I think i figured that much out. Because no where in the program it says to reset the collision variables, it just resets them inside the if statements. I have figured it out though. Thanks.
Offline Swattkidd7

Junior Member





« Reply #6 - Posted 2010-07-08 07:41:00 »

No, I think what he means is, you will have some method or something to check if there is collision and thats where those variables will be reset, set, etc.

For example

1  
2  
3  
4  
5  
6  
7  
public void checkCollision()
{
    if(isCollisionGoingLeft)
         collisionLeft = true;
    else
         collisionLeft = false;
}


Something like that. The variables are not useless because they are the flags that are set in your collision detection that tells the physics logic what to do. (i.e if(collisionLeft) do code).
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 (17 views)
2014-07-30 21:08:39

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

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

Dwinin (12 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!