Friday, June 21, 2019

java - Draw Rectangle To All Dimensions of Image


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?



Answer



//This checks overlap in a single dimension -- either x or in y

public boolean checkOverlap(int start0, int end0, int start1, int end1)
{
if(start0 > start1 && start0 < end1)
return true;
if(start1 > start0 && start1 < end0)
return true;
return false;
}

//This does it for two dimensions. If overlapping in X AND Y, you've collided.

//You can call this in your movePlayer handler, but if the "females" (er!) move
//AS WELL, then you must do it per update or you will miss collisions (unless
//you make sure all "females" are moved before the player, on each update).
public bool isColliding(Rectangle rect0, Rectangle rect1)
{
if (checkOverlap(rect0.x, rect0.x + rect0.width, rect1.x, rect1.x + rect1.width) &&
checkOverlap(rect0.y, rect0.y + rect0.height, rect1.y, rect1.y + rect1.height))
{
//resolve your collision. You don't need to store isColliding -- just
//calculate, then act on the result.

}
}

On each game loop update: Get your inputs, move the player, get all inputs for other entities and move them, then at the end, run through the full entities list (player + "females") and run isColliding() on each. This the standard way to do 2D AABB collisions.


Actually if you are running through the full list of n entities and comparing them against n-1 other entities, then for the first entity in the list, call it a, you will have to check against b through n, but on checking b you will not have to check it against a (since a was just checked against b in the prior step) or itself, so you would check from c through n... and so on for each remaining entity. This essentially looks like:


for (int i = 0; i < entities.length; i++) //run through ALL entities
{
Entity e1 = entities[i];
for (int j = i+1; j < entities.length; j++) //run through each entity AFTER e1
{

Entity e2 = entities[j];
if (isColliding(e1.rect, e2.rect))
resolveCollisionFor(e1, e2);
}
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...