Draw Rectangle To All Dimensions of Image
Posted
by
opiop65
on Game Development
See other posts from Game Development
or by opiop65
Published on 2012-10-27T21:52:09Z
Indexed on
2012/10/27
23:22 UTC
Read the original article
Hit count: 328
I have some rudimentary collision code:
public class Collision {
static boolean isColliding = false;
static Rectangle player;
static Rectangle female;
public static void collision(){
Rectangle player = Game.Playerbounds();
Rectangle female = Game.Femalebounds();
if(player.intersects(female)){
isColliding = true;
}else{
isColliding = false;
}
}
}
And this is the rectangle code:
public static Rectangle Playerbounds() {
return(new Rectangle(posX, posY, 25, 25));
}
public static Rectangle Femalebounds() {
return(new Rectangle(femaleX, femaleY, 25, 25));
}
My InputHandling class:
public static void movePlayer(GameContainer gc, int delta){
Input input = gc.getInput();
if(input.isKeyDown(input.KEY_W)){
Game.posY -= walkSpeed * delta;
walkUp = true;
if(Collision.isColliding == true){
Game.posY += walkSpeed * delta;
}
}
if(input.isKeyDown(input.KEY_S)){
Game.posY += walkSpeed * delta;
walkDown = true;
if(Collision.isColliding == true){
Game.posY -= walkSpeed * delta;
}
}
if(input.isKeyDown(input.KEY_D)){
Game.posX += walkSpeed * delta;
walkRight = true;
if(Collision.isColliding == true){
Game.posX -= walkSpeed * delta;
}
}
if(input.isKeyDown(input.KEY_A)){
Game.posX -= walkSpeed * delta;
walkLeft = true;
if(Collision.isColliding == true){
Game.posX += walkSpeed * delta;
}
}
}
The code works partially. Only the right and top side of the images collide. How do I correct the rectangle so it will draw on all sides? Thanks for any suggestions!
© Game Development or respective owner