View Full Version : Moving an object diagonally
I have made a diagram as to represent what Im looking to do.
what I'd like to do: move and object diagnally (using x and y increments)
what's known at the start: the angle
In the end I would like to figure out how to move a movieclip down (or up) on a diagnal axis (not a 90 degree or 45 degree movement). I know there has to be a way using math to figure out how many pixels for x and y to make the object appear to move on a diagonal axis. Knowing the angle (in this case 50 degrees) does anyone know an equation?
theqman
07-19-2008, 09:21 AM
Not sure if this is what you are looking for but you could use
Object.x += 45;
Object.y += angle;
that should work. If its too much speed just divide both by the same amount.
EDIT: put that on enter frame or w/e
I dont think thats what I am looking for... It has to move as though its on a track..... right now the x and y are not proportionate to eachother..... if this were a line that was at a 45 degrees angle I would say:var diagMoveTimer:Timer=new Timer(30);
var angle:Number = 50;
diagMoveTimer.addEventListener(TimerEvent.TIMER, diagMove);
function diagMove(event:TimerEvent):void {
diagLine_mc.x += 5;
diagLine_mc.y += 5;
}
diagMoveTimer.start();And you would see it move straight.
Now since it is not a nice angle such as 45, how do you figure out the different values for x and y since they will not be equal?
Xegnma
07-21-2008, 04:10 AM
Given a circle of radius 'r' and a fixed angle 'theta' we can
find a point on the circle corresponding to 'r' and 'theta'
using the following equations:
x = r * Math.cos(theta);
y = r * Math.sin(theta);
To move the point along "a track", simply change the radius
keeping the angle fixed. :)
So in your case you might do something like the following:
var radius:uint = 0;
var xOffset:uint = startingX;
var yOffset:uint = startingY;
var angle_in_radians:uint = angle_in_degrees * Math.PI/180;
function diagMove(event:TimerEvent): void {
radius += 5;
_mc.x = radius * Math.cos(angle_in_radians) + xOffset;
_mc.y = radius * Math.sin(angle_in_radians) + yOffset;
}
I suggest brushing up on your trig. ;)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.