Away3D HoverCamera Pan Angle - relative angle out of 360 - actionscript-3

I'm trying to work out the percentage of rotation of the HoverCamera but .panAngle() returns any number from -infinity to + inifinity, is there a way to work out the current angle out of 360 instead, no matter how times you rotate the camera?

For the mod operator in AS3 you need to check for negative values too:
angle = angle % 360;
if(angle<0) angle+=360;

Related

How can I make a symbol point at the mouse?

I want to make a symbol rotate to point at the mouse. I'm using this function, but it doesn't work below the symbol's pivot. The inverse tan function has a range of 180 degrees right? So how can i get 360 degrees of movement?
Would I need to add an if statement to check the mouse position or is there a more elegant solution?
function panelTrack(){
angle = -180/Math.PI * Math.atan((mouseX - panel.x)/(mouseY - panel.y));
panel.rotation = angle;
trace(panel.rotation);
}
Math isn't my strong point - so perhaps someone else will provide a better answer, but to get all 4 quadrants, you need to use atan2.
angle = Math.atan2(mouseY - panel.y, mouseX - panel.x) * 180 / Math.PI;
I seem to recall it has to do with a check for a value of 0 (that Math.atan doesn't do).
const radiance:Number=180/Math.PI;
angle=-(Math.atan2(mouseX-panel.x, mouseY-panel.y))*radiance;
I used minus because usually the orientation is reverse when you don't add minus.
hope this helps.

Flash crash after matrix rotation

In my Flash program I have a step where I want to rotate a displayObject around the center of its container.
As some of you may know, Flash has a default center point for rotation which is the top left corner, and doesn't fit for my case.
To achieve my specific rotation, I do 3 successive transformations using matrices, like this:
public function rotateAroundCenter(object:DisplayObject, container:DisplayObject, angleDegrees:Number):void {
var matrix:Matrix = object.transform.matrix;
var rect:Rectangle = object.getBounds(container);
matrix.translate(-(rect.left + (rect.width / 2)), -(rect.top + (rect.height / 2)));
matrix.rotate((angleDegrees / 180) * Math.PI);
matrix.translate(rect.left + (rect.width / 2), rect.top + (rect.height / 2));
object.transform.matrix = matrix;
}
This bit of code does the trick and I can rotate displayObjects around their container center like I want to.
Problem: For some of these objects (couldn't find a discriminating factor between those who work and those who don't), any time I try to apply a 180 degrees rotation to put them upside down using previous bit of code, Flash unloads the SWF, which seems very much like a crash to me. I only get this crash for some of these objects, but if they crash once they crash anytime I apply a 180 degrees rotation using my function.
I suspect a memory leak, but then, if 90 or 270 degrees rotations work, why would this specific case make my whole program crash?
Any clues about this issue will be very appreciated. Thanks!

Plotting polyline in html5 canvas with both positive and negative y axis points

I have to plot a polyline in HTML5 canvas where y axis points are both positive and negative.
such as y=[5,10,50,-150,80,-90]. The x-axis is spaced at 1. x=[1,2,3,4,5,6].
I am trying to use translate() and scale() to plot this, but so far not successful.
If someone could point how both positive and negative points be plotted in canvas it will be great.
Suppose your canvas is 1000 by 1000 and you want to plot points that are 0 to 7 in the x and -200 to 100 in the y. Then you need to do two things:
1) Recenter so that the origin is at (0, 333.3) in pixel coordinates. You would do this with translate(0, 333.3).
2) Scale so that an x of 7 appears at pixel position 1000 and y of 100 appears at pixel position -333.3 (now the top of the window). You would do this with scale(1000 / 7, -333.3 / 100).
More generally, for canvas size (w, h) and axis ranges xmin...xmax and ymin...ymax, you would do:
ctx.translate(w * xmax / (xmax - xmin), h * ymax / (ymax - ymin));
ctx.scale(w / (xmax - xmin), -h / (ymax - ymin));

Movieclip stops rotating after it's Box2D Body

I have made a Car Game using Box2D [in Flash] and I have one remaining bug, which I cannot fix. I added graphics and put them on top of the Box2D body. Everything went as good as expected, but after X rotations the movie clips for the car-wheels, stop spinning. I do something like this wheelSprite.rotation = wheelBody.GetAngle() * 180 / Math.PI. I ran a separate program and I saw that, if you do X.rotation += variable and you increase the variable every frame, after ~30 000 (value of variable) the MovieClip stops rotating, so I reset it to 0 after ~28 000. What do I do? The wheelBody.GetAngle() keeps going up, and I need it to make it look real. How do I reset it?
I faced this problem some time ago. The solution was:
rotation = newRotation % 2*Math.PI;
Which means that rotation must be between 0 and 360 degrees (0 - 2*PI).
Remainder solve this issue:
yourMC.rotation = (yourMCbody.GetAngle() * 180 / Math.PI) % 360;
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#modulo
Can't you use the SetAngle() function to set the angle to an equivalent angle?
ie: If the angle rotates over 360 degrees, set it back to 0?
The Box2D manual specifies that the rotation of bodies is unbounded and may get big after awhile and that you can you can reset it using SetAngle.
I use a while loop to calculate the normalized angle (you can apparently call modulo with a floating point operand but I don't know if that is bad for performance). This code normalizes the angle to 0 <= angle < 2pi but I've seen angles sometimes normalized to -pi <= angle < pi as well.
const 2PI:Number = Math.PI * 2;
var rotation:Number = wheelBody.GetAngle();
// normalize angle
while(rotation >= 2PI)
rotation -= 2PI;
while(rotation < 0)
rotation += 2PI;
// store the normalized angle back into Box2D body so it doesn't overflow (optional)
wheelBody.SetAngle(rotation);
// convert to degrees and set rotation of flash sprite
wheelSprite.rotation = rotation * 180 / Math.PI;
I haven't collected the code into a function but it would be easy to do so.

AS3: angle of reflection

I am stumped on working out the angle of reflection in my AS3 project, I have the formula but the formula relies on you knowing the angle of the object that is reflected against (to work out the perpendicular angle) the trouble is the objects are all at 0 rotation but are shaped differently (e.g the four sides of the stage have a line across them which is at 0 rotation but some are horizontal and some vertical).
How would I work out the angle of reflection purely from the angle of the object that is being reflected, taking into account AS3 returns rotation values between -180 and 180.
Anyone got an idea?
This doesn't answer my question so much as it only works on objects reflecting of horizontal or vertical objects but this function will get you the angle of reflection based upon the rotation of the object which is being reflected...
var reflection:int = (incidence > 0) ? 180 - incidence: -180 - incidence;
where incidence which is the rotation of the object which is being reflected.