CrazyM Posted September 17, 2014 Share Posted September 17, 2014 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 Quote CrazyMinnowStudio.com Youtube, Twitter, Facebook, G+ Link to comment Share on other sites More sharing options...
Guppy Posted September 17, 2014 Share Posted September 17, 2014 Isn't that what curve / curveangle is for? http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/math/mathcurveangle-r603 Quote System: Linux Mint 17 ( = Ubuntu 14.04 with cinnamon desktop ) Ubuntu 14.04, AMD HD 6850, i5 2500k Link to comment Share on other sites More sharing options...
CrazyM Posted September 17, 2014 Author Share Posted September 17, 2014 Math:CurveAngle seems to do the same thing as the Math:Inc method I'm currently using, but with a difference scale on the step value. I was hoping there might be a cleaner approach than the one I'm using already. Thanks Quote CrazyMinnowStudio.com Youtube, Twitter, Facebook, G+ Link to comment Share on other sites More sharing options...
CrazyM Posted September 17, 2014 Author Share Posted September 17, 2014 The main issue is with detecting an entities arrival at its target rotation so the rotate flag can be disabled, because the rotation float values are usually something like 89.38285 instead of 90. I tried Math:Round, but this still seems like an unreliable solution. Quote CrazyMinnowStudio.com Youtube, Twitter, Facebook, G+ Link to comment Share on other sites More sharing options...
Rick Posted September 17, 2014 Share Posted September 17, 2014 You can do > or < checks and snap to a desired angle if the checks are valid. Quote Link to comment Share on other sites More sharing options...
Ma-Shell Posted September 18, 2014 Share Posted September 18, 2014 Just replace if Math:Round(self.selected:GetRotation(true).x) == self.rotAngle.x By if Math:Abs(self.selected:GetRotation(true).x - self.rotAngle.x) < threshold Where you have to choose an acceptable threshold 1 Quote Link to comment Share on other sites More sharing options...
CrazyM Posted September 18, 2014 Author Share Posted September 18, 2014 @Rick - I will likely use the snapping idea with Ma-Shell's elegant proximity test to keep entities aligned. @Ma-Shell - That's a great idea, very clean! Thanks guys! Quote CrazyMinnowStudio.com Youtube, Twitter, Facebook, G+ Link to comment Share on other sites More sharing options...
Recommended Posts
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.