Skip to content

hitBox to Polygon #42

@Sabathorn

Description

@Sabathorn

Hi!! this patch is for convert the Entity Hitbox to a Polygon.
with minor changes in World (the hitBox rendering for tests)

From db598388c208f306ed4ed7321b986fb758827e09 Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Tue, 19 Apr 2011 06:25:42 -0430
Subject: [PATCH] Created a Polygon hitBox

---
 src/it/randomtower/engine/World.java         |    8 ++-
 src/it/randomtower/engine/entity/Entity.java |   89 +++++++++++++------------
 2 files changed, 51 insertions(+), 46 deletions(-)

diff --git a/src/it/randomtower/engine/World.java b/src/it/randomtower/engine/World.java
index a0f7bbe..b640848 100644
--- a/src/it/randomtower/engine/World.java
+++ b/src/it/randomtower/engine/World.java
@@ -74,9 +74,11 @@ public class World extends BasicGameState {
        for (Entity e : entities) {
            if (ME.debugEnabled) {
                g.setColor(ME.borderColor);
-               Rectangle hitBox = new Rectangle(e.x + e.hitboxOffsetX, e.y
-                       + e.hitboxOffsetY, e.width, e.height);
-               g.draw(hitBox);
+               if(null != e.hitBox){
+                   g.draw(new Rectangle(e.x + e.hitBox.getX(), e.y
+                               + e.hitBox.getY(), e.width, e.height
+                               ));
+               }
                g.setColor(Color.white);
            }
            if (camera != null) {
diff --git a/src/it/randomtower/engine/entity/Entity.java b/src/it/randomtower/engine/entity/Entity.java
index 0ddd6c5..5f5d72f 100644
--- a/src/it/randomtower/engine/entity/Entity.java
+++ b/src/it/randomtower/engine/entity/Entity.java
@@ -19,6 +19,7 @@ import org.newdawn.slick.Image;
 import org.newdawn.slick.Input;
 import org.newdawn.slick.SlickException;
 import org.newdawn.slick.SpriteSheet;
+import org.newdawn.slick.geom.Polygon;
 import org.newdawn.slick.geom.Rectangle;
 import org.newdawn.slick.geom.Vector2f;

@@ -112,14 +113,16 @@ public abstract class Entity implements Comparable<Entity> {
    public boolean visible = true;

    /** x offset for collision box */
-   public float hitboxOffsetX;
+   //public float hitboxOffsetX;
    /** y offset for collision box */
-   public float hitboxOffsetY;
+   //public float hitboxOffsetY;
    /** hitbox width of entity **/
-   public int hitboxWidth;
+   //public int hitboxWidth;
    /** hitbox height of entity **/
-   public int hitboxHeight;
+   //public int hitboxHeight;

+   public Rectangle hitBox;
+   
    /** stateManager for entity **/
    public StateManager stateManager;

@@ -166,14 +169,14 @@ public abstract class Entity implements Comparable<Entity> {
        }
        if (on) {
            // modify hitbox position accordingly - move it a bit up and left
-           this.hitboxOffsetX -= whalf;
-           this.hitboxOffsetY -= hhalf;
+           this.hitBox.setX(this.hitBox.getX() - whalf);
+           this.hitBox.setY(this.hitBox.getY() - hhalf);
            this.centered = true;
        } else {
            if (centered == true) {
                // reset hitbox position to top left origin
-               this.hitboxOffsetX += whalf;
-               this.hitboxOffsetY += hhalf;
+               this.hitBox.setX(this.hitBox.getX() + whalf);
+               this.hitBox.setY(this.hitBox.getY() + hhalf);
            }
            this.centered = false;
        }
@@ -267,9 +270,7 @@ public abstract class Entity implements Comparable<Entity> {
        }
        if (ME.debugEnabled) {
            g.setColor(ME.borderColor);
-           Rectangle hitBox = new Rectangle(x + hitboxOffsetX, y
-                   + hitboxOffsetY, hitboxWidth, hitboxHeight);
-           g.draw(hitBox);
+           if(null !=hitBox){g.draw(hitBox);}
            g.setColor(Color.white);
            g.drawRect(x, y, 1, 1);
            // draw entity center
@@ -422,11 +423,12 @@ public abstract class Entity implements Comparable<Entity> {
     */
    public void setHitBox(float xOffset, float yOffset, int width, int height,
            boolean collidable) {
-       this.hitboxOffsetX = xOffset;
-       this.hitboxOffsetY = yOffset;
-       this.hitboxWidth = width;
-       this.hitboxHeight = height;
+       //this.hitboxOffsetX = xOffset;
+       //this.hitboxOffsetY = yOffset;
+       //this.hitboxWidth = width;
+       //this.hitboxHeight = height;
        this.collidable = true;
+       hitBox = new Rectangle(xOffset,yOffset,width,height);
    }

    /**
@@ -439,6 +441,8 @@ public abstract class Entity implements Comparable<Entity> {
        return type.addAll(Arrays.asList(types));
    }

+   
+   
    /**
     * check collision with another entity of given type
     * 
@@ -453,15 +457,14 @@ public abstract class Entity implements Comparable<Entity> {
        // offset
        for (Entity entity : world.getEntities()) {
            if (entity.collidable && entity.type.contains(type)) {
-               if (!entity.equals(this)
-                       && x + hitboxOffsetX + hitboxWidth > entity.x
-                               + entity.hitboxOffsetX
-                       && y + hitboxOffsetY + hitboxHeight > entity.y
-                               + entity.hitboxOffsetY
-                       && x + hitboxOffsetX < entity.x + entity.hitboxOffsetX
-                               + entity.hitboxWidth
-                       && y + hitboxOffsetY < entity.y + entity.hitboxOffsetY
-                               + entity.hitboxHeight) {
+               if ( !entity.equals(this) && x + hitBox.getX() + hitBox.getWidth() > entity.x
+                       + entity.hitBox.getX()
+                       && y + hitBox.getY() + hitBox.getHeight() > entity.y
+                               + entity.hitBox.getY()
+                       && x + hitBox.getX() < entity.x + entity.hitBox.getX()
+                               + entity.hitBox.getWidth()
+                       && y + hitBox.getY() < entity.y + entity.hitBox.getY()
+                               + entity.hitBox.getHeight()) {
                    this.collisionResponse(entity);
                    entity.collisionResponse(this);
                    return entity;
@@ -483,14 +486,14 @@ public abstract class Entity implements Comparable<Entity> {
    public Entity collideWith(Entity other, float x, float y) {
        if (other.collidable) {
            if (!other.equals(this)
-                   && x + hitboxOffsetX + hitboxWidth > other.x
-                           + other.hitboxOffsetX
-                   && y + hitboxOffsetY + hitboxHeight > other.y
-                           + other.hitboxOffsetY
-                   && x + hitboxOffsetX < other.x + other.hitboxOffsetX
-                           + other.hitboxWidth
-                   && y + hitboxOffsetY < other.y + other.hitboxOffsetY
-                           + other.hitboxHeight) {
+                   && x + hitBox.getX() + hitBox.getWidth() > other.x
+                           + other.hitBox.getY()
+                   && y + hitBox.getY() + hitBox.getHeight() > other.y
+                           + other.hitBox.getY()
+                   && x + hitBox.getX() < other.x + other.hitBox.getX()
+                           + other.hitBox.getWidth()
+                   && y + hitBox.getY() < other.y + other.hitBox.getY()
+                           + other.hitBox.getHeight()) {
                this.collisionResponse(other);
                other.collisionResponse(this);
                return other;
@@ -507,14 +510,14 @@ public abstract class Entity implements Comparable<Entity> {
        for (Entity entity : world.getEntities()) {
            if (entity.collidable && entity.type.contains(type)) {
                if (!entity.equals(this)
-                       && x + hitboxOffsetX + hitboxWidth > entity.x
-                               + entity.hitboxOffsetX
-                       && y + hitboxOffsetY + hitboxHeight > entity.y
-                               + entity.hitboxOffsetY
-                       && x + hitboxOffsetX < entity.x + entity.hitboxOffsetX
-                               + entity.hitboxWidth
-                       && y + hitboxOffsetY < entity.y + entity.hitboxOffsetY
-                               + entity.hitboxHeight) {
+                       && x + hitBox.getX() + hitBox.getWidth() > entity.x
+                               + entity.hitBox.getX()
+                       && y + hitBox.getY() + hitBox.getHeight() > entity.y
+                               + entity.hitBox.getY()
+                       && x + hitBox.getX() < entity.x + entity.hitBox.getX()
+                               + entity.hitBox.getWidth()
+                       && y + hitBox.getY() < entity.y + entity.hitBox.getY()
+                               + entity.hitBox.getHeight()) {
                    this.collisionResponse(entity);
                    entity.collisionResponse(this);
                    if (collidingEntities == null)
@@ -535,9 +538,9 @@ public abstract class Entity implements Comparable<Entity> {
     *            The y-position of the point.
     */
    public boolean collidePoint(float x, float y) {
-       if (x >= this.x - hitboxOffsetX && y >= this.y - hitboxOffsetY
-               && x < this.x - hitboxOffsetX + width
-               && y < this.y - hitboxOffsetY + height) {
+       if (x >= this.x - hitBox.getX() && y >= this.y - hitBox.getY()
+               && x < this.x - hitBox.getX() + width
+               && y < this.y - hitBox.getY() + height) {
            this.collisionResponse(null);
            return true;
        }
-- 
1.7.3.1.msysgit.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions