Hi ya'll. Working on my first Box2DAS3 app and I have a misalignment between my costumes and my actors which seems to be caused by real number arithmetic tolerance errors. Here's a Cabin link to show you what I mean.
http://www.swfcabin.com/open/1263139802
Click in the game window to get the keystrokes registered, then use left and right arrows to rotate the maze...and the sprites become misaligned within one revolution. And they don't correct themselves when the direction of rotation is changed. Here's the relevant code:
I can't think of a solution to the misalignment...well, one solution....if I had the vertices of the b2Shapes I could redraw the sprites in the correct position every frame (although that seems excessively processor intensive as the maze gets more complex). Can I get those vertices from the b2Body's shapelist?
EDIT:Sorry about the work ya'll had to do because of the forum/wiki hacker!
http://www.swfcabin.com/open/1263139802
Click in the game window to get the keystrokes registered, then use left and right arrows to rotate the maze...and the sprites become misaligned within one revolution. And they don't correct themselves when the direction of rotation is changed. Here's the relevant code:
Code:
package
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import fl.motion.MatrixTransformer;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.display.Sprite;
import flash.geom.Point;
public class MazeActor extends Sprite
{
var costume:Sprite;
var shape:b2Shape;
var previousAngle:Number = 0.0;
public function MazeActor(parent:DisplayObjectContainer, parentBody:b2Body, objectDefArray:Array)
{
var i:int = 0;
var startPoint:Point = new Point();
var obj:*;
objectDefArray.reverse();
var objectShapeDef:b2PolygonDef = new b2PolygonDef();
objectShapeDef.density = 0.0;
objectShapeDef.friction = 1.0;
objectShapeDef.restitution = 1.0;
graphics.beginFill(objectDefArray[0]);
i = 0;
do {
obj = objectDefArray.pop();
if (obj is Point)
{
b2Vec2(objectShapeDef.vertices[i]).Set(obj.x / Physical.RATIO, obj.y / Physical.RATIO);
if (i) graphics.lineTo(obj.x + 214.0, obj.y + 220.0);
else
{
startPoint = obj;
graphics.moveTo(obj.x + 214.0, obj.y + 220.0);
}
}
else
{
graphics.lineTo(startPoint.x + 214.0, startPoint.y + 220.0);
graphics.endFill();
}
i++;
} while (obj is Point);
objectShapeDef.vertexCount = i - 1;
shape = parentBody.CreateShape(objectShapeDef);
costume = this;
parent.addChild(this);
}
public function Update(allMazeActors:Array, mazeBody:b2Body):void
{
UpdateCostume(mazeBody);
}
private function UpdateCostume(mazeBody:b2Body):void
{
var m:Matrix = this.transform.matrix;
MatrixTransformer.rotateAroundExternalPoint(m, 214.0, 220.0, (mazeBody.GetAngle() - previousAngle) * 180.0 / Math.PI);
this.transform.matrix = m;
previousAngle = mazeBody.GetAngle();
}
}
}
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import fl.motion.MatrixTransformer;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.display.Sprite;
import flash.geom.Point;
public class MazeActor extends Sprite
{
var costume:Sprite;
var shape:b2Shape;
var previousAngle:Number = 0.0;
public function MazeActor(parent:DisplayObjectContainer, parentBody:b2Body, objectDefArray:Array)
{
var i:int = 0;
var startPoint:Point = new Point();
var obj:*;
objectDefArray.reverse();
var objectShapeDef:b2PolygonDef = new b2PolygonDef();
objectShapeDef.density = 0.0;
objectShapeDef.friction = 1.0;
objectShapeDef.restitution = 1.0;
graphics.beginFill(objectDefArray[0]);
i = 0;
do {
obj = objectDefArray.pop();
if (obj is Point)
{
b2Vec2(objectShapeDef.vertices[i]).Set(obj.x / Physical.RATIO, obj.y / Physical.RATIO);
if (i) graphics.lineTo(obj.x + 214.0, obj.y + 220.0);
else
{
startPoint = obj;
graphics.moveTo(obj.x + 214.0, obj.y + 220.0);
}
}
else
{
graphics.lineTo(startPoint.x + 214.0, startPoint.y + 220.0);
graphics.endFill();
}
i++;
} while (obj is Point);
objectShapeDef.vertexCount = i - 1;
shape = parentBody.CreateShape(objectShapeDef);
costume = this;
parent.addChild(this);
}
public function Update(allMazeActors:Array, mazeBody:b2Body):void
{
UpdateCostume(mazeBody);
}
private function UpdateCostume(mazeBody:b2Body):void
{
var m:Matrix = this.transform.matrix;
MatrixTransformer.rotateAroundExternalPoint(m, 214.0, 220.0, (mazeBody.GetAngle() - previousAngle) * 180.0 / Math.PI);
this.transform.matrix = m;
previousAngle = mazeBody.GetAngle();
}
}
}
I can't think of a solution to the misalignment...well, one solution....if I had the vertices of the b2Shapes I could redraw the sprites in the correct position every frame (although that seems excessively processor intensive as the maze gets more complex). Can I get those vertices from the b2Body's shapelist?
EDIT:Sorry about the work ya'll had to do because of the forum/wiki hacker!