Data Realms Fan Forums
http://45.55.195.193/

Constant velocity?
http://45.55.195.193/viewtopic.php?f=73&t=15073
Page 1 of 2

Author:  numgun [ Sat May 30, 2009 5:58 pm ]
Post subject:  Constant velocity?

Can someone make a snippet of code to make an objects velocity always maintain X value of speed?

I have no idea how to do that since the vector stuff is giving me headaches.
So what I need is this:

Code:
if objectVel < 26 then
    objectVel = 26
end


How do you make it work with those goddamn vectors?

Author:  Foa [ Sat May 30, 2009 6:24 pm ]
Post subject:  Re: Constant velocity?

Code:
if object.Vel < 26 then
    object.Vel = 26;
end


I think you forgot the periods, and the semicolon.

Author:  robolee [ Sat May 30, 2009 6:24 pm ]
Post subject:  Re: Constant velocity?

velocity has both magnitude and direction, you can't just set the magnitude.

you need to check the velocity by using pythagoras theorum on Vel.X and Vel.Y... and I CBA to think about trig, but this hacky way works:

Code:
objectVel = math.sqrt( math.pow(object.Vel.X,2) + math.pow(object.Vel.Y,2) );   --get the magnitude of the velocity

if (objectVel>=27) or (objectVel<=25) then --check if it's not 26
   if object.Vel.X>object.Vel.Y then
      object.Vel.Y=math.sqrt(math.pow(26,2)-math.pow(object.Vel.X,2)); --if it's going faster in Xdir, then increase Y dir to make the overall velocity 26
   else
      object.Vel.X=math.sqrt(math.pow(26,2)-math.pow(object.Vel.Y,2)); --same as above but swap the X and Y around
   end
end


you could just use "if objectVel~=26 then" but then Lua will probably always report it as being false.

Author:  mail2345 [ Sat May 30, 2009 6:51 pm ]
Post subject:  Re: Constant velocity?

Simper:
Code:
if self.Vel.X < 0 and self.Vel.X > -1 * x then
self.Vel.X = -1 * x
end
if self.Vel.X > 0 and self.Vel.X <  x then
self.Vel.X =  x
end


I don't think numgun minds the direction, as long as it's going left/right.

Author:  robolee [ Sat May 30, 2009 7:04 pm ]
Post subject:  Re: Constant velocity?

I'm pretty sure he DOES care about the overall velocity and not just the X velocity, if you shoot it roughly straight up, this will make it suddenly jolt to either the left or right and carry on heading towards the left or right

Author:  Grif [ Sat May 30, 2009 7:34 pm ]
Post subject:  Re: Constant velocity?

Why would you fire an AR2 alt fire directly upwards?

Author:  numgun [ Sat May 30, 2009 7:42 pm ]
Post subject:  Re: Constant velocity?

Actually direction matters. The weapon is supposed to have a tactical nature with the bouncing, so you can fire the shot and let it bounce along the bunker corridors to hit the target behind cover/corner. Just like in those little "puzzles" in HL2 where you needed to bounce the shot around to hit that hiding bastard.

Author:  piipu [ Sat May 30, 2009 7:59 pm ]
Post subject:  Re: Constant velocity?

Code:
function Update(self)
  if self.Vel.Magnitude < 26 then
    local veldir = math.atan2(self.Vel.X,self.Vel.Y)
    self.Vel = Vector(26 * math.cos(veldir), 26 * math.sin(veldir))
  end
end

Simper, workier

Author:  numgun [ Sat May 30, 2009 8:58 pm ]
Post subject:  Re: Constant velocity?

piipu wrote:
Code:
function Update(self)
  if self.Vel.Magnitude < 26 then
    local veldir = math.atan2(self.Vel.X,self.Vel.Y)
    self.Vel = Vector(26 * math.cos(veldir), 26 * math.sin(veldir))
  end
end

Simper, workier


Very often the shot suddenly shifts to fly at a completely different angle.
I get myself zapped 8/10 times.

