Results 1 to 2 of 2
  1. #1
    buddyboy3121 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default Java collision! Help me please :( game programming coding for days trying to fix this

    I am creating a game with slick2D and this is my first time creating a game that involves using tiles instead of pixels (It uses both), and I am having some trouble with collision when I hit a rock (Block that should stop the player when the player hits it) the player stops but when I go back in the opposite direction the player can't move. Here is the for main classes that involve the collision stuff or anything that has coordinates.

    Here you go :yeye:

    [spoiler]
    Java Code:
    package MainGame;
    
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;
    import org.peter.generator.*;
    
    public class Playing extends BasicGameState {
    	
    	private Level level;
    	private MouseHandler mouse;
    	private FastInv basicInv;
    	private Player player;
    	
    	//Coordinates
    	public static float offsetX = (Tile.SIZE * Tile.BLOCK) / 2;
    	public static float offsetY = (Tile.SIZE * Tile.BLOCK) / 2;
    	public final float startingX = offsetX;
    	public final float startingY = offsetY;
    	public static float shiftX = Game.screenWidth / 2;
    	public static float shiftY = Game.screenHeight / 2;
    	public float imageX = 0;
    	public float imageY = 0;
    	public static int mouseX = (int) offsetX;
    	public static int mouseY = (int) offsetY;
    	public static int blockX = 0;
    	public static int blockY = 0;
    	
    	//Init
    	public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
    		level = new Level();
    		mouse = new MouseHandler();
    		basicInv = new FastInv();
    		player = new Player();
    		
    		basicInv.init();
    		level.generate();
    		player.init();
    	}
    	
    	//Render
    	public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
    		level.render(g);
    		
    		
    		g.setColor(Color.white);
    		
    		g.drawString(String.format("offsetX: %f\noffsetY: %f\nshiftX: %f\nshiftY: %f\nimageX: %f\nimageY: %f\nmouseX: %d\nmouseY: %d\n" +
    				"blockX: %d\nblockY: %d\n testX: %f\ntestY: %f", offsetX,
    				offsetY, shiftX, shiftY, imageX, imageY, mouseX, mouseY, blockX, blockY, (Playing.offsetX + Playing.shiftX) / 32, (Playing.offsetY + Playing.shiftY) / 32), Game.screenWidth - 200, 15);
    		basicInv.render(g);
    		
    		player.render(g);
    		g.drawRect((float) shiftX , (float) shiftY, 32, 32);
    
    	}
    	
    	//Update
    	public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
    	    level.updateLevel();
    	    player.update(container, delta);
            mouseX = mouse.getMouseX();
            mouseY = mouse.getMouseY();
            blockX = mouse.getBlockX();
            blockY = mouse.getBlockY();
            basicInv.update();
            
            
    	}
    	
    	
    	public int getID() {
    		return 1;
    	}
    }
    Java Code:
    package MainGame;
    
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.geom.Rectangle;
    
    public class AddCollision {
    	
    	public Rectangle object = new Rectangle(0,0,0,0);
    	
    	private float x, y;
    	private float oX, oY;
    	
    	public AddCollision(int x, int y, int width, int height) {
    		this.x = x;
    		this.y = y;
    		object.setBounds(x, y, 32, 32);
    		oX = x;
    		oY = y;
    		
    	}
    	
    	public float getX() {
    		return (float) object.getX();
    	}
    	
    	public float getY() {
    		return (float) object.getY();
    	}
    	
    	public void render(Graphics g) {
    		
    		
    	}
    	
    	//Checks collision
    	public boolean hit(Rectangle playerBounds) {
    		if (playerBounds.intersects(object)) {
    			return true;
    		}
    		
    		return false;
    	}
    	
    }
    Java Code:
    package MainGame;
    
    import org.newdawn.slick.Animation;
    import org.newdawn.slick.GameContainer;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.Image;
    import org.newdawn.slick.Input;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.geom.Rectangle;
    import org.peter.generator.Level;
    import org.peter.generator.Tile;
    
    public class Player {
    	
    	public static Rectangle playerBounds;
    	private static Animation player, up, down, left, right;
    	private final float dx = 0.1f;
    	private final float dy = 0.1f;
    
    	private float playerX;
    	private float playerY;
    	
    	public void init() throws SlickException {
    		Image playerLeft[] = { new Image("images/player/left_1.png"), new Image("images/player/left_3.png") };
    		Image playerRight[] = { new Image("images/player/right_1.png"), new Image("images/player/right_3.png")};
    		Image playerUp[] = { new Image("images/player/up_1.png"), new Image("images/player/up_3.png")};
    		Image playerDown[] = { new Image("images/player/down_1.png"), new Image("images/player/down_3.png")};
    		
    		left = new Animation(playerLeft, 250, false);
    		right = new Animation(playerRight, 250, false);
    		up = new Animation(playerUp, 250, false);
    		down = new Animation(playerDown, 250, false);
    		
    		player = down;
    		
    		playerBounds = new Rectangle(0, 0, 0, 0);
    	}
    	
    	public void render(Graphics g) {
    		player.draw(Playing.shiftX, Playing.shiftY);
    		
    	}
    	
    	public void update(GameContainer container, int delta) {
    		  Input input = container.getInput();
    
    		  //Sets the X and Y pixels of where the player is.
    		  playerX = (Playing.offsetX + Playing.shiftX);
    		  playerY = (Playing.offsetY + Playing.shiftY);
    		  
    		  playerBounds.setBounds(playerX, playerY, 32, 32);
    		  
    		        //down
    				if (input.isKeyDown(Input.KEY_S) || input.isKeyDown(Input.KEY_DOWN)) {
    					Playing.offsetY += dy * delta;
    					player = down;
    					player.update(delta);
    					
    					for(AddCollision c: Level.collision) {
    						if (c.hit(playerBounds)) {
    							
    							Playing.offsetY -= dy * delta;
    						}
    
    					}
    					
    					if (Playing.offsetY > Tile.SIZE * Tile.BLOCK - Game.screenHeight - Tile.BLOCK) {
    						Playing.offsetY = Tile.SIZE * Tile.BLOCK - Game.screenHeight - Tile.BLOCK;
    					}
    					
    				}
    				
    				//up
    		        if (input.isKeyDown(Input.KEY_W) || input.isKeyDown(Input.KEY_UP)) {
    		        	Playing.offsetY -= dy * delta;
    					player = up;
    					player.update(delta);
    					
    					for(AddCollision c: Level.collision) {
    						if (c.hit(playerBounds)) {
    							
    							Playing.offsetY += dy * delta;
    						}
    
    					}
    					
    		            if (Playing.offsetY < 0 + Tile.BLOCK) {
    		            	Playing.offsetY = 0 + Tile.BLOCK;
    					}
    				}
    		        
    		        //right
    		        if (input.isKeyDown(Input.KEY_D) || input.isKeyDown(Input.KEY_RIGHT)) {
    		        	Playing.offsetX += dx * delta;
    		        	player = right;
    		        	player.update(delta);
    		        	
    		        	for(AddCollision c: Level.collision) {
    						if (c.hit(playerBounds)) {
    							
    							Playing.offsetX -= dx * delta;
    						}
    
    					}
    		        	
    		        	if (Playing.offsetX > Tile.SIZE * Tile.BLOCK - Game.screenWidth - Tile.BLOCK) {
    		        		Playing.offsetX = Tile.SIZE * Tile.BLOCK - Game.screenWidth - Tile.BLOCK;
    					}
    		      	}
    		        
    		        //left
    		        if (input.isKeyDown(Input.KEY_A) || input.isKeyDown(Input.KEY_LEFT)) {
    		        	Playing.offsetX -= dx * delta;
    		        	player = left;
    		        	player.update(delta);
    		        	
    		        	for(AddCollision c: Level.collision) {
    						if (c.hit(playerBounds)) {
    							
    							Playing.offsetX += dx * delta;
    						}
    
    					}
    		        	
    		        	if (Playing.offsetX < Tile.BLOCK) {
    		        		Playing.offsetX = Tile.BLOCK;
    					}
    		      	}
    		        
    		  
    	}
    	
    	public Rectangle getPlayer() {
    		return playerBounds;
    	}
    
    
    }
    Java Code:
    package org.peter.generator;
    
    
    import org.newdawn.slick.Color;
    import org.newdawn.slick.Graphics;
    import org.newdawn.slick.Image;
    import org.newdawn.slick.SlickException;
    import org.newdawn.slick.geom.Rectangle;
    
    import MainGame.MouseHandler;
    import MainGame.Playing;
    
    public class Tile {
    
    
    	public static final int SIZE = 75;
    	public static final int BLOCK = 32;
    
    	public float x;
    	public float y;
    	public float oX;
    	public float oY;
    	public int id = 0;
    
    	private Image grass;
    	private Image plant;
    	private Image rock;
    	private Image hoed;
    	private Image hoed_watered;
    
    	public Rectangle tileArea;
    	private MouseHandler mouse;
    	boolean showBorders = false;
    
    	public Tile(float x, float y, int id) {
    		this.oX = x;
    		this.oY = y;
    		this.id = id;
    
    		try {
    			grass = new Image("images/grass.png");
    			plant = new Image("images/plant.png");
    			rock = new Image("images/rock.png");
    			hoed = new Image("images/hoed.png");
    			hoed_watered = new Image("images/hoed-watered.png");
    			mouse = new MouseHandler();
    			tileArea = new Rectangle(x, y, BLOCK, BLOCK);
    			
    		} catch (SlickException e) {
    			e.printStackTrace();
    		}
    	}
    
    	//sets location
    	public void setLocation(float x, float y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	public void update() {
    		x = oX - Playing.offsetX;
    		y = oY - Playing.offsetY;
    
    		tileArea.setBounds((int) x, (int) y, BLOCK, BLOCK);
    		if(tileArea.contains(mouse.getMouseX() - Playing.offsetX, mouse.getMouseY() - Playing.offsetY)){
    			showBorders = true;
    		}else{
    			showBorders = false;
    		}
    	}
    
    	public void render(Graphics g) {
    		g.drawImage(getTile(id), x, y);
    		
    		if(showBorders){
    			g.setColor(Color.white);
    			g.drawRect(x, y, BLOCK - 1, BLOCK - 1);
    		}
    		
    	}
    	
    
    	public Image getTile(int ID) {
    		switch (ID) {
    		case 0:
    			return grass;
    		case 1:
    			return plant;
    		case 2:
    			return rock;
    		case 3:
    			return hoed;
    		case 4:
    			return hoed_watered;
    		}
    
    		return null;
    	}
    	
    
    }
    Java Code:
    package org.peter.generator;
    
    import java.util.ArrayList;
    
    import org.newdawn.slick.Graphics;
    
    import MainGame.AddCollision;
    import MainGame.Game;
    import MainGame.MouseHandler;
    import MainGame.Playing;
    
    public class Level {
    
    	public static Tile level[][] = new Tile[Tile.SIZE][Tile.SIZE];
    	public static ArrayList<AddCollision> collision = new ArrayList<AddCollision>();
    
    	//Make level.
    	public void generate() {
    
    		for (int x = 0; x < Tile.SIZE; x++) {
    
    			for (int y = 0; y < Tile.SIZE; y++) {
    				int random = (int) (Math.random() * 30);
    				int tileId = 0;
    
    				
    				if (random > 20 && random < 27) {
    
    					tileId = 1;
    				} else if (random > 27) {
    					tileId = 2;
    					
    					collision.add(new AddCollision(x * 32, y * 32, 32, 32));
    				}
    
    				level[x][y] = new Tile(x * Tile.BLOCK, y * Tile.BLOCK, tileId);
    				
    
    			}
    		}
    
    	}
    	
    	
    	//Get tile
    	public static Tile getTile(float playerX, float playerY) {
    		return level[(int) playerX][(int) playerY];
    	}
    	
    	//Replace tile
    	public boolean replaceTile(int x, int y, int remove, int replace) {
    		if (remove == level[x][y].id) {
    			level[x][y] = new Tile(x * 32, y * 32, replace);
    			return true;
    		}
    		
    		return false;
    	}
    	
    	//Remove tile
    	public boolean removeTile(int x, int y, int ID) {
    		if (ID == level[x][y].id) {
    			level[x][y] = new Tile(x * 32, y * 32, 0);
    			return true;
    		}
    		return false;
    		
    	}
    	
    	
    	//Updates level
    	public void updateLevel() {
    		   for (int x = 0; x < Tile.SIZE; x++) {
    
    				for (int y = 0; y < Tile.SIZE; y++) {
    					level[x][y].update();
    				}
    			}
    	}
    
    	//Renders tiles
    	public void render(Graphics g) {
    		for (int x = (int) (Playing.offsetX / Tile.BLOCK) - 1; x < (Game.screenWidth / Tile.BLOCK) + Playing.offsetX / Tile.BLOCK + 1; x++) {
    			
    			for (int y = (int) (Playing.offsetY / Tile.BLOCK) - 1; y < (Game.screenHeight / Tile.BLOCK) + Playing.offsetY / Tile.BLOCK  + 1; y++) {
    				
    				level[x][y].render(g);
    			}
    		}
    		
    		for (AddCollision c: collision) {
    			c.render(g);
    		}
    		
    	}
    
    }
    [/spoiler]

    Here you go :)
    Please let me know how I can fix this problem. I having been working on this more then 5 hours and it is starting to make me a bit sad :(

    Let me know if you need more code or you want the whole project! Thank you guys!!!!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Java collision! Help me please :( game programming coding for days trying to fix

    Print out the new position values for the player on a collision, both before and after you change them.
    In fact, you seem to be setting the player position (not so sure I've read that correctly, though) then checking, then resetting them.

    In addition, on a collision, note the values for all the stuff that determined a collision...the AddCollision used, and the playerBounds values.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java game collision detection need help!
    By NOOB-PROGRAMMER in forum New To Java
    Replies: 2
    Last Post: 01-18-2013, 05:16 PM
  2. android programming vs game programming using java
    By vgoel38 in forum Forum Lobby
    Replies: 2
    Last Post: 09-08-2012, 01:11 AM
  3. Help: Eclipse java programming(coding)
    By weikang in forum New To Java
    Replies: 3
    Last Post: 12-25-2010, 01:38 AM
  4. Game Help.working on it for days,.
    By xSkittlesx in forum Java Applets
    Replies: 12
    Last Post: 05-31-2010, 04:24 PM

Tags for this Thread

Posting Permissions

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