Results 1 to 2 of 2
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default 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;
    	}
    
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default Re: Collisions Not Working in Platformer

    I fixed it! No need for any help, but thanks for looking!

Similar Threads

  1. Replies: 7
    Last Post: 12-03-2012, 09:31 PM
  2. Java platformer, rect collision
    By Lionlev in forum Advanced Java
    Replies: 8
    Last Post: 10-19-2012, 01:41 PM
  3. Collisions are working & not working
    By Jayayoh in forum Java 2D
    Replies: 1
    Last Post: 06-24-2011, 05:21 PM
  4. Solving Collisions in a car game
    By Rectal Exambot in forum Advanced Java
    Replies: 1
    Last Post: 09-29-2010, 02:13 PM
  5. Check The Collisions
    By Luff in forum AWT / Swing
    Replies: 7
    Last Post: 06-23-2010, 12:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •