Jump to content

Best method for smooth rotation over time to a specific angle


CrazyM
 Share

Recommended Posts

Can anyone recommend the best method to rotate an entity in global space, over time, to a specific angle (90 degree increments)?

 

I wrote a bit that does this, but it feels like bloated attempt and has some issues when it comes to detecting when the rotation has arrived at it's destination due to float value variations.

 

In the example below, a button click sets the rotation in motion with (self.rotateX = true), and increments the first step in the rotation with (self.rotateSpeed). In the update function, the current global x rotation is compared to the target (self.rotAngle.x) rotation, and the rotation should continue until the global and target rotations match. At this point, the rotateX flag is disabled (self.rotateX = false).

 

function Script:UpdateWorld()
   -- Attempting to detect when the target rotation has been reached
   if Math:Round(self.selected:GetRotation(true).x) ~= self.rotAngle.x and self.rotateX == true then
    self.newRot = self.selected:GetRotation(true)
    self.newRot.x = Math:Inc(self.rotAngle.x, self.selected:GetRotation(true).x, self.rotateSpeed)
    self.selected:SetRotation(self.newRot, true)
    if Math:Round(self.selected:GetRotation(true).x) == self.rotAngle.x and self.rotateX == true then
	    self.rotateX = false
	    System:Print("X rotation complete")
    end
   end
end

function Script:ButtonClick(mPos)
   if self:IsInRect(mPos, self.upRect) == true then
    self.rotateX = true
    self.rotAngle = self.selected:GetRotation(true) + Vec3(90,0,0)
    self.newRot.x = Math:Inc(self.rotAngle.x, self.selected:GetRotation(true).x, self.rotateSpeed)
    self.selected:SetRotation(self.newRot, true)
   end
end

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...