Jump to content

  • Log In with Google      Sign In   
  • Create Account

Tankyroo

Member Since 05 Jul 2010
Offline Last Active Aug 17 2012 11:43 AM

Topics I've Started

2D Collision detection, need some help.

15 August 2012 - 09:47 PM

Hey, I'm here to ask for help on a topic I'm sure a lot of people get stuck on, and that is collision detection. I'll describe what I'm having trouble with and all of the specifics below.

I'm learning Java, and after making a couple simple games I decided it would be a good learning experience to make a platformer, but I'm having a lot of trouble so far.

I'm using Java with the Slick2D library.

So far I have a character (rectangle) that you are able to control with a very basic movement system (you can move left, right, and jump.) and now I'm going into what I can't put off any longer, collision detection. I've tried a few different methods that I came up with myself, and nothing is working, I've also tried looking for some tutorials on google, but I can't find what I'm looking for.

I'm not trying to do anything fancy, I just want to be able to collide with and jump on another rectangle to create the feel of a solid block.

What is the best way of doing this? I can't seem to get the player to stop at the right place, especially while jumping onto the block.

Any and all help is appreciated, thanks for reading!

Code below:

Main class:
public class Main extends BasicGame{
Player p = new Player();
Cube c = new Cube();

public Main(String title) {
  super(title);
}
public static void main(String[] args) throws SlickException{
  AppGameContainer app = new AppGameContainer(new Main("Game"));
  app.setTargetFrameRate(60);
  app.setDisplayMode(640, 480, false);
  app.start();
}

@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
  p.paint(g);
  c.paint(g);
}
@Override
public void init(GameContainer gc) throws SlickException {

}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
  p.update(60);
  c.update(60);
  p.keyListener(gc);
}
}

Player class:
public class Player {
Rectangle p;
Cube c = new Cube();
int x, y, width, height, speed;
double gravity, yVelocity;
boolean jump, moveDown, moveLeft, moveRight, onGround, colliding;

public Player(){
  x = 50;
  y = 100;
  width = 64;
  height = 64;
  speed = 100;
  gravity = 500;
  p = new Rectangle(x,y,width,height);
}

public void update(long deltaTime){
  double delta = deltaTime / 1000.0;
  if(jump)
   yVelocity = -400;
  if(moveLeft){
   x -= speed * delta;
  }
  if(moveRight){
   x += speed * delta;
  }

  if(x <= 0)
   x=0;
  if(y <= 0)
   y=0;
  if(x >= 640 - width)
   x = 640 - width;
  if(y >= 480 - width)
   onGround = true;
  else
   onGround = false;
  if(y > 480 - width)
   y = 480 - width;
  if(onGround && !jump)
   yVelocity = 0;

  y += yVelocity * delta;
  yVelocity += gravity * delta;

  p.setX(x);
  p.setY(y);
}

public void keyListener(GameContainer gc){
  Input input = gc.getInput();
  if(input.isKeyPressed(Input.KEY_W) && onGround || input.isKeyPressed(Input.KEY_SPACE) && onGround){
   jump = true;
  }else{jump = false;}
  if(input.isKeyDown(Input.KEY_A)){
   moveLeft = true;
  }else{moveLeft = false;}
  if(input.isKeyDown(Input.KEY_D)){
   moveRight = true;
  }else{moveRight = false;}
}


public void paint(Graphics g){
  g.drawRect(x,y,width,height);
  g.drawString("X/Y:" + x + "/" + y, 10, 25);
  g.drawString("onGround: " + onGround, 10, 40);
}

Cube class:
public class Cube {

int x, y, width, height;
double gravity, yVelocity;
boolean onGround;
Rectangle c;

public Cube(){
  x = 300;
  y = 100;
  width = 64;
  height = 64;
  gravity = 500;
  c = new Rectangle(x, y, width, height);
}

public void update(long deltaTime){
  double delta = deltaTime / 1000.0;

  if(y >= 480 - width){
   y = 480 - width;
   onGround = true;
  }else{onGround = false;}

  if(!onGround){
   y += yVelocity * delta;
   yVelocity += gravity * delta;
  }

  c.setX(x);
  c.setY(y);
}

public void paint(Graphics g){
  g.drawRect(x,y,width,height);
}
}

The code is probably very messy and I'm sure I've done a lot of things the wrong way, so feel free to point those out for my future reference.

Simple way to rotate image?

12 February 2012 - 09:51 PM

Hey guys, I have a question, I'm trying to make a game and I need to rotate my ship image, what is the most simple way of doing this?
Or am I better off just using four different images?

Thanks.

[Java] Getting mouse coordinates

26 December 2011 - 05:32 PM

Hey guys, I was wondering, what is the best way to get the coordinates of the mouse?

When I use Mouse.getX() and Mouse.getY(), the Y coordinate is wrong, it shows that 0 is at the bottom, instead of the top. How do I fix this? Or what is the best way to get the coordinates?

[Java] Detecting collision between rectangles between classes

19 December 2011 - 11:54 PM

The title probably doesn't make much sense, but I'm having trouble detecting the collision between my player from my Main class and the monster from the Entity class.

Problem: The collision isn't where the 'monster' rectangle(s) are drawn, the collision is at 0,0 but the 'monsters' are clearly at different coordinates.

Since I've been having so much trouble and I'm not completely sure what's causing it, I'm just going to cope/paste both of my classes here.

Main class:
package src;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;


public class Main extends BasicGame{
	int px = 100, py = 100;
	Entity eo = new Entity("", 0, 0, 0);
	Rectangle player = new Rectangle(px, py, 16, 16);
	
