Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - 2D Platformer Collision Detection /w Gravity Physics
    primarykey
    data
    text
    <p>As this seems to be a recurring topic on 'the stack', I am going to reinforce my problem as something not covered. What has been covered is 2D tile collision for platform games etc., but with the way I have made my game, there are no tiles. I am also using no extra libraries, everything is written by my own hand.</p> <p>What I have is bounding Rect's for every object in the game. So far there are only two object classes in use, Platform and Entity. Entity contains all the stuff for player movement etc. while Platform is for a solid non-moving platform.</p> <p>Platform.java:</p> <pre><code>package com.toongames.game.objects; import android.graphics.Color; import android.graphics.Rect; import com.toongames.framework.Graphics; public class Platform { private int x, y; private int width, height; public Platform(int par1, int par2, int par3, int par4) { x = par1; y = par2; width = par3; height = par4; } public Rect getBounds() { return new Rect(x, y, x + width, y + height); } } </code></pre> <p>Entity.java:</p> <pre><code> package com.toongames.game.entity; import android.graphics.Color; import android.graphics.Point; import android.graphics.Rect; import com.toongames.framework.Graphics; import com.toongames.framework.Image; public class Entity { public final float GRAVITY = 0.1F; private String entityID; private Point pos; private int dx; private float vel; public Point desiredPos; public boolean onGround; public Entity(String par0String, int par1, int par2) { entityID = par0String; pos = new Point(par1, par2); desiredPos = pos; dx = 0; vel = 0; } public void update(float deltaTime) { vel = vel + (GRAVITY * deltaTime); pos.y += (vel * deltaTime); pos.x += dx; } public void setDx(int par1) { dx = par1; } public int getDx() { return dx; } public void setVelocity(int par1) { vel = par1; } public float getVelocity() { return vel; } public void setPos() { pos = desiredPos; } public Rect getBounds() { return new Rect(desiredPos.x, desiredPos.y, desiredPos.x + 80, desiredPos.y + 80); } } </code></pre> <p>I have successfully made the player collide with things both up and down, but I cannot for the life of me manage to make the player collide right and left. Whenever I collide with a platform while moving left or right, I just jump to the top of the platform I collided with.</p> <p>I know it has something to do with my logic, but I cannot figure out the correct logic to use.</p> <p>ScreenGame.java:</p> <pre><code> package com.toongames.game.screen; // Imports here... public class ScreenGame extends Screen { private Entity player; private Button left, right, jump; private Platform floor, p, p2, p3; private ArrayList&lt;Platform&gt; platforms; public ScreenGame(Game game) { super(game); player = new Entity("PLAYER", 300, 100, Assets.charRight); left = new Button(Assets.move_left, 10, 790 - Assets.move_left.getHeight(), Assets.move_left.getWidth(), Assets.move_left.getHeight()); right = new Button(Assets.move_right, 20 + Assets.move_left.getWidth(), 790 - Assets.move_right.getHeight(), Assets.move_right.getWidth(), Assets.move_right.getHeight()); jump = new Button(Assets.jump, 1270 - Assets.jump.getWidth(), 790 - Assets.jump.getHeight(), Assets.jump.getWidth(), Assets.jump.getHeight()); floor = new Platform(0, 790, 1280, 80); p = new Platform(1280 - 500, 500, 400, 80); p2 = new Platform(0, 200, 400, 80); p3 = new Platform(400, 120, 200, 80); platforms = new ArrayList&lt;Platform&gt;(); platforms.add(floor); platforms.add(p); platforms.add(p2); platforms.add(p3); } // An update method calls these public void updateMovement(float deltaTime) { List&lt;TouchEvent&gt; touchEvents = game.getInput().getTouchEvents(); int len = touchEvents.size(); for (int i = 0; i &lt; len; i++) { TouchEvent event = touchEvents.get(i); if (event.type == TouchEvent.TOUCH_DOWN) { if (inBounds(left.getBounds(), event)) { player.setDx((int) -(deltaTime * 1.5F)); } else if (inBounds(right.getBounds(), event)) { player.setDx((int) deltaTime * 2); } else if (inBounds(jump.getBounds(), event)) { if (player.onGround) { player.setVelocity(-8); } } } else if (event.type == TouchEvent.TOUCH_DRAGGED) { if (inBounds(left.getBounds(), event)) { player.setDx((int) -deltaTime * 2); } else if (inBounds(right.getBounds(), event)) { player.setDx((int) deltaTime * 2); } else if (inBounds(jump.getBounds(), event)) { if (player.onGround) { player.setVelocity(-8); } } else { player.setDx(0); player.jumpCounter = 0; } } else if (event.type == TouchEvent.TOUCH_UP) { player.setDx(0); player.jumpCounter = 0; } } } // An update method calls these public void updateGameObjects(float deltaTime) { for (Platform p : platforms) p.update(); player.update(deltaTime); } // An update method calls these public void checkCollisions() { Rect playerRect = player.getBounds(); for (Platform p : platforms) { Rect pRect = p.getBounds(); if (Rect.intersects(playerRect, pRect)) { Rect intersection = playerRect; intersection.intersect(pRect); if (player.getVelocity() != player.GRAVITY) { int resolutionHeight; if (player.getVelocity() &lt; player.GRAVITY) resolutionHeight = intersection.height(); else { resolutionHeight = -intersection.height(); player.onGround = true; } player.setVelocity(0); player.desiredPos = new Point(player.desiredPos.x, player.desiredPos.y + resolutionHeight); } } } player.setPos(); } } </code></pre> <p>As an extra note, I have cut out some of the unnecessary code to do with images for the entity and entity health etc.. Also I have cut out empty methods and stuff like that that have no relevance what so ever.</p> <p>[EDIT] Cut out most of the drawing code and imports. All the absolutely necessary stuff is there now.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload