Author |
Message |
numgun
Joined: Sat Jan 13, 2007 11:04 pm Posts: 2932
|
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?
|
Sat May 30, 2009 5:58 pm |
|
|
Foa
Data Realms Elite
Joined: Wed Sep 05, 2007 4:14 am Posts: 3966 Location: Canadida
|
Re: Constant velocity?
Code: if object.Vel < 26 then object.Vel = 26; end I think you forgot the periods, and the semicolon.
|
Sat May 30, 2009 6:24 pm |
|
|
robolee
Joined: Fri May 11, 2007 4:30 pm Posts: 1040 Location: England
|
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.
Last edited by robolee on Sat May 30, 2009 7:08 pm, edited 1 time in total.
|
Sat May 30, 2009 6:24 pm |
|
|
mail2345
Joined: Tue Nov 06, 2007 6:58 am Posts: 2054
|
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.
|
Sat May 30, 2009 6:51 pm |
|
|
robolee
Joined: Fri May 11, 2007 4:30 pm Posts: 1040 Location: England
|
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
|
Sat May 30, 2009 7:04 pm |
|
|
Grif
REAL AMERICAN HERO
Joined: Sat Jan 27, 2007 10:25 pm Posts: 5655
|
Re: Constant velocity?
Why would you fire an AR2 alt fire directly upwards?
|
Sat May 30, 2009 7:34 pm |
|
|
numgun
Joined: Sat Jan 13, 2007 11:04 pm Posts: 2932
|
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.
|
Sat May 30, 2009 7:42 pm |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
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
|
Sat May 30, 2009 7:59 pm |
|
|
numgun
Joined: Sat Jan 13, 2007 11:04 pm Posts: 2932
|
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.
|
Sat May 30, 2009 8:58 pm |
|
|
robolee
Joined: Fri May 11, 2007 4:30 pm Posts: 1040 Location: England
|
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!!
|
Sun May 31, 2009 12:58 am |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
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
|
Sun May 31, 2009 4:37 pm |
|
|
numgun
Joined: Sat Jan 13, 2007 11:04 pm Posts: 2932
|
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_-
|
Sun May 31, 2009 4:47 pm |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
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.
|
Sun May 31, 2009 4:56 pm |
|
|
robolee
Joined: Fri May 11, 2007 4:30 pm Posts: 1040 Location: England
|
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!!!).
|
Sun May 31, 2009 5:05 pm |
|
|
piipu
Joined: Mon Jun 30, 2008 9:13 pm Posts: 499 Location: Finland
|
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.
|
Sun May 31, 2009 5:16 pm |
|
|
|