MarteEngine
MarteEngine copied to clipboard
hitBox to Polygon
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
Nice one, Sabathorn. I will look at it and merge it into my devtommy branch. Good base to extend it for scaling and rotation (see issues #12 and #24). Thanks a lot!
not at all! I was trying to do scaling and rotation, but the Polygon jumps around when applying transformations ^^u
Oops seems that i pasted the wrong content, in that patch i was working with a Rectangle.. i have the Polygon one... (and i'm a bit confused with git too ^^u. I will checkout again and overwrite the changed files to make a new patch..
ok this i sthe good one!! Sorry for the mistake..
From 33ccbebd8bf7873fb06157602a5869e70926518c Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Tue, 19 Apr 2011 10:57:22 -0430
Subject: [PATCH] HitBox to Polygon.. all test working fine
---
src/it/randomtower/engine/World.java | 8 +-
src/it/randomtower/engine/entity/Entity.java | 109 +++++++++++++------------
2 files changed, 62 insertions(+), 55 deletions(-)
diff --git a/src/it/randomtower/engine/World.java b/src/it/randomtower/engine/World.java
index a0f7bbe..f4aa010 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..3422c1b 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;
@@ -111,15 +112,9 @@ public abstract class Entity implements Comparable<Entity> {
/** true if this entity should be visible, false otherwise */
public boolean visible = true;
- /** x offset for collision box */
- public float hitboxOffsetX;
- /** y offset for collision box */
- public float hitboxOffsetY;
- /** hitbox width of entity **/
- public int hitboxWidth;
- /** hitbox height of entity **/
- public int hitboxHeight;
-
+ /** this entity hitBox */
+ public Polygon hitBox;
+
/** stateManager for entity **/
public StateManager stateManager;
@@ -164,16 +159,18 @@ public abstract class Entity implements Comparable<Entity> {
whalf = animations.get(currentAnim).getWidth() / 2;
hhalf = animations.get(currentAnim).getHeight() / 2;
}
+
+ if(null == hitBox){return;}
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;
}
@@ -207,6 +204,7 @@ public abstract class Entity implements Comparable<Entity> {
checkWorldBoundaries();
previousx = x;
previousy = y;
+
}
/**
@@ -237,6 +235,7 @@ public abstract class Entity implements Comparable<Entity> {
g.rotate(x, y, angle);
}
anim.draw(xpos, ypos, w * scale, h * scale, color);
+
if (angle != 0)
g.resetTransform();
} else if (currentImage != null) {
@@ -254,10 +253,11 @@ public abstract class Entity implements Comparable<Entity> {
currentImage.setRotation(angle);
}
if (scale != 1.0f) {
- if (centered)
+ if (centered){
g.translate(xpos - (w * scale - w), ypos - (h * scale - h));
- else
+ }else{
g.translate(xpos, ypos);
+ }
g.scale(scale, scale);
g.drawImage(currentImage, 0, 0);
} else
@@ -266,12 +266,14 @@ public abstract class Entity implements Comparable<Entity> {
g.resetTransform();
}
if (ME.debugEnabled) {
- g.setColor(ME.borderColor);
- Rectangle hitBox = new Rectangle(x + hitboxOffsetX, y
- + hitboxOffsetY, hitboxWidth, hitboxHeight);
- g.draw(hitBox);
+
+ if(null != hitBox){
+ //create a rectangle and print it like world.render does.
+ g.setColor(ME.borderColor);
+ g.draw(new Rectangle(x + hitBox.getX() , y + hitBox.getY() , width, height));
+ }
g.setColor(Color.white);
- g.drawRect(x, y, 1, 1);
+ g.drawRect(x , y , 1, 1);
// draw entity center
if (width!=0 && height!=0){
float centerX = x + width/2;
@@ -422,11 +424,13 @@ 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.collidable = true;
+
+ hitBox = new Polygon(new float[]{xOffset,yOffset, //Top-Left
+ xOffset+width, yOffset, //Top Right
+ xOffset+width,yOffset+height,//Bottom-Right
+ xOffset,yOffset+height}); //Bottom-Left
+
}
/**
@@ -439,6 +443,8 @@ public abstract class Entity implements Comparable<Entity> {
return type.addAll(Arrays.asList(types));
}
+
+
/**
* check collision with another entity of given type
*
@@ -453,15 +459,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 +488,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 +512,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 +540,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
thank you for report, we'll evaluate that change to see if we need it or not :D
As Gornova said ;-) Rotation not working might be the case because the rotation center is not properly taken into account.
About rotation:
yes i was thinking the same about centers, the problem was in the test rendering (the red border used for tests) i had to change the draw method at that point to see it spinning in the entity tests, g.draw(new Polygon(hitBox.transform(Transform.createTranslateTransform(x, y)).getPoints()));
I noticed that World and Entity have the same "render red border on entities" test code i had to comment one.. but now the shape is spinning real fast - faster than the Entity image -. :) . Well i will let you know if i can make it spin at the right velocity.
Hi!! i managed to do scaling an rotating but... with a strange behavior:
this is how it looks like http://img810.imageshack.us/img810/6098/rotatingscaling.png
and this is when it fails: http://img28.imageshack.us/img28/9571/rotatingscaling2.png
it's like the hitBox is"overflowing", i'm still looking for a solution... any advice would be appreciated. Thanks in advance.
My spare time is currently limited so I want to finish my competition entry first. Afterwards I proceed with Marte. The error is either somewhere in your code or in Slick's Shape classes. Just add some debug output where you dump the scaling or rotation factor of the Shape. Should keep you going ;-) The Shape around the triangle looks like it's angle is a bit off compared to the image.
The Shape around the triangle looks like it's angle is a bit off compared to the image.
Yes, the shape is updating faster than the animations, Still looking for a way to sync both ( Entity.update() and anim ).
sorry for delay on answer :D
my suggestion is always to build a little new test, so it's easy to test for both me and thaaks :D
Here is the Patch (may need the previous one - hitbox to Polygon -)
What i did here:
- created a variable at Entity called hitBoxOrig wich stores the original points of the hitBox.
- created variable at Entity called maxScale. (need a scale limit to check the scale transformation)
- Method Entity.Update(): there i was doing transformations (maybe also the bug's lair).
- leave a comment on Entity.setAngle().
- AngleAlphaScaleMoveEntity: added a "set name" line for debugging. Asigned maxScale.
I'm using the Entity's tests. Thanks a lot!!
From a43e02d90dae6ffd0d9318c74357d5d5feab2704 Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Wed, 20 Apr 2011 09:29:40 -0430
Subject: [PATCH 2/2] Nearly implemented rotation and scale for the hitbox.
Rotate: cannot sync with the animations.
Scale: has a glitch when reaching the maximum scale limit. ( the target scale value)
---
src/it/randomtower/engine/ME.java | 2 +-
src/it/randomtower/engine/World.java | 11 +++-
src/it/randomtower/engine/entity/Entity.java | 51 ++++++++++++++++----
.../test/entity/AngleAlphaScaleMoveEntity.java | 6 ++-
4 files changed, 56 insertions(+), 14 deletions(-)
diff --git a/src/it/randomtower/engine/ME.java b/src/it/randomtower/engine/ME.java
index 2891764..9dcb8f1 100644
--- a/src/it/randomtower/engine/ME.java
+++ b/src/it/randomtower/engine/ME.java
@@ -20,7 +20,7 @@ import org.newdawn.slick.state.StateBasedGame;
public class ME {
/** true if debug is enabled, shows hitbox of entities **/
- public static boolean debugEnabled = false;
+ public static boolean debugEnabled = true;
/** key to activate debug mode **/
public static int keyToggleDebug = -1;
/** default border color of hitbox in debug mode **/
diff --git a/src/it/randomtower/engine/World.java b/src/it/randomtower/engine/World.java
index f4aa010..ae0df82 100644
--- a/src/it/randomtower/engine/World.java
+++ b/src/it/randomtower/engine/World.java
@@ -13,7 +13,10 @@ import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
+import org.newdawn.slick.geom.Shape;
+import org.newdawn.slick.geom.Transform;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;
@@ -75,9 +78,11 @@ public class World extends BasicGameState {
if (ME.debugEnabled) {
g.setColor(ME.borderColor);
if(null != e.hitBox){
- g.draw(new Rectangle(e.x + e.hitBox.getX(), e.y
- + e.hitBox.getY(), e.width, e.height
- ));
+ // g.draw(new Rectangle(e.x + e.hitBox.getX(), e.y
+ // + e.hitBox.getY(), e.width, e.height
+ // ));
+
+ // g.draw(new Polygon(e.hitBox.transform(Transform.createTranslateTransform(e.x, e.y)).getPoints()));
}
g.setColor(Color.white);
}
diff --git a/src/it/randomtower/engine/entity/Entity.java b/src/it/randomtower/engine/entity/Entity.java
index 3422c1b..4eab37a 100644
--- a/src/it/randomtower/engine/entity/Entity.java
+++ b/src/it/randomtower/engine/entity/Entity.java
@@ -20,7 +20,7 @@ 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.Transform;
import org.newdawn.slick.geom.Vector2f;
//TODO modify hitbox coordinates to a real shape without changing method interface.
@@ -74,9 +74,12 @@ public abstract class Entity implements Comparable<Entity> {
* used for direction!
*/
protected int angle = 0;
-
+
/** scale used for both horizontal and vertical scaling. */
public float scale = 1.0f;
+
+ /** the maximun scale to be used in a transformation */
+ public float maxScale = 1.0f;
/**
* color of the entity, mainly used for alpha transparency, but could also
@@ -114,6 +117,8 @@ public abstract class Entity implements Comparable<Entity> {
/** this entity hitBox */
public Polygon hitBox;
+ /** the original points of the hitBox, used for scaling. */
+ private float[] hitBoxOrig = new float[0];
/** stateManager for entity **/
public StateManager stateManager;
@@ -204,6 +209,30 @@ public abstract class Entity implements Comparable<Entity> {
checkWorldBoundaries();
previousx = x;
previousy = y;
+
+ /* from this point hitBox related updates */
+
+ if(null == hitBox){ return;}
+ /*
+ if (animations != null && currentAnim != null){
+ Animation anim = animations.get(currentAnim);
+ if (anim != null) {
+ //Can't syncrhonize with the animation
+ }
+ }
+ */
+
+ if( scale > maxScale){
+ scale = maxScale;
+ }
+ //System.out.println(" Object: " + name + " scale:\t" + scale + " maxScale\t" + maxScale);
+ if( scale <= 1f ){ hitBox = new Polygon(hitBoxOrig); }
+
+ hitBox = new Polygon(hitBox.transform(Transform.createScaleTransform(scale, scale)).getPoints());
+
+ if(0 == angle){return;}
+ System.out.println(angle);
+ hitBox = new Polygon(hitBox.transform(Transform.createRotateTransform(angle)).getPoints());
}
@@ -235,9 +264,10 @@ public abstract class Entity implements Comparable<Entity> {
g.rotate(x, y, angle);
}
anim.draw(xpos, ypos, w * scale, h * scale, color);
-
- if (angle != 0)
+
+ if (angle != 0){
g.resetTransform();
+ }
} else if (currentImage != null) {
currentImage.setAlpha(color.a);
int w = currentImage.getWidth() / 2;
@@ -260,6 +290,7 @@ public abstract class Entity implements Comparable<Entity> {
}
g.scale(scale, scale);
g.drawImage(currentImage, 0, 0);
+
} else
g.drawImage(currentImage, xpos, ypos);
if (scale != 1.0f)
@@ -270,7 +301,7 @@ public abstract class Entity implements Comparable<Entity> {
if(null != hitBox){
//create a rectangle and print it like world.render does.
g.setColor(ME.borderColor);
- g.draw(new Rectangle(x + hitBox.getX() , y + hitBox.getY() , width, height));
+ g.draw(new Polygon(hitBox.transform(Transform.createTranslateTransform(x, y)).getPoints()));
}
g.setColor(Color.white);
g.drawRect(x , y , 1, 1);
@@ -426,11 +457,12 @@ public abstract class Entity implements Comparable<Entity> {
boolean collidable) {
this.collidable = true;
- hitBox = new Polygon(new float[]{xOffset,yOffset, //Top-Left
- xOffset+width, yOffset, //Top Right
+
+ hitBox = new Polygon(new float[]{xOffset,yOffset, //Top-Left
+ xOffset+width, yOffset, //Top Right
xOffset+width,yOffset+height,//Bottom-Right
- xOffset,yOffset+height}); //Bottom-Left
-
+ xOffset,yOffset+height}); //Bottom-Left
+ hitBoxOrig = hitBox.getPoints();
}
/**
@@ -787,6 +819,7 @@ public abstract class Entity implements Comparable<Entity> {
// TODO: add proper rotation for the hitbox/shape here!!!
public void setAngle(int angle) {
this.angle = angle;
+ //not working?? hitBox = new Polygon( hitBox.transform(Transform.createRotateTransform(angle)).getPoints());
}
public Color getColor() {
diff --git a/test/it/randomtower/test/entity/AngleAlphaScaleMoveEntity.java b/test/it/randomtower/test/entity/AngleAlphaScaleMoveEntity.java
index 5da8a89..45b43ca 100644
--- a/test/it/randomtower/test/entity/AngleAlphaScaleMoveEntity.java
+++ b/test/it/randomtower/test/entity/AngleAlphaScaleMoveEntity.java
@@ -15,13 +15,14 @@ public class AngleAlphaScaleMoveEntity extends Entity {
private float scaleDir = -0.1f;
private float alphaDir = -0.05f;
+
private String[] rotatingPlayers = {"Player", "ScaledPlayer", "RotatingAndAlphaPlayer", "RotatingAndScalingAndAlphaPlayer" };
private String[] scalingPlayers = {"ScaledPlayer", "RotatingAndScalingAndAlphaPlayer" };
private String[] alphaPlayers = {"RotatingAndAlphaPlayer", "RotatingAndScalingAndAlphaPlayer" };
public AngleAlphaScaleMoveEntity(float x, float y, boolean changeAngle, boolean changeAlpha, boolean changeScale, boolean move) {
super(x, y);
-
+ name = "AngleAlphaScaleMoveEntity at position ( " + x + "," + y + ")";
// load and get the image that we are showing
if (ResourceManager.getImage("ship") == null) {
try {
@@ -42,12 +43,15 @@ public class AngleAlphaScaleMoveEntity extends Entity {
if (changeScale) {
this.setAlarm("scaleMe", 10, false, true);
+ this.maxScale = 2.0f; //setting the maxScale value (hardCoded for testing purposes only)
}
if (changeAlpha) {
this.setAlarm("alphaMe", 5, false, true);
}
this.setCentered(true);
+
+
}
@Override
--
1.7.3.1.msysgit.0
Hello again. I fixed the scaling, but rotating still out of sync. Here is the patch. I applied transformations at the Entity.Update() method using a Transformation object. This patch contains also the Hitbox to Polygon so the older (and useless) patches i posted aren't needed. Almost done, i think that if i could know when the animations update then i can do the sync..
From 7aa2aaa9546b18960cda723982b156ecb84ddcbb Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Sun, 24 Apr 2011 15:07:37 -0430
Subject: [PATCH] hitbox to polygon, Scaling and rotating(rotating aout of Sync)
---
src/it/randomtower/engine/World.java | 4 +-
src/it/randomtower/engine/entity/Entity.java | 129 +++++++++++++++-----------
2 files changed, 75 insertions(+), 58 deletions(-)
diff --git a/src/it/randomtower/engine/World.java b/src/it/randomtower/engine/World.java
index a0f7bbe..498272d 100644
--- a/src/it/randomtower/engine/World.java
+++ b/src/it/randomtower/engine/World.java
@@ -74,9 +74,7 @@ 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);
+ g.draw(e.hitBox);
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..eb5a8b4 100644
--- a/src/it/randomtower/engine/entity/Entity.java
+++ b/src/it/randomtower/engine/entity/Entity.java
@@ -19,7 +19,8 @@ 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.Rectangle;
+import org.newdawn.slick.geom.Polygon;
+import org.newdawn.slick.geom.Transform;
import org.newdawn.slick.geom.Vector2f;
//TODO modify hitbox coordinates to a real shape without changing method interface.
@@ -73,9 +74,10 @@ public abstract class Entity implements Comparable<Entity> {
* used for direction!
*/
protected int angle = 0;
-
+
/** scale used for both horizontal and vertical scaling. */
public float scale = 1.0f;
+
/**
* color of the entity, mainly used for alpha transparency, but could also
@@ -111,15 +113,11 @@ public abstract class Entity implements Comparable<Entity> {
/** true if this entity should be visible, false otherwise */
public boolean visible = true;
- /** x offset for collision box */
- public float hitboxOffsetX;
- /** y offset for collision box */
- public float hitboxOffsetY;
- /** hitbox width of entity **/
- public int hitboxWidth;
- /** hitbox height of entity **/
- public int hitboxHeight;
-
+ /** this entity hitBox */
+ public Polygon hitBox;
+ /** the original points of the hitBox, used for scaling. */
+ private float[] hitBoxOrig = new float[0];
+
/** stateManager for entity **/
public StateManager stateManager;
@@ -164,16 +162,18 @@ public abstract class Entity implements Comparable<Entity> {
whalf = animations.get(currentAnim).getWidth() / 2;
hhalf = animations.get(currentAnim).getHeight() / 2;
}
+
+ if(null == hitBox){return;}
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;
}
@@ -207,6 +207,15 @@ public abstract class Entity implements Comparable<Entity> {
checkWorldBoundaries();
previousx = x;
previousy = y;
+
+ /* from this point hitBox related updates */
+ if(null == hitBox ){ return;}
+ hitBox = new Polygon(hitBoxOrig);
+ Transform trans = new Transform(Transform.createTranslateTransform(x, y),Transform.createScaleTransform(scale, scale));
+ //can't sync with animations
+ trans.concatenate(Transform.createRotateTransform(angle));
+ hitBox = new Polygon(hitBox.transform(trans).getPoints());
+
}
/**
@@ -237,8 +246,10 @@ public abstract class Entity implements Comparable<Entity> {
g.rotate(x, y, angle);
}
anim.draw(xpos, ypos, w * scale, h * scale, color);
- if (angle != 0)
+
+ if (angle != 0){
g.resetTransform();
+ }
} else if (currentImage != null) {
currentImage.setAlpha(color.a);
int w = currentImage.getWidth() / 2;
@@ -254,24 +265,28 @@ public abstract class Entity implements Comparable<Entity> {
currentImage.setRotation(angle);
}
if (scale != 1.0f) {
- if (centered)
+ if (centered){
g.translate(xpos - (w * scale - w), ypos - (h * scale - h));
- else
+ }else{
g.translate(xpos, ypos);
+ }
g.scale(scale, scale);
g.drawImage(currentImage, 0, 0);
+
} else
g.drawImage(currentImage, xpos, ypos);
if (scale != 1.0f)
g.resetTransform();
}
if (ME.debugEnabled) {
- g.setColor(ME.borderColor);
- Rectangle hitBox = new Rectangle(x + hitboxOffsetX, y
- + hitboxOffsetY, hitboxWidth, hitboxHeight);
- g.draw(hitBox);
+
+ if(null != hitBox){
+ //create a rectangle and print it like world.render does.
+ g.setColor(ME.borderColor);
+ g.draw(hitBox);
+ }
g.setColor(Color.white);
- g.drawRect(x, y, 1, 1);
+ g.drawRect(x , y , 1, 1);
// draw entity center
if (width!=0 && height!=0){
float centerX = x + width/2;
@@ -422,11 +437,14 @@ 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.collidable = true;
+
+ hitBox = new Polygon(new float[]{xOffset,yOffset, //Top-Left
+ xOffset+width, yOffset, //Top Right
+ xOffset+width,yOffset+height,//Bottom-Right
+ xOffset,yOffset+height}); //Bottom-Left
+
+ hitBoxOrig = hitBox.getPoints();
}
/**
@@ -439,6 +457,8 @@ public abstract class Entity implements Comparable<Entity> {
return type.addAll(Arrays.asList(types));
}
+
+
/**
* check collision with another entity of given type
*
@@ -453,15 +473,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 +502,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 +526,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 +554,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
Hi again. Now the animations are in sync, the other entities in the test don't. here is a screenshot: http://img847.imageshack.us/img847/410/rotatingsca.png i will post the patch when i can make it work.
super cool :D you are just too fast to be followed, thanks for your effort!
hi Sabathorn, any news, do you have something working? If you can please feedback me on this otherwise i need to move this issue on version 0.4
Hi!! i did it but i have a little synchronization problem.
here are the patches:
From 7aa2aaa9546b18960cda723982b156ecb84ddcbb Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Sun, 24 Apr 2011 15:07:37 -0430
Subject: [PATCH 1/2] hitbox to polygon, Scaling and rotating(rotating aout of Sync)
---
src/it/randomtower/engine/World.java | 4 +-
src/it/randomtower/engine/entity/Entity.java | 129 +++++++++++++++-----------
2 files changed, 75 insertions(+), 58 deletions(-)
diff --git a/src/it/randomtower/engine/World.java b/src/it/randomtower/engine/World.java
index a0f7bbe..498272d 100644
--- a/src/it/randomtower/engine/World.java
+++ b/src/it/randomtower/engine/World.java
@@ -74,9 +74,7 @@ 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);
+ g.draw(e.hitBox);
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..eb5a8b4 100644
--- a/src/it/randomtower/engine/entity/Entity.java
+++ b/src/it/randomtower/engine/entity/Entity.java
@@ -19,7 +19,8 @@ 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.Rectangle;
+import org.newdawn.slick.geom.Polygon;
+import org.newdawn.slick.geom.Transform;
import org.newdawn.slick.geom.Vector2f;
//TODO modify hitbox coordinates to a real shape without changing method interface.
@@ -73,9 +74,10 @@ public abstract class Entity implements Comparable<Entity> {
* used for direction!
*/
protected int angle = 0;
-
+
/** scale used for both horizontal and vertical scaling. */
public float scale = 1.0f;
+
/**
* color of the entity, mainly used for alpha transparency, but could also
@@ -111,15 +113,11 @@ public abstract class Entity implements Comparable<Entity> {
/** true if this entity should be visible, false otherwise */
public boolean visible = true;
- /** x offset for collision box */
- public float hitboxOffsetX;
- /** y offset for collision box */
- public float hitboxOffsetY;
- /** hitbox width of entity **/
- public int hitboxWidth;
- /** hitbox height of entity **/
- public int hitboxHeight;
-
+ /** this entity hitBox */
+ public Polygon hitBox;
+ /** the original points of the hitBox, used for scaling. */
+ private float[] hitBoxOrig = new float[0];
+
/** stateManager for entity **/
public StateManager stateManager;
@@ -164,16 +162,18 @@ public abstract class Entity implements Comparable<Entity> {
whalf = animations.get(currentAnim).getWidth() / 2;
hhalf = animations.get(currentAnim).getHeight() / 2;
}
+
+ if(null == hitBox){return;}
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;
}
@@ -207,6 +207,15 @@ public abstract class Entity implements Comparable<Entity> {
checkWorldBoundaries();
previousx = x;
previousy = y;
+
+ /* from this point hitBox related updates */
+ if(null == hitBox ){ return;}
+ hitBox = new Polygon(hitBoxOrig);
+ Transform trans = new Transform(Transform.createTranslateTransform(x, y),Transform.createScaleTransform(scale, scale));
+ //can't sync with animations
+ trans.concatenate(Transform.createRotateTransform(angle));
+ hitBox = new Polygon(hitBox.transform(trans).getPoints());
+
}
/**
@@ -237,8 +246,10 @@ public abstract class Entity implements Comparable<Entity> {
g.rotate(x, y, angle);
}
anim.draw(xpos, ypos, w * scale, h * scale, color);
- if (angle != 0)
+
+ if (angle != 0){
g.resetTransform();
+ }
} else if (currentImage != null) {
currentImage.setAlpha(color.a);
int w = currentImage.getWidth() / 2;
@@ -254,24 +265,28 @@ public abstract class Entity implements Comparable<Entity> {
currentImage.setRotation(angle);
}
if (scale != 1.0f) {
- if (centered)
+ if (centered){
g.translate(xpos - (w * scale - w), ypos - (h * scale - h));
- else
+ }else{
g.translate(xpos, ypos);
+ }
g.scale(scale, scale);
g.drawImage(currentImage, 0, 0);
+
} else
g.drawImage(currentImage, xpos, ypos);
if (scale != 1.0f)
g.resetTransform();
}
if (ME.debugEnabled) {
- g.setColor(ME.borderColor);
- Rectangle hitBox = new Rectangle(x + hitboxOffsetX, y
- + hitboxOffsetY, hitboxWidth, hitboxHeight);
- g.draw(hitBox);
+
+ if(null != hitBox){
+ //create a rectangle and print it like world.render does.
+ g.setColor(ME.borderColor);
+ g.draw(hitBox);
+ }
g.setColor(Color.white);
- g.drawRect(x, y, 1, 1);
+ g.drawRect(x , y , 1, 1);
// draw entity center
if (width!=0 && height!=0){
float centerX = x + width/2;
@@ -422,11 +437,14 @@ 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.collidable = true;
+
+ hitBox = new Polygon(new float[]{xOffset,yOffset, //Top-Left
+ xOffset+width, yOffset, //Top Right
+ xOffset+width,yOffset+height,//Bottom-Right
+ xOffset,yOffset+height}); //Bottom-Left
+
+ hitBoxOrig = hitBox.getPoints();
}
/**
@@ -439,6 +457,8 @@ public abstract class Entity implements Comparable<Entity> {
return type.addAll(Arrays.asList(types));
}
+
+
/**
* check collision with another entity of given type
*
@@ -453,15 +473,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 +502,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 +526,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 +554,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
Second patch:
From 91d6b5cfea17ab553f6d2a2551a4ce97effffe10 Mon Sep 17 00:00:00 2001
From: unknown <Sabathorn@.(none)>
Date: Tue, 26 Apr 2011 18:09:28 -0430
Subject: [PATCH 2/2] Scaling and rotating. almost done. tha animations works fine, but cant sync
with other entities yet.
---
src/it/randomtower/engine/entity/Entity.java | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/src/it/randomtower/engine/entity/Entity.java b/src/it/randomtower/engine/entity/Entity.java
index eb5a8b4..59a56cf 100644
--- a/src/it/randomtower/engine/entity/Entity.java
+++ b/src/it/randomtower/engine/entity/Entity.java
@@ -212,8 +212,7 @@ public abstract class Entity implements Comparable<Entity> {
if(null == hitBox ){ return;}
hitBox = new Polygon(hitBoxOrig);
Transform trans = new Transform(Transform.createTranslateTransform(x, y),Transform.createScaleTransform(scale, scale));
- //can't sync with animations
- trans.concatenate(Transform.createRotateTransform(angle));
+ trans.concatenate(Transform.createRotateTransform((float)(angle * Math.PI/360)));
hitBox = new Polygon(hitBox.transform(trans).getPoints());
}
@@ -281,7 +280,6 @@ public abstract class Entity implements Comparable<Entity> {
if (ME.debugEnabled) {
if(null != hitBox){
- //create a rectangle and print it like world.render does.
g.setColor(ME.borderColor);
g.draw(hitBox);
}
--
1.7.3.1.msysgit.0
i couldn't work around that little Sync issue (run the AngleAlphaScaleMoveTest.java) .
Is this code now in the dev branch?
I've tried to merge pull request on this topic but don't work, so reverter it :(
see discussion here: https://github.com/Gornova/MarteEngine/pull/54
Hmm, strange. In your dev branch the Entity class now has a hitbox Rectangle/Shape and that's used. Does that work for you? Additionally does the StarCleaner example still work in your dev branch? In my merged version the Angel just falls through all blocks... Can you please verify IF it is working in your branch or your local version on your hard disk?
sorry, forgot to push on dev branch, now it's all ok on that branch!
Damned...more work...but I'm progressing with the merge. StarCleaner already worked last night with my Entity and World classes. Will check against your dev code again. Might have to wait until the weekend. We have visitors at home for the week - so I need to be social ;-)
be social! :D But continue on merge, I'll wait it :D
Is there any progress on the polygon hitboxes, and/or the rotation of hitboxes?
Sorry, at least not from my side. Working on anything games related is a bit short right now :-(
Stef, can you try this?
Well there is a problem, the patches above create a new hitbox in each update iteration. To prevent this let's just create a new hitbox when the entity moves/rotates/scale. But these vars(x,y,angle,scale) are public. Is it ok to make them all private? This is a big change since so many classes use the x,y already...
Yes, do it. It's right place on setX, setY, setAngle and scale to calculate hitbox
This means changing 50 files that use entity.x and entity.y, I feel uncomfortable making such a change.