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.