	public Main() {
		super("Test");
	}
	
	public static void main(String[] args) throws SlickException{
		AppGameContainer app = new AppGameContainer(new Main());
		app.start();
	}

	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		Entity m1 = new Entity("Monster1", 100, 400, 200); m1.render(gc, g); 
		Entity m2 = new Entity("Monster2", 100, 550, 200); m2.render(gc, g);
		Entity m3 = new Entity("Monster3", 100, 500, 300); m3.render(gc, g);
		g.drawString("X:" + player.getX() + "\nY: " + player.getY(), 10, 40);
		//Draw player
		g.drawRect(px, py, 16 ,16);
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
	}

	@Override
	public void update(GameContainer gc, int arg) throws SlickException {
		//Movement
		if(Keyboard.isKeyDown(Keyboard.KEY_W)) py -= 0.1;
		else if(Keyboard.isKeyDown(Keyboard.KEY_S)) py ++;
		else if(Keyboard.isKeyDown(Keyboard.KEY_D)) px ++;
		else if(Keyboard.isKeyDown(Keyboard.KEY_A)) px -= 0.1;
		
		player.setX(px);
		player.setY(py);
		
		// Collision
		if(player.intersects(eo.r)){
			System.out.println("Collision");
		}else{System.out.println("No collision");}
		
	}
}

And my Entity class:
package src;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;


public class Entity extends BasicGame{
	public String name;
	public int health, x, y;
	public Rectangle r = new Rectangle(x, y, 32, 32);

	
	public Entity(String n, int h, int posx, int posy){
		super("Test");
		name = n;
		health = h;
		x = posx;
		y = posy;
	}
	
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		g.drawString(name, x-20, y-32);
		g.drawString("Health: " + health, x-20, y-16);
    	g.drawRect(x, y, 32, 32);
		g.drawRect(x, y, 16, 16);
	}
	
	@Override
	public void init(GameContainer gc) throws SlickException {
		
	}

	@Override
	public void update(GameContainer gc, int i) throws SlickException {
	}
	
}

This draws the 'collision boxes' to where they need to be, I've made them bigger for testing purposes.
But the actually point of collision is at 0,0 (top-left.)

The code is messy, and I'm sure there are plenty of things wrong with it, but I've just been trying for the past two hours and can't get it to work, so it's far from what it needs to be.

Any and all help is appreciated, thanks for taking the time to read this.

[java] How to create multiple instances of the same thing?

03 December 2011 - 11:31 PM

This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below:

What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.)

How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks!

Oh also, once I do draw them and everything, how do I destroy them?

PARTNERS