Results 1 to 2 of 2
- 01-03-2014, 03:07 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 168
- Rep Power
- 9
Collisions Not Working in Platformer
Hi everyone,
I am making a platformer game in java, and I'm having a bit of a problem with collisions. So, my collisions work fine, but I am trying to make it so that the game only updates the collisions with blocks that are 3 away from the player. The blocks that are three away from the start are working, but once I move it does not seem to be updating the correct blocks, even though it should be.
Here's the Player.java file where the collisions happen.
Java Code:package com.patrickfeltes.entities; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; import com.patrickfeltes.GameState.GameState; import com.patrickfeltes.game.GamePanel; import com.patrickfeltes.objects.Block; import com.patrickfeltes.objects.Map; import com.patrickfeltes.physics.Collision; public class Player extends GameEntity { private boolean right = false, left = false, jumping = false, falling = false; private int movingSpeed = 2; private int fallingSpeed = 2; private double jumpingSpeed = 2.5; private double totalJump = 0; private int xCoor, yCoor; public Player() { setBounds(GamePanel.WIDTH / 2 - (Block.blockSize / 2), GamePanel.HEIGHT / 2 - Block.blockSize, Block.blockSize - 10, Block.blockSize - 10); } private boolean playerNotFalling = false; public void tick() { xCoor = (x + GameState.xOffset) / Block.blockSize; yCoor = (y + GameState.yOffset) / Block.blockSize; for(int i = xCoor - 3; i < xCoor + 3; i++) { for(int j = yCoor - 3; j < yCoor + 3; j++) { try { if(Map.blocks[i][j].getID() != -1) { System.out.println(Map.getBlocks()[i][j].x + "," + Map.getBlocks()[i][j].y); if(Collision.playerBlockColliding(Map.getBlocks()[i][j], new Point(x + width + GameState.xOffset, y + GameState.yOffset)) || Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + width + GameState.xOffset, y + height + GameState.yOffset - 1))) { right = false; } if(Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + GameState.xOffset - 1, y + GameState.yOffset)) || Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + GameState.xOffset - 1, y + height + GameState.yOffset - 1))) { left = false; } if(Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + GameState.xOffset + 1, y + GameState.yOffset)) || Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + width + GameState.xOffset - 1, y + GameState.yOffset))) { jumping = false; } if(Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + GameState.xOffset + 1, y + height + GameState.yOffset + 1)) || Collision.playerBlockColliding(Map.blocks[i][j], new Point(x + width + GameState.xOffset - 1, y + height + GameState.yOffset + 1))) { falling = false; playerNotFalling = true; } else { if(!jumping && !playerNotFalling) { falling = true; } } } }catch(Exception e) { System.out.println("Out Of Bounds!"); } } } playerNotFalling = false; if(left) { GameState.xOffset -= movingSpeed; } if(right) { GameState.xOffset += movingSpeed; } if(jumping) { GameState.yOffset -= (int)jumpingSpeed; jumpingSpeed *= .99; totalJump += jumpingSpeed; if(totalJump >= 80) { jumping = false; jumpingSpeed = 2.5; totalJump = 0; } } if(falling) { GameState.yOffset += movingSpeed; } } public void draw(Graphics g) { g.setColor(Color.BLACK); g.fillRect(x, y, width, height); } public void keyPressed(int k) { if(k == KeyEvent.VK_D) { right = true; } if(k == KeyEvent.VK_A) { left = true; } if(k == KeyEvent.VK_SPACE && !jumping && !falling) { jumping = true; } } public void keyReleased(int k) { if(k == KeyEvent.VK_D) { right = false; } if(k == KeyEvent.VK_A) { left = false; } } public void setPosition(int x, int y) { this.x = x; this.y = y; } public int getXCoor() { return xCoor; } public int getYCoor() { return yCoor; } }
- 01-03-2014, 05:51 PM #2
Senior Member
- Join Date
- Jan 2013
- Posts
- 168
- Rep Power
- 9
Similar Threads
-
Hmmm how do I make a camera follow the player? (Platformer game)
By Lionlev in forum Advanced JavaReplies: 7Last Post: 12-03-2012, 10:31 PM -
Java platformer, rect collision
By Lionlev in forum Advanced JavaReplies: 8Last Post: 10-19-2012, 02:41 PM -
Collisions are working & not working
By Jayayoh in forum Java 2DReplies: 1Last Post: 06-24-2011, 06:21 PM -
Solving Collisions in a car game
By Rectal Exambot in forum Advanced JavaReplies: 1Last Post: 09-29-2010, 03:13 PM -
Check The Collisions
By Luff in forum AWT / SwingReplies: 7Last Post: 06-23-2010, 01:30 PM
Bookmarks