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.
Related
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;
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!
This may be an issue that I simply do no know the proper terminology to research the answer to this, I am pretty sure the solution is a function of trig.
I have a method which accepts an X/Y position coordinate and an angle in degrees. It should return an updated X/Y based on the rotation angle provided.
For example, A point is usually located at x=0,y=2 (top middle). Now I need to rotate it to it's side by 90 degrees. In my mind I know it's location is now x=2,y=0 (middle right) but I do not know the equation to produce this.
I think I need to first determine the quadrant of the starting point, and then perform the proper trig function from there. This is for a game I am developing using libgdx which is where the Vector2 object comes from.
I have come this far:
public Vector2 getPointsRotated(Vector2 startPoint, float angle){
Vector2 newPoint = new Vector2(0,0);
// determine the starting quadrant
int quad=0;
if((startPoint.x>=0)&&(startPoint.y>=0)){quad=0;}
if((startPoint.x<0)&&(startPoint.y>=0)){quad=1;}
if((startPoint.x<0)&&(startPoint.y<0)){quad=2;}
if((startPoint.x>=0)&&(startPoint.y<0)){quad=3;}
if(quad==0){
// doesn't work
newPoint.x = (float) ((newPoint.x)* (Math.sin(angle)));
newPoint.y = (float) ((newPoint.y)* (Math.cos(angle)));
}
// ...
// other quadrants also don't work
// ...
return newPoint;
}
Thanks for any help.
Update:
I have been avoiding and returning to this problem for a few days. Now after finally posting the question here I figure it out within minutes (for ppl using libgdx anyway).
Libgdx provides a rotate function for Vector2s
so something like:
Vector2 position = new Vector2(0,2);
position.rotate(angle);
works perfectly.
I find rotation matrices are very helpful for this sort of problem.
https://gamedev.stackexchange.com/questions/54299/tetris-rotations-using-linear-algebra-rotation-matrices
I'm writing a paint program that uses shape brushes to draw by using the matrix function.
Everything works well aside from the fact that it's not smooth at all. There will be gaps in the painting if the mouse is moved at a high speed.
I've looked everywhere but haven't been able to find any solution.
The code basically looks like this:
//Press mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseDown_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove_drawContainer);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp_drawContainer)
}
//Move mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseMove_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
}
If anyone could help me figure out how to smooth out the drawing, I'd be forever grateful! =p
Thanks in advance.
You probably need some kind of interpolation between the mouse positions... there are of course many ways, I'll describe one very easy to implement but a bit hard to fine tune. Basically instead of drawing in each mouse position, you use an easing equation that follows the mouse with some delay... this way the described line will be a bit smoother, and will draw a few times between each mouse position.
So instead of doing (pseudocode):
onMouseMove {
draw(mouseX, mouseY);
}
You do something like:
x = 0;
y = 0;
onEnterFrame {
x += (mouseX - x) * 0.2;
y += (mouseY - y) * 0.2;
draw(x, y);
}
Although maybe what you really need is a way to limit the maximum distance between points, so if the mouse moves more in one frame, you interpolate points between the two positions and draw as many times as it's needed.
Or if you're looking for smoother lines (avoid sharp corners) maybe you also need to use beziers to control the resulting line.
Anyway, it all depends on the kind of drawing you're looking for.
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.