Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Rotate and Move — Gideros Forum

Rotate and Move

CyberienceCyberience Member
edited February 2013 in General questions
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

Comments

  • techdojotechdojo Guru
    Accepted Answer
    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
  • CyberienceCyberience 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
    ---------------------------------------
  • techdojotechdojo Guru
    Accepted Answer
    If you mean do you want to pick some random coord on the screen, and have the sprite move to that location?

    If so the process is a little different, as you already know the position, it's the angle you need to calculate.

    What you do is pick a position on screen relative to your current position, then use the math.atan2() function with the relative offset, this will return the angle from where you are now to where you want to go.

    You should be able to plug these numbers into a MovieClip object and have your sprite automatically update itself as the MovieClip runs, failing that you just perform the movement each step as above.

    Note - Once you know the x,y offsets you only need to calculate the direction angle once (it'll never change - although the "facing" direction will if you want to rotate smoothly) and also the x,y frame step value is simply the offset (distance to move) / number of frames to give a constant speed across the distance.
    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
Sign In or Register to comment.