AS3 Rotate 3D object around point in space - actionscript-3

I use
appendRotation(_Y, Vector3D.Y_AXIS);
to rotate an object around its center. works fine.
when I want the object to rotate around the center of scene, I use
prependRotation( _Y, Vector3D.Y_AXIS , new Vector3D( 0, 0, 0 ) );
works fine.
but if I want to rotate object around some point in space like this:
prependRotation( _Y, Vector3D.Y_AXIS , new Vector3D( 10, 10, 10 ) );
it doesn't work, the object gets totaly distorted, can anyone give me a hint, what am I doing wrong?

It could be resolved by adding your object into parent container. then
move object to proper position: appendTranslation(10, 10, 10), and then apply rotation to parent container: appendRotation(_Y, Vector3D.Y_AXIS).

Related

Libgdx / Physics Editor Rotation of body

and a nice evening.
I've the following problem:
A Box2d dynamic body with one fixture all vertices placed with positiv x / y from the origin of the body.
http://i.stack.imgur.com/MXQRr.png
But the rotation is on body origin 0/0 not on mass center..
So I tried these approaches:
1:
Set origin of Body in the Middle of the body(vertices positive and negativ)
Problem: rotation works, Sprite positioning is nearly impossible
2:
Set mass data center =
originOfBody.x + width / 2, originOfBody.y + height / 2
Problem: It's like I'm doing nothing, still rotation around origin of body at 0, 0
In the following Code snippet you can see my instantiation.
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(new Vector2(200, 2));
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 0.6f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.2f;
bodyLoader.attachFixture(body, "Rocket1n2", fixtureDef, WIDTH);
originOfBody = bodyLoader.getOrigin("Rocket1n2", WIDTH);
MassData data = body.getMassData();
data.center.set(Box2DUtils.getWidth(body) / 2, 0);
data.I = body.getMassData().I; //without this line programm fails with assertion
body.setMassData(data);
Vector2 cpy = data.center.cpy();
I rotate the player with following code:
body.setAngularVelocity(addition); //addition is predefined(atm 1 / -1)
I currently use Body Physics Editor from aurelia(?) with some hacks it worked, I dont have any Problems with other objects, because I dont rotate them. Placement works like a charm..
I wish I do a significant error.. Normally box2d must calculate the rotationcenter (Mass Center) automatically?
I cant help myself this time.. :(
I'd recommend getting your first approach to work.
Set origin of Body in the Middle of the body(vertices positive and
negativ) Problem: rotation works, Sprite positioning is nearly
impossible
The rotation works fine, but the sprite positioning is difficult. I had this same problem when I was making a physics game last year. What helped me was seeing exactly how the positioning worked.
Box2D bodies are center-focused. That means that calling the position of the body will give you the object's middle. Sprites are corner focused, meaning a sprite's position is it's bottom-left corner. Thus, you can't just set each of them as the same thing. I recommend shifting the sprite from the body's position down and to the left by half the body's height and width, respectively.

How to make fabric.js canvas center be 0,0?

I'm just starting with fabric.js and I wonder if there is a way to create a canvas in such a way that I can work with cartesian coordinates? For example, I want a circle that I create at (0,0) to show up in the center of my canvas that reaches from -400 to 400 on the x-axis and -400 to 400 on the y.axis, with higher values of y going up and higher values of x going right.
Is there a way to do that without translating every position by hand?
You can use originX and originY attributes.
var obj = canvas.getObjects();
for (var i in obj) {
obj[i].set({
originX: 'left',
originY: 'top',
left: 0,
top: 0
});
}
canvas.renderAll();
Check this fiddle with examples: http://jsfiddle.net/1ow02gea/51/
You cannot change the origin of the canvas element without introducing visual artifacts. What you can do is pan the canvas to a virtual "0,0" position. Bear in mind that you will have to adjust both X_PAN_VALUE and Y_PAN_VALUE to your specific use case.
canvas.viewportTransform[4] = X_PAN_VALUE;
canvas.viewportTransform[5] = Y_PAN_VALUE;
Better late than never ;)

clearRect issue in animation

