Hello, does anybody have a simple example of a projectile motion code?
thanks
Printable View
Hello, does anybody have a simple example of a projectile motion code?
thanks
x = -t^2+4x+500
I havn't been to school in a wekk.. but I think this is a valid quadratic equation.
i need to be able to chose an angle and power and change the projectile motion accordingly, but thanks
hmm lemme think..
the angle could be easily achieved with sin(angle)*X for the change in upwards motion... but there is alot I don't know about quadratic equation, they only taught me to change it from trinome to binome..
It migh tlook something like:
Y = -X^2+(sin(angle)*X)+StartPointOfShot
note: this probably won't do gravity very well.. post this in the maths forum.
Hmm... Try this:
VB Code:
'To get the projectile moving Gravity = 0.5 ShootSpeed = 10 X = 100 Y = 100 'These are just sample coordinates, where the projectile starts... XSpeed = cos(Angle) * ShootSpeed YSpeed = sin(Angle) * ShootSpeed 'Every frame YSpeed = YSpeed + Gravity X = X + XSpeed Y = Y + YSpeed
These are untested values, you'll have to experiment with those to get the result you want ;)
Oh, and be careful with the variables' scope and put the Every Frame code in a timer or something, the other code in the Form_Load or something like that...
Since games use a step-by-step simulation and not immediate calculation it's actually very straight forward:
1) Slow down the overall speed of the projectile by a constant factor (drag).
2) Increase the down-part of the speed of the projectile by a constant factor (gravity).
3) Modify the projectile position with the speed vector.
Hmm CornedBee that's exactly what I did :p
No, you don't have drag.
Duh :rolleyes: it's the same thing except for the drag. Ok. I never used drag and it always worked really well, why would you need to slow down the projectile like that? With most projectiles air resistance is almost non-existant, what else do you need...
Huh? Never fired a rifle then, eh? Fire off a 22-250 - at the muzzle it's moving at about 4000 ft. per sec.. At 1000 yards it's moving at about 2000 ft. per sec.. What's slowing it down?Quote:
Originally posted by Jotaf98
Duh :rolleyes: it's the same thing except for the drag. Ok. I never used drag and it always worked really well, why would you need to slow down the projectile like that? With most projectiles air resistance is almost non-existant, what else do you need...
Ever seen a game with a line of sight of 1000 yards? Ill give you Delta Force, however, drag is included in the Delta Force calculations, if I recall correctly. Most of the time, its irrelevent, and most often, gunshots are reduced to simple ray casts, and damage is applied instantly.Quote:
Originally posted by Sheppe
Huh? Never fired a rifle then, eh? Fire off a 22-250 - at the muzzle it's moving at about 4000 ft. per sec.. At 1000 yards it's moving at about 2000 ft. per sec.. What's slowing it down?
Z.
Was just the general projectile formula. Could also be a stone or a ball threwn. And some balls have a LOT air resistance.
To make it even better you'd even have to calculate in the wind, but not many games have wind at all. But if you write a Worms-like game you should know about it.
I like Max Payne for it's non-instant gunshots :). Although they do travel a bit too slowly.
For drag, think of shot put.
Battlefield 1942 - you can see up to 4 Km away. :)Quote:
Originally posted by Zaei
Ever seen a game with a line of sight of 1000 yards?
Z.
-Sheppe
when i use your code the shape just gets moved to the top left corner...
Whose code?
He probably tried Jotaf98s signatur....:DQuote:
Originally posted by CornedBee
Whose code?
Have you just copied jotaf's code? Then it is of course bound to fail...
...did you use the joke tags now???....I did, I hope you understood that...Quote:
Originally posted by CornedBee
Have you just copied jotaf's code? Then it is of course bound to fail...
Quote:
Originally posted by Jotaf98
Hmm... Try this:
VB Code:
'To get the projectile moving Gravity = 0.5 ShootSpeed = 10 X = 100 Y = 100 'These are just sample coordinates, where the projectile starts... XSpeed = cos(Angle) * ShootSpeed YSpeed = sin(Angle) * ShootSpeed 'Every frame YSpeed = YSpeed + Gravity X = X + XSpeed Y = Y + YSpeed
These are untested values, you'll have to experiment with those to get the result you want ;)
Oh, and be careful with the variables' scope and put the Every Frame code in a timer or something, the other code in the Form_Load or something like that...
this code... hahaha
actually i fixed it a bit, but now it just goes on a diagonal down the screen.
Perhaps it is that Cos() and Sin() expect Radians, not Angles.
Huh... the "To get the projectile moving" part is meant to be used ONCE (to get the projectile moving :rolleyes: ), the "Every frame" part is every frame... :rolleyes: :rolleyes: :rolleyes:
If you're using a control, you should also at the end of every frame do this:
VB Code:
obj.Left = X obj.Top = Y
But of course that's a bit obvious, isn't it? ;)
Oh you also need to declare the variables... :p (as Single or something) anything else?
You're right Sas but I think that his problem was that he was running the whole code every frame (so the variables were reset every frame).
Yes, that's what I thought too when I asked for copy/paste.
my whole code is:
Dim x, y, shootspeed, gravity, angle, xspeed, yspeed As Single
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
'To get the projectile moving
gravity = 0.5
shootspeed = 10
angle = 1
x = 100
y = 100 'These are just sample coordinates, where the projectile starts...
xspeed = Cos(angle) * shootspeed
yspeed = Sin(angle) * shootspeed
End Sub
Private Sub Timer1_Timer()
'Every frame
yspeed = yspeed + gravity
x = x + xspeed
y = y + yspeed
Shape1.Left = x
Shape1.Top = y
End Sub
When this is run the shape just drops off to the bottom, and doesnt seem to come back up
x- and yspeed speed should be modified each timer tick.
Z.
hey cornbee perhaps you should explain how to slow down the overall velocity when using vx and vy as variables, when i got tired with it, i wrote a complete 2d vectors module for it. any better way ??
I Could Be Wrong But Shoudnt It BeQuote:
X = X + XSpeed
Y = Y + YSpeed
X = X + XSpeed
Y = Y - YSpeed
No. That would be for the special case of when -y moves in our "up" direction.Quote:
Originally posted by BodwadUK
I Could Be Wrong But Shoudnt It Be
X = X + XSpeed
Y = Y - YSpeed
Z.
You can avoid using trigonometry this way:
VB Code:
Const drag = whatever Dim x As Single Dim y As Single Dim l As Single Dim nl As Single Dim f As Single l = sqrt(x^2 + y^2) nl = l - drag f = nl / l x = x * f y = y * f
But I haven't tested if this really yields correct results, it's just an "educated guess".
Guys Guys Guys...
You need a quadratic equation for this.
Here's a basic explanation.
A quadratic equation is an equation used to make a parrabolic curve on a graph, I know it can be used for this because they use them in astronaut training to simulate 0g gravity.
It has a basic format: ax^2 + bx + c
a will change how sharp the curve is, c will change the starting height and b changes a bit of everything... here is an example
Oh, and if a is negative, the cuve will face down instead of up, which is VERY important hehe..
-1x^2 + 4x + 25 I am going to simplify this quadratic equation
-1(x^2 - 4x) + 25 First I put x^2 and 4x in paranthese (4x\-1=-4x)
-1(x^2 - 4x +4-4)+25 Then I do (b/2)^2, add + subtract it inside ()
-1(x^2 - 4x + 4) + 25 +4 Take -4, divide it by -1 and get it out of ()
-1(x-2)^2 +29 Get rid of -4x+4, move the ^2 so that it affects the whole ()
and change -4x + 4 to - half of b (4/2=2)
-1(x-2)^2 +29 so what's the point of doing this? Well I can tell you that the projectile will hit it's maximum height at (2, 29), now where did I get those numbers.. hmmm
-1(x-2)^2 +29
All you have to do is change -1 for sharpness of the curve, -2 for where it hits maxY on X axis (x = -2*-1) and 29 is where it maxes on Y axe.
Yeah I know that alkatran (that was today's topic for our math class) but it's not that useful if you want to calculate it every frame. My method is fine, but your code needs a few changes: first, that angle throws the shape almost directly to the bottom of the screen, so of course you don't notice any difference, make the angle 5 or something; second, I gave you everything in pixels and it doesn't work if your form is set to twips... (I tried it with these changes and it worked fine ;) )
Alkatran: as Jotaf said, your code is nice to plot a trajectory curve or to calculate where a projectile hits, but we're talking about simulation here.
Wouldn't work well??...
It would make a great simulation
Private function ProY(X, SAngle, SHeight)
ProY = -(sin(SAngle)*X^2) + 5x + SHeight
'Will not work well
end function
Just increase X by Speed every frame, and voila, a weird moving projectile!
No. Then speed would be only horizontal speed. What if the projectile was fired at an angle?
And it's useless calculation. Nobody wants that in a game loop.
The 5*X part should be removed. It's not useful for trajectory.
One more thing CornBee,
The drag MUST be proportional to the square of the velocity
DragForce=-k*v*v
Acc=DragForce/mass
v=v-Acc
vx=v*cos(Angle)
vy=v*sin(Angle)+gravity
v=sqr(vx*vx+vy*vy)
if vx<>0 then
Angle=Atn(vy/vx)
End if
posX=posX+vx
posY=posY + vy
Yeah so I guess it's solved :D
One more thing. I noticed you said that it "doesn't come back up". So you want it to bounce when it hits the ground? To do that, you'll have to add this check:
VB Code:
'Basically we're checking if the shape has hit the bottom of the 'form, you can of course use the variables X and Y or something 'else than the form's height to check this, you can for example 'make it bounce on walls and stuff... If Shape1.Top + Shape1.Height > Form1.ScaleHeight * 15 Then 'Invert the Y speed, so it starts moving up instead of down, and multiply it by 0.75 to decrease it a bit (or it wouldn't lose velocity with the hit and would never stop bouncing) YSpeed = YSpeed * -0.75 End If
Hey hey looks like I'm a frenzied member now... cool :cool: :D