for the longest time I have been using just rectangles to handle collision, and I was happy with that but I wanted to branch out in to something more complex with polygons using slick2d as the base system.
after some working with it I realized how easy is was compared to what I was doing before, but I ran into a problem. What I want is the ability to slide up slopes with little to no customization of the engine to handle certain shapes.
this is the basis of what I have so far
Entity.class
package Pheniox.Entity;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
import Pheniox.World.World;
public abstract class Entity {
protected Vector2f position = new Vector2f(0,0);
protected Vector2f velocity = new Vector2f(0,0);
protected World world;
public boolean removed = false;
private Object entities;
//collision booleans
protected boolean collideTop,collideLeft, collideBottom,collideRight;
//constructors
public Entity(float x, float y){
position.x = x;
position.y = y;
}
public void init(World world){
this.world = world;
init();
}
//removes the player from the world
public void remove(){
removed = true;
}
//Movement methods
public void move(Vector2f vel, int delta){
move(vel.x, vel.y, delta);
}
//takes 2 floats and moves
public void move(float xa, float ya, int delta){
if(xa != 0 && ya != 0){
move(xa, 0,delta);
move(0,ya,delta);
return;
}
boolean canPass = true;
collideTop = false;
collideLeft = false;
collideBottom = false;
collideRight = false;
ArrayList<Entity> entities = world.getEntities();
for(Entity e: entities){
if(e != this){
if(this.getCollisionBox(xa * delta, ya * delta).intersects(e.getCollisionBox(e.velocity.x * delta,e.velocity.y * delta))){
canPass = false;
}
if(xa > 0 && !canPass){
collideLeft = true;
}
if(xa < 0 && !canPass){
collideRight = true;
}
if(ya > 0 && !canPass){
collideBottom = true;
}
if(ya < 0 && !canPass){
collideTop = true;
}
}
}
if(canPass){
this.position.x += delta * xa;
this.position.y += delta * ya;
}
}
//render methods
public void render(GameContainer gc, Graphics g){
renderEntity(gc,g);
}
//update methods
public void update(GameContainer gc, StateBasedGame sbg , int delta){
move(velocity.x,velocity.y,delta);
updateEntity(gc,sbg,delta);
}
//abstract classes
public abstract void init();
public abstract void renderEntity(GameContainer gc,Graphics g);
public abstract void updateEntity(GameContainer gc, StateBasedGame sbg, int delta);
public abstract Shape getCollisionBox(float xa, float ya);
}
the collision box shape is declared in “getCollisionBox(float xa, float ya)”
when being extends by other classes
basicaly what I want is say in a 2d platformer the ability to go up slopes, and I want it to just inheret to the entity class and not have certain cases that make it happen.
any help would be appreciated.