View unanswered posts | View active topics It is currently Wed Dec 25, 2024 5:37 pm



Reply to topic  [ 4 posts ] 
 Rotation math 
Author Message
User avatar

Joined: Mon Oct 11, 2010 1:15 pm
Posts: 594
Location: Finlandia
Reply with quote
Post 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.


Thu Mar 13, 2014 8:41 pm
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
Location: In your office, earning your salary.
Reply with quote
Post 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:


Thu Mar 13, 2014 11:40 pm
Profile
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post 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.


Sat Mar 15, 2014 10:49 pm
Profile WWW
Data Realms Elite
Data Realms Elite
User avatar

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
Location: In your office, earning your salary.
Reply with quote
Post 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.


Sat Mar 15, 2014 11:11 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.035s | 13 Queries | GZIP : Off ]