Rotate and Move - Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Rotate and Move

Member
edited February 2013
Trying to Rotation a sprite, simple enoughm, I placed it into a movieClip and it flies, putting tweening in place, slides it nicely,

Now I am Rotating the sprite and want to move in the direction the front of the sprit eis facing. and I am getting a little mixed up.

For example. I have the rotation example:
dragon:setRotation(75)
Then I have the position
dragon:setPosition(100,100)

Whats the best way to update the X,Y with the new position based on the Angle? is there a Tweening way to do this? or do I need to place the value updates into an event? and whats the best method to update the X,Y coordinates based on Angular calculation?

Regards

Likes: hosamred

Dislikes: clement

REAL programmers type copy con filename.exe
---------------------------------------
+1 -1 (+1 / -1 ) Share on Facebook

• Guru
You need to calculate a new x,y offset from your current position using basic 2D trig. Something like
 ```local cx,cy -- current x, y pos local xo,yo -- x, y offset local angle -- current facing angle local speed -- movement speed / distance to move   xo = math.sin(math.rad(angle)) * speed yo = 0 - math.cos(math.rad(angle)) * speed cx = cx + xo cy = cy + yo   self:setRotation(angle) self:setPosition(cx,cy)```
I might have got the sin / cos the wrong way round - can never remember (also yo is negative because y=0 is top left not bottom left), and don't forget that sin / cos work in radians not degrees, so you might want to calc that first.
WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
• Member
edited February 2013
Update, since I got it working, here is my working prototype. Angle of direction of the sprite/movieclip,and the x,y potion update based on that angle, so others should find it easy to follow and make changes, such as randomising the position or angles.
 ```function MovieSprite:running(event) local cx, cy = self:getX() ,self:getY() -- current x, y pos local xo,yo -- x, y offset self.speed = self.speed or 1 -- movement speed / distance to move local Radtheta = self.angle * math.pi/180 xo = math.cos(Radtheta) * self.speed yo = math.sin(Radtheta) * self.speed cx = cx + xo cy = cy + yo   self:setRotation(self.angle) self:setPosition(cx,cy)   end```
All I want to figure out now is if there is a way to make this work with the tween, Rather than have a specific location to fly to, just to travel in random patterns in a rough direction
REAL programmers type copy con filename.exe
---------------------------------------
• Guru