Im having issue with clearRect, i have an image u can move up and down and which follow the angle where the mousse is but in some frames of the animation the clearRect let a small edge of the previous image state ( 'this' reference to the image and 'ctx' is the 2d context, 'this.clear()' is called each frame before redrawing the image at the new coordinates )
this.clear = function(){
game.ctx.save();
game.ctx.translate(this.x+this.width/2, this.y+this.height/2);//i translate to the old image center
game.ctx.rotate(this.angle);//i rotate the context to the good angle
game.ctx.clearRect(this.width/-2, this.height/-2, this.width, this.height);//i clear the old image
game.ctx.restore();
};
if i replace the clearRect line by
game.ctx.clearRect(this.width/-2-1, this.height/-2-1, this.width+2, this.height+2);
it works but its not the logical way
The problem is that you are only clearing at position half the width/height, not position minus half the width/height.
Regarding anti-aliasing: when you do a rotation there will be anti-aliased pixels regardless of the original position being integer values. This is because after the pixels relative positions are run through the transformation matrix their offsets will in most cases be float values.
Try to change this line:
game.ctx.clearRect(this.width/-2, this.height/-2, this.width, this.height);
to this instead including compensation for anti-aliased pixels (I'll split the lines for clearity):
game.ctx.clearRect(this.x - this.width/2 - 1, /// remember x and y
this.y - this.height/2 - 1,
this.width + 2,
this.height + 2);

Increase width without moving image

I'm developing a drawing app, and I ran into a problem, and a possible solution would be to be able to resize horizontally (increase or decrease the width) of an image without altering it's pivot, but ending with a "directional resize", it means, that if I start dragging the right resize anchor, the image starts increasing it's width to the right, instead of always taking into consideration the pivot.
So, what I'm doing now is to increase the width and at the same time I move the image width/2, it works, however when I have to rotate the image.. everything starts to get broken, and even if I set the pivot in the middle of the sprite, since the image (that is contained inside the sprite) x is altered, it doesn't matter.
I've read some about matrices, but I'm not sure if this is possible.
Thanks.
There a few ways you can accomplish this. The first is to use a transform matrix.
Here is a function I use to rotate an object around a point in that fashion:
*You'll need to import fl.motion.MatrixTransformer, if not using flashPro you can get it here: https://code.google.com/p/artitem-as3/source/browse/trunk/ArtItem/src/fl/motion/MatrixTransformer.as?r=3
/**
* Rotates a displayObject around the passed local point.
* #param displayObj
* #param relativeDegrees - the relative amount of degrees to rotate the object
* #param point - a local point to rotate the object around, if null, center of bounding box will be used.
*/
public static function rotateAroundPoint(displayObj:DisplayObject, relativeDegrees:Number, point:Point = null):void {
var m:Matrix = displayObj.transform.matrix.clone();
if (!point) {
//default is center of bounding box
var r:Rectangle = displayObj.getBounds(displayObj);
point = new Point(r.x + (r.width * .5), r.y + (r.height * .5));
}
MatrixTransformer.rotateAroundInternalPoint(m, point.x, point.y, relativeDegrees);
displayObj.transform.matrix = m;
}
Another more lazy way to do this, is to use a dummy parent object and just rotate that:
var dummy:Sprite = new Sprite();
dummy.addChild(yourObjectToRotate);
//this effectively makes the anchor point of dummy in the center, so when you rotate it it rotate from the center.
yourObjectToRotate.x = -(yourObjectToRotate.width * .5);
yourObjectToRotate.y = -(yourObjectToRotate.height * .5);
dummy.rotation = 90;

AS3 - Painless Simple Animation?

What I have are two MovieClips that I wish to move around the screen (one horizontally and the other vertically) my ideal scenario would be an android type animation like this...
TranslateAnimation moveDown;
if (isMainGuiVisible)
{
moveDown = new TranslateAnimation(0, 0, 0, 150);
}
else
{
moveDown = new TranslateAnimation(0, 0, 150, 0);
}
moveDown.setDuration(time);
moveDown.setFillAfter(true);
frmMainGUI_mc.startAnimation(moveDown);
Where it's doing this...
moveDown = new TranslateAnimation (fromXDelta, toXDelta, fromYDelta, toYDelta)
Edit: I basically need a way to animate a MovieClip moving off and on screen in the direction I tell it to. If that is like above where I'm telling it to move back and forth by 150 pixels that moves it off and on screen or some other way...
Use a tweening library. Greensock's TweenLite is a popular one but there are others (I use GTween generally).
Basic example from the TweenLite page:
TweenLite.to(mc, 1, {x:65, y:117});
Where mc is the display object to tween, 1 is the duration of the tween and x and y are the new x and y positions.