Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  Quick and dirty platformer with Physics has errors with moving platforms  (Read 1131 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-11-24 03:50:03 »

Alright, so I'm trying to make a quick and dirty platformer engine, and I am having some problems with my collision detection and moving platforms. for one thing the "player" seems to bounce on top of moving platforms ever so slightly, and when he hits the right side errors happen as well. I'll upload a jnlp demo so you can try to find more errors and see whats happening, but here is the source:

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  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
import java.awt.Rectangle;
import java.util.Vector;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;


public class Platformer extends BasicGame{

   boolean keys[];
   int ALL_KEYS = 0xFF;
   Player player;
   Vector<Vector<Thing> > things;
   int level = 0;
   
   public Platformer() {
      super("You've met with a terrible fate, haven't you?");
   }
   
   public void init(GameContainer gc) throws SlickException {
      keys = new boolean[ALL_KEYS];
      for(int i = 0; i < ALL_KEYS; i++){
         keys[i] = false;
      }
     
      player = new Player();
      things = new Vector<Vector<Thing> >();
      Vector<Thing> temp = new Vector<Thing>();
      temp.add(new Thing(0, 440, 640, 40, 1));
      temp.add(new Thing(200, 300, 240, 50, 1));
      temp.add(new Thing(500, 200, 240, 50, 1));
     
      things.add(temp);  
   }
   
   public void update(GameContainer gc, int delta) throws SlickException{
      if(keys[Input.KEY_UP]){
         player.velo = player.maxJump;
         keys[Input.KEY_UP] = false;
      }
     
      if(keys[Input.KEY_DOWN]){
         keys[Input.KEY_DOWN] = false;
      }
     
      if(keys[Input.KEY_LEFT]){
         player.delta -= player.speed;
         
         if(player.delta < -player.maxSpeed)
            player.delta = -player.maxSpeed;
      }
     
      else if(keys[Input.KEY_RIGHT]){
         player.delta += player.speed;
         
         if(player.delta > player.maxSpeed)
            player.delta = player.maxSpeed;
      }
     
      else{
         if(player.delta < -0.5){
            player.delta += player.speed;
         }

         else if(player.delta > 0.5){
            player.delta -= player.speed;
         }
         
         else if(player.delta > -0.5 && player.delta < 0.5){
            player.delta = 0;  
         }
      }
     
      if(player.delta < 0)
         player.moveLeft(things.get(level));
     
      else if(player.delta > 0)
         player.moveRight(things.get(level));
     
      if(player.velo < 0)
         player.moveUp(things.get(level));
     
      else
         player.moveDown(things.get(level));
     
     
      things.get(level).get(1).moveRight(player, things.get(level));
     
   }
   
   public void render(GameContainer gc, Graphics g) throws SlickException{
      g.setColor(new Color(0,55,55));
      g.fillRect(0, 0, 640, 480);
     
      g.setColor(new Color(255,0,0));
      g.fillRect(player.x, player.y, player.width, player.height);
     
      for(int i = 0; i < things.get(level).size(); i++){
         if(things.get(level).get(i).type == 1)
            g.setColor(new Color(0,100,100));
         
         g.fillRect(things.get(level).get(i).x, things.get(level).get(i).y,things.get(level).get(i).width, things.get(level).get(i).height);
      }
   }
   
   public void keyPressed(int key, char c) {
      keys[key] = true;
   }
   
   public void keyReleased(int key, char c) {
      keys[key] = false;
   }
   
   public static void main(String[] args) throws SlickException{
       AppGameContainer app =
         new AppGameContainer( new Platformer() );
     
       app.setShowFPS(false);
       app.setAlwaysRender(true);
       app.setTargetFrameRate(60);
       app.setDisplayMode(640, 480, false);
       app.start();
   }

   
   class Player{
      float x = 50;
      float y = 50;
     
      float delta = 0; // x momentum
     float velo = 0;
      int height = 50;
      int width = 30;
      float speed = 0.2f;
      int maxSpeed = 6;
      int maxFallSpeed = 5;
      int maxJump = -8;
     
      public void moveLeft(Vector<Thing> things){
         x += delta;
         
         if(x < 0)
            x = 0;
         
         for(int i = 0; i < things.size(); i++){
            if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               x += (things.get(i).x + things.get(i).width) - x;
               delta = 0;
            }
         }
      }
     
      public void moveRight(Vector<Thing> things){
         x += delta;
         
         if(x + width > 640)
            x = (640 - width);
         
         for(int i = 0; i < things.size(); i++){
            if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               x -= (x + width) - things.get(i).x;
               delta = 0;
            }
         }
      }
     
      public void moveLeftWithThing(Vector<Thing> things, float thingSpeed){
         x -= thingSpeed;
         
         if(x < 0)
            x = 0;
         
         for(int i = 0; i < things.size(); i++){
            if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               x += (things.get(i).x + things.get(i).width) - x;
               delta = 0;
            }
         }
      }
     
      public void moveRightWithThing(Vector<Thing> things, float thingSpeed){
         x += thingSpeed;
         
         if(x + width > 640)
            x = (640 - width);
         
         for(int i = 0; i < things.size(); i++){
            if(new Rectangle((int) x, (int) y, width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               x -= (x + width) - things.get(i).x;
               delta = 0;
            }
         }
      }
     
      public void moveUp(Vector<Thing> things){
         y += velo;

         velo += speed;
         
         if(velo > maxFallSpeed)
            velo = maxFallSpeed;
         
         for(int i = 0; i < things.size(); i++){
            if(new Rectangle((int) x, (int) y, width, height/2).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               y += (things.get(i).y + things.get(i).height) - y;
               velo = 0;
            }
         }
      }
     
      public void moveDown(Vector<Thing> things){
         y += velo;

         velo += speed;
         
         if(velo > maxFallSpeed)
            velo = maxFallSpeed;
         
         
         boolean b = false;
         for(int i = 0; i < things.size(); i++){
            if(!b && new Rectangle((int) x, (int) y + (height/2), width, height).intersects(new Rectangle((int) things.get(i).x, (int) things.get(i).y, things.get(i).width, things.get(i).height))){
               y -= (y + height) - things.get(i).y;
               velo = 0;
            }
         }
      }
     
   }
   
   class Thing{
      float x = 50;
      float y = 50;
      int height = 50;
      int width = 30;
      int type = -1;
      float speed = 0.5f;
     
      public Thing(float x, float y, int width, int height, int type){
         this.x = x;
         this.y = y;
         this.width = width;
         this.height = height;
         this.type = type;
      }
     
      public void moveUp(Player player){
         y -= 0.5f;
         
         if(new Rectangle((int) x,(int) y, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
            player.y -= (player.y + player.height) - y;
            player.velo = 0;
         }
      }
     
      public void moveRight(Player player, Vector<Thing> things){
         x += speed;
         
         if(new Rectangle((int) x,(int) y - 1, width, height).intersects(new Rectangle((int) player.x, (int) player.y, player.width, player.height))){
            player.moveRightWithThing(things, speed);
         }
      }
   }
   
}


And here is the demo: http://prime.programming-designs.com/java/platformer_demo/platdemo.jnlp
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #1 - Posted 2010-11-24 16:54:00 »

1  
2  
3  
for(int i = 0; i < ALL_KEYS; i++){
   keys[i] = false;
}


Just wanted to point out that that loop is unnecessary. When you create a boolean-array, all values are already set to false Smiley

EDIT: I also figured out why you get pushed to the left side of the static, non-moving platform if you touch both the moving and the non-moving platform at the same time.
I believe it is because when you check for collissions, the game thinks you are colliding with the right side of the top platform even though you are colliding with the moving one.
I suggest you separate the collission and moving code. First move the objects and then check for collissions and re-position the player.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!