Data Realms Fan Forums
http://forums.datarealms.com/

Rotation math
http://forums.datarealms.com/viewtopic.php?f=73&t=45589
Page 1 of 1

Author:  4zK [ Thu Mar 13, 2014 8:41 pm ]
Post subject:  Rotation math

I'm looking to make a missile turn slightly upwards on creation, relative to the angle it is launched in. What I'm going for is complete horizontal angle being the maximum (slight rotation upwards), and no rotation applied when shot directly upwards or downwards (and relative angles in between). How should I start?

I could go as fancy as taking gravity to count, but I wont go that far for now.

Author:  Asklar [ Thu Mar 13, 2014 11:40 pm ]
Post subject:  Re: Rotation math

Alright, if I understood well, you could do this in many ways, but two in particular come to mind. First one would be the simplest, which would be giving a script to the rocket that would change it's RotAngle by the factor you want if it's RotAngle is different than Pi/2 or Pi*3/2 (don't remember if it's measured in radians or not, I think it is).

So the code would basically look like this:
Code:
function Create(self)
   local pi = 3.14159265

   if self.HFlipped == false then
      self.reverseCheckNum = 1
   else
      self.reverseCheckNum = -1
   end

   if self.RotAngle ~= pi/2 or self.RotAngle ~= pi*3/2 then
      self.RotAngle = self.RotAngle + factor*self.reverseCheckNum
   end
end


The other one would be attaching the script to the gun and make it create the missile with your preferential angles/speeds and other things based on the gun's RotAngle and what not.

Something like:
Code:
function Update(self)

   local pi = 3.14159265

   if self.HFlipped == false then
      self.reverseCheckNum = 1
   else
      self.reverseCheckNum = -1
   end

   if self.lastRound == 0 then
      self.lastRound = self.Magazine.Capacity
   end

   if self:IsActivated() and not self:IsReloading() and self.Magazine.RoundCount ~= self.lastRound then
      self.missile = CreateAEmitter("My missile goes here","HereGoesMod.rte")
      if self.RotAngle ~= pi/2 or self.RotAngle ~= pi*3/2 then
         self.missile.Vel = self.Vel+Vector(10*self.reverseCheckNum,0)
         self.missile.Pos = self.MuzzlePos
         MovableMan:AddParticle(self.missile)
      else
         self.missile.Vel = self.Vel+Vector(10*self.reverseCheckNum,0):RadRotate(self.RotAngle+(factor*self.reverseCheckNum))
         self.missile.Pos = self.MuzzlePos
         MovableMan:AddParticle(self.missile)
      end
      self.lastRound = self.Magazine.RoundCount
   end
end


Then you make a little formula for factor, something like local factor = self.RotAngle/10 or something among those lines. The last one basically makes the gun shoot almost completely thanks to lua, and whatever the weapon fires should be a null particle or some invisible pixel with 0.00000001 mass and 0 sharpness with X velocity in order to simulate the actual round shot, so that the AI can fire it properly.

Now, I don't think I might have understood your idea completely or if my code is completely right (though it should, since most are parts ripped from my own little personal mods that work), but :grin:

Author:  TheLastBanana [ Sat Mar 15, 2014 10:49 pm ]
Post subject:  Re: Rotation math

I would avoid checking whether your missile's angle is equal (or not equal) to a specific number -- floating-point numbers don't handle equality tests very well, so
Code:
floatVar == pi

for example will almost never return True.

Instead, you probably want to check if the number is either less than or greater than the target value and act accordingly.

Author:  Asklar [ Sat Mar 15, 2014 11:11 pm ]
Post subject:  Re: Rotation math

TheLastBanana wrote:
I would avoid checking whether your missile's angle is equal (or not equal) to a specific number -- floating-point numbers don't handle equality tests very well, so
Code:
floatVar == pi

for example will almost never return True.

Instead, you probably want to check if the number is either less than or greater than the target value and act accordingly.


Yeah, I kind of thought that after posting the message but I forgot to change it. I had that problem when doing my helicopter and checked the interval instead, so I'm not sure why I even said that in the first place.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/