Author:  robolee [ Sun May 31, 2009 12:58 am ]
Post subject:  Re: Constant velocity?

Well I tested mine out and it would seem that I wasn't thinking about negative values, BUT the magnitude IS always 26. I'll patch it up now...
Code:
if GotActor == false then
         if (self.Vel.Magnitude >= 27) or (self.Vel.Magnitude <= 25) then --check if it's not 26
            if self.Vel.X < 0 then
               self.VX = 0-self.Vel.X;
               reverseX = 1;
            else
               self.VX = self.Vel.X;
               reverseX = 0;
            end
            if self.Vel.Y < 0 then
               self.VY = 0-self.Vel.Y;
               reverseY = 1;
            else
               self.VY = self.Vel.Y;
               reverseY = 0;
            end
            if self.VX > self.VY then
               self.Vel.Y=math.sqrt(math.pow(26,2)-math.pow(self.VX,2)); --if it's going faster in Xdir, then increase Y dir to make the overall velocity 26
               if reverseY == 1 then
                  self.Vel.Y = 0-self.Vel.Y;
               end
            else
               self.Vel.X=math.sqrt(math.pow(26,2)-math.pow(self.VY,2)); --same as above but swap the X and Y around
               if reverseX == 1 then
                  self.Vel.X = 0-self.Vel.X;
               end
            end
         end
      end

this should and DOES work perfectly. I tested it. BE HAPPY!!

Author:  piipu [ Sun May 31, 2009 4:37 pm ]
Post subject:  Re: Constant velocity?

This also works. I'm sure everyone agrees that the problem is now very solved.
Code:
function Update(self)
    self.Vel.X = math.cos(self.Vel.AbsRadAngle) * 28
    if self.Vel.Y > 0 then
   self.Vel.Y = math.sqrt(28 ^ 2 - self.Vel.X ^ 2)
    else
   self.Vel.Y = -math.sqrt(28 ^ 2 - self.Vel.X ^ 2)
    end
end

Author:  numgun [ Sun May 31, 2009 4:47 pm ]
Post subject:  Re: Constant velocity?

@_@

So many codes to choose from. Right now I'm running with Robolee's code and its working pretty fine.

Piipu's code seems smaller which is always better.
I'll need to test it before I say anything else though.

And then another thing: wtf is with these people, pulling all sorts of nuclear science and epic math for physics? I mean I dont understand a damn of any bit of those codes at all. Feels like all the advanced Lua coders here are like some rocket scientists. o_-

Author:  piipu [ Sun May 31, 2009 4:56 pm ]
Post subject:  Re: Constant velocity?

My method works too, I actually tested it ingame to avoid errors. Though it bounces the harpoon pretty weirdly. Vectors aren't really that complicated once you understand what they are. Then you need just some basic trigonometry.

Author:  robolee [ Sun May 31, 2009 5:05 pm ]
Post subject:  Re: Constant velocity?

numgun wrote:
@_@

So many codes to choose from. Right now I'm running with Robolee's code and its working pretty fine.

Piipu's code seems smaller which is always better.
I'll need to test it before I say anything else though.

And then another thing: wtf is with these people, pulling all sorts of nuclear science and epic math for physics? I mean I dont understand a damn of any bit of those codes at all. Feels like all the advanced Lua coders here are like some rocket scientists. o_-


I'm doing all three sciences at A level, and doing stuff like the photoelectric effect, forces and suvat equations, quark structure, mesons, bosons, baryons, leptons, anti-particles, strong interaction, weak interaction, beta emission... it goes on... and on... with words you probably wont even recognise or have barely scratched the surface on. So I suppose I'm probably qualified to be a rocket scientist lol (as soon as I get my A and beat Will!!!).

Author:  piipu [ Sun May 31, 2009 5:16 pm ]
Post subject:  Re: Constant velocity?

I'm... in the first year of upper secondary school? Also, you propably won't really need nuclear science for CC lua scripting.

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