Author |
Message |
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Bullet curving
Yeah. So... I have a bullet that travels randomly, and I want it to go towards actors when it gets close enough. The code "self.Vel = actor.Pos - self.Pos" makes the bullet head directly towards the target in a straight line. However, I wanted it to keep it's current path and gradually curve towards the target, like this: Any help would be much appreciated
|
Mon Jul 20, 2009 11:29 pm |
|
|
LowestFormOfWit
Joined: Mon Oct 06, 2008 2:04 am Posts: 1559
|
Re: Bullet curving
Use a sine wave.
|
Mon Jul 20, 2009 11:50 pm |
|
|
Mind
Joined: Thu Mar 06, 2008 10:54 pm Posts: 1360 Location: USA
|
Re: Bullet curving
Yeah I wish I knew how :3
|
Mon Jul 20, 2009 11:51 pm |
|
|
LowestFormOfWit
Joined: Mon Oct 06, 2008 2:04 am Posts: 1559
|
Re: Bullet curving
Mind wrote: Yeah I wish I knew how :3 Me too! Just ask one of the Sparkle Mages.
|
Mon Jul 20, 2009 11:52 pm |
|
|
CrazyMLC
Joined: Fri Dec 22, 2006 4:20 am Posts: 4772 Location: Good news everyone!
|
Re: Bullet curving
LowestFormOfWit wrote: Mind wrote: Yeah I wish I knew how :3 Me too! Just ask one of the Sparkle Mages. A new term has been created today.
|
Tue Jul 21, 2009 12:02 am |
|
|
Kyred
Joined: Sun May 31, 2009 1:04 am Posts: 308
|
Re: Bullet curving
Lol! This post has good timing. About an hour ago, I got a projectile to move along the path of a desired function in CC using only velocity. If you add (or subtract) the target's velocity into the equation, it should account for moving targets.
The way I did it was I took the trajectory that I wanted the projectile to follow and turned it into a parametric function (where both the positions of x and y are based on a function of time) that is based on the distance and angle of your projectile to the target. Then you take the derivative (rate of change) of those functions with respect to time to get the function for velocity that you need to make the projectile travel along that path.
I'll be posting another proof of concept some time in the week or so that will explain how to do something like this.
|
Tue Jul 21, 2009 1:39 am |
|
|
BlackNecro
Joined: Sat Dec 02, 2006 12:41 am Posts: 27 Location: Germany
|
Re: Bullet curving
I'd prolly go about something like: Code: self.Vel = self.Vel + (actor.Pos - self.Pos) * x * dt x being the time in seconds to reach the desired direction, dt being the simtime of a timer resetting after each refresh. Either that works or ... not as I'm pretty much tired :3 Or incase you wanna use a sine wave lemme think for a bit.
|
Tue Jul 21, 2009 2:37 am |
|
|
TheBigCheese
Joined: Mon Jul 27, 2009 4:07 am Posts: 6
|
Re: Bullet curving
Could you base the sine function on the distance to the target?
That would make it end up at 0 on the normalized y axis relative to the normal bullet path, assuming you stretched the sine function correctly. The actual bullet placement would then be the bullet's relative y axis projected into the screen's coordinates.
To get a random curve you would just multiply the sine by a random number -1 through 1 which would flip it and squish it depending on the number.
|
Mon Jul 27, 2009 10:11 pm |
|
|
Kyred
Joined: Sun May 31, 2009 1:04 am Posts: 308
|
Re: Bullet curving
If anyone is still interested in this, here are the equations: For position: Code: t = time (time in milliseconds / 1000); tmax = total time for the projectile to follow the curve. theta = angle from Vector(1,0). (ie. projectile.Vel.AbsRadAngle) a = relative amplitude of the wave.
x = t/tan(theta) y = tan(theta)*t + a*sin(2*(t*PI)/tmax)
If you want to do it using velocity, then just take the derivative with respect to time and you get: Code: t = time (time in milliseconds / 1000); tmax = total time for the projectile to follow the curve. theta = angle from Vector(1,0). (ie. projectile.Vel.AbsRadAngle) a = relative amplitude of the wave.
x = 1/tan(theta) y = tan(theta) + a * (2*PI)/tmax * cos( 2 * (t*PI)/tmax)
|
Sun Aug 02, 2009 7:03 pm |
|
|
|