Permalink
Browse files

sanitised versions of DrawableNode and AnimatedImageNode and added fl…

…ip and fixed movement bounding box update bugs
  • Loading branch information...
1 parent e482e6b commit be9e90488ef552ffb4c4d5ba9e0e98db2057a4dd @pixelpusher committed May 24, 2012
@@ -150,9 +150,9 @@ class AnimatedImageNode extends DrawableNode
{
int currentCol = spriteIndex % cols;
int currentRow = spriteIndex/cols;
-
+
imgX = currentCol*imgW;
- //println("x=" +imgX);
+ //println("x=" +imgX);
imgY = currentRow*imgH;
}
@@ -248,6 +248,20 @@ class AnimatedImageNode extends DrawableNode
renderer.noStroke();
}
+ renderer.pushMatrix();
+ //rectMode(CORNER);
+ //rect(pos.x, pos.y, w,h);
+ if (rotationSpeed == 0f)
+ {
+ renderer.translate(pos.x, pos.y);
+ }
+ else
+ {
+ Vec2D m = middle();
+ renderer.translate(m.x, m.y);
+ renderer.rotate(rotation);
+ renderer.translate(-w/2, -h/2);
+ }
//imgX, imgY, imgW, imgH
@@ -257,20 +271,33 @@ class AnimatedImageNode extends DrawableNode
renderer.textureMode(IMAGE);
renderer.beginShape();
renderer.texture( img );
- renderer.vertex(pos.x, pos.y, imgX, imgY);
+
+ float tx0=imgX;
+ float tx1=imgX+imgW-1;
+
+ if (flipped)
+ {
+ tx0 += imgW-1;
+ tx1 = imgX;
+ }
+ renderer.vertex(0, 0, tx0, imgY);
// subtract 1px from width because of Processing bug?
- renderer.vertex(pos.x+w, pos.y, imgX+imgW-1, imgY);
- renderer.vertex(pos.x+w, pos.y+h, imgX+imgW-1, imgY+imgH);
- renderer.vertex(pos.x, pos.y+h, imgX, imgY+imgH);
+ renderer.vertex(w, 0, tx1, imgY);
+ renderer.vertex(w, h, tx1, imgY+imgH);
+ renderer.vertex(0, h, tx0, imgY+imgH);
+
renderer.endShape();
+
+ renderer.popMatrix();
}
void unload()
{
img = null;
currentSprite = null;
+ super.unload();
}
// end class
@@ -65,6 +65,7 @@ void draw()
// if we hit the sprite with the mouse, knock it around and start animating
if (sprites[i].pointInside(mouseX, mouseY))
{
+ sprites[i].rotationSpeed = PI/10f;
sprites[i].accelerate((new Vec2D(mouseX-pmouseX, mouseY-pmouseY)).limit(speedLimit));
sprites[i].start(); // start animating
}
@@ -28,6 +28,9 @@ class DrawableNode
Vec2D vel; // velocity
Vec2D accel; // acceleration
+ float rotation; // z rotation, clockwise in radians
+ float rotationSpeed; // z rotation speed per frame, clockwise in radians
+
// drawing variables
boolean hasStroke = false;
color strokeColor = color(0);
@@ -54,6 +57,8 @@ class DrawableNode
vel = new Vec2D();
accel = new Vec2D();
+ rotationSpeed = 0f;
+
data = new HashMap<String, Object>();
updateBoundingBox();
@@ -68,6 +73,8 @@ class DrawableNode
vel = new Vec2D();
accel = new Vec2D();
+ rotationSpeed = 0f;
+
data = new HashMap<String, Object>();
w = _w;
@@ -79,13 +86,15 @@ class DrawableNode
void setX(float _x)
{
+ prevPos.x = _x;
pos.x = _x;
minX = pos.x;
maxX = pos.x + w;
}
void setY(float _y)
{
+ prevPos.y = _y;
pos.y = _y;
minY = pos.y;
maxY = pos.y + h;
@@ -169,6 +178,14 @@ class DrawableNode
finishedMoving();
}
+ if (abs(rotationSpeed) > PI/60f)
+ {
+ rotation += rotationSpeed;
+ rotationSpeed *= frictionCoeff;
+ }
+ else
+ rotationSpeed = 0f;
+
if (wrap)
{
if (pos.x < 0) setX(width-w-1);
@@ -185,17 +202,16 @@ class DrawableNode
void moveTo(float x, float y)
{
- pos.set(x, y);
prevPos.set(pos);
- updateBoundingBox();
+ setX(x);
+ setY(y);
}
void move(float x, float y)
{
- pos.addSelf(x, y);
prevPos.set(pos);
-
+ pos.addSelf(x, y);
updateBoundingBox();
}
@@ -225,12 +241,23 @@ class DrawableNode
}
// you can change this in subclasses to make custom objects
-
- renderer.rectMode(CORNERS);
- renderer.rect(minX, minY, maxX, maxY);
- }
-
+ //rectMode(CORNER);
+ //rect(pos.x, pos.y, w,h);
+ if (rotationSpeed == 0f)
+ {
+ renderer.rectMode(CORNERS);
+ renderer.rect(minX, minY, maxX, maxY);
+ }
+ else
+ {
+ Vec2D m = middle();
+ renderer.translate(m.x, m.y);
+ renderer.rectMode(CENTER);
+ renderer.rotate(rotation);
+ renderer.rect(0, 0, w, h);
+ }
+ }
// simple rectangluar boundary hit test
@@ -150,9 +150,9 @@ class AnimatedImageNode extends DrawableNode
{
int currentCol = spriteIndex % cols;
int currentRow = spriteIndex/cols;
-
+
imgX = currentCol*imgW;
- //println("x=" +imgX);
+ //println("x=" +imgX);
imgY = currentRow*imgH;
}
@@ -248,6 +248,20 @@ class AnimatedImageNode extends DrawableNode
renderer.noStroke();
}
+ renderer.pushMatrix();
+ //rectMode(CORNER);
+ //rect(pos.x, pos.y, w,h);
+ if (rotationSpeed == 0f)
+ {
+ renderer.translate(pos.x, pos.y);
+ }
+ else
+ {
+ Vec2D m = middle();
+ renderer.translate(m.x, m.y);
+ renderer.rotate(rotation);
+ renderer.translate(-w/2, -h/2);
+ }
//imgX, imgY, imgW, imgH
@@ -257,20 +271,33 @@ class AnimatedImageNode extends DrawableNode
renderer.textureMode(IMAGE);
renderer.beginShape();
renderer.texture( img );
- renderer.vertex(pos.x, pos.y, imgX, imgY);
+
+ float tx0=imgX;
+ float tx1=imgX+imgW-1;
+
+ if (flipped)
+ {
+ tx0 += imgW-1;
+ tx1 = imgX;
+ }
+ renderer.vertex(0, 0, tx0, imgY);
// subtract 1px from width because of Processing bug?
- renderer.vertex(pos.x+w, pos.y, imgX+imgW-1, imgY);
- renderer.vertex(pos.x+w, pos.y+h, imgX+imgW-1, imgY+imgH);
- renderer.vertex(pos.x, pos.y+h, imgX, imgY+imgH);
+ renderer.vertex(w, 0, tx1, imgY);
+ renderer.vertex(w, h, tx1, imgY+imgH);
+ renderer.vertex(0, h, tx0, imgY+imgH);
+
renderer.endShape();
+
+ renderer.popMatrix();
}
void unload()
{
img = null;
currentSprite = null;
+ super.unload();
}
// end class
@@ -63,6 +63,8 @@ class DrawableNode
vel = new Vec2D();
accel = new Vec2D();
+ rotationSpeed = 0f;
+
w = _w;
h = _h;
@@ -140,7 +142,12 @@ class DrawableNode
void update()
{
- rotation += rotationSpeed;
+ if (abs(rotationSpeed) > EPSILON)
+ {
+ rotation += rotationSpeed;
+ }
+ else
+ rotationSpeed = 0f;
if (moving)
{
@@ -183,11 +190,20 @@ class DrawableNode
void moveTo(float x, float y)
{
prevPos.set(pos);
- pos.set(x, y);
+ setX(x);
+ setY(y);
+ }
+
+
- // update minX, maxX, etc
+ void move(float x, float y)
+ {
+ prevPos.set(pos);
+ pos.addSelf(x, y);
+ updateBoundingBox();
}
+
/// draw /////////////////////////////////////////////
//////////////////////////////////////////////////////
@@ -231,8 +247,6 @@ class DrawableNode
}
-
-
// simple rectangluar boundary hit test
boolean intersects(DrawableNode other)
{
@@ -247,8 +261,18 @@ class DrawableNode
}
+ boolean pointInside(float x, float y)
+ {
+ if (x < minX || x > maxX) return false;
+ if (y < minY || y > maxY) return false;
+
+ return true;
+ }
+
+
void unload()
{
+ data.clear();
}
void finishedMoving()
Oops, something went wrong.

0 comments on commit be9e904

Please sign in to comment.