Results 1 to 38 of 38

Thread: Example of Projectile motion

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Ontario, Canada
    Posts
    12

    Example of Projectile motion

    Hello, does anybody have a simple example of a projectile motion code?

    thanks

  2. #2
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    x = -t^2+4x+500

    I havn't been to school in a wekk.. but I think this is a valid quadratic equation.
    Don't pay attention to this signature, it's contradictory.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Ontario, Canada
    Posts
    12
    i need to be able to chose an angle and power and change the projectile motion accordingly, but thanks

  4. #4
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    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.
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hmm... Try this:

    VB Code:
    1. 'To get the projectile moving
    2. Gravity = 0.5
    3. ShootSpeed = 10
    4. X = 100
    5. Y = 100 'These are just sample coordinates, where the projectile starts...
    6. XSpeed = cos(Angle) * ShootSpeed
    7. YSpeed = sin(Angle) * ShootSpeed
    8.  
    9. 'Every frame
    10. YSpeed = YSpeed + Gravity
    11. X = X + XSpeed
    12. 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...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hmm CornedBee that's exactly what I did
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    No, you don't have drag.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  9. #9
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Duh 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...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  10. #10
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Jotaf98
    Duh 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?
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  11. #11
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    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?
    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.

    Z.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I like Max Payne for it's non-instant gunshots . Although they do travel a bit too slowly.

    For drag, think of shot put.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  14. #14
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Originally posted by Zaei
    Ever seen a game with a line of sight of 1000 yards?
    Z.
    Battlefield 1942 - you can see up to 4 Km away.

    -Sheppe
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Ontario, Canada
    Posts
    12
    when i use your code the shape just gets moved to the top left corner...

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Whose code?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by CornedBee
    Whose code?
    He probably tried Jotaf98s signatur....

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Have you just copied jotaf's code? Then it is of course bound to fail...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by CornedBee
    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...

  20. #20

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Ontario, Canada
    Posts
    12
    Originally posted by Jotaf98
    Hmm... Try this:

    VB Code:
    1. 'To get the projectile moving
    2. Gravity = 0.5
    3. ShootSpeed = 10
    4. X = 100
    5. Y = 100 'These are just sample coordinates, where the projectile starts...
    6. XSpeed = cos(Angle) * ShootSpeed
    7. YSpeed = sin(Angle) * ShootSpeed
    8.  
    9. 'Every frame
    10. YSpeed = YSpeed + Gravity
    11. X = X + XSpeed
    12. 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.

  21. #21
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Perhaps it is that Cos() and Sin() expect Radians, not Angles.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  22. #22
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Huh... the "To get the projectile moving" part is meant to be used ONCE (to get the projectile moving ), the "Every frame" part is every frame...

    If you're using a control, you should also at the end of every frame do this:

    VB Code:
    1. obj.Left = X
    2. obj.Top = Y

    But of course that's a bit obvious, isn't it?

    Oh you also need to declare the variables... (as Single or something) anything else?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  23. #23
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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).
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, that's what I thought too when I asked for copy/paste.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  25. #25

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Ontario, Canada
    Posts
    12
    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

  26. #26
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    x- and yspeed speed should be modified each timer tick.

    Z.

  27. #27
    Member anjulpa's Avatar
    Join Date
    Aug 2001
    Location
    india
    Posts
    43
    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 ??
    Attached Files Attached Files

    ppl saw this
    Die, ***** Die !

  28. #28
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    X = X + XSpeed
    Y = Y + YSpeed
    I Could Be Wrong But Shoudnt It Be

    X = X + XSpeed
    Y = Y - YSpeed
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  29. #29
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by BodwadUK
    I Could Be Wrong But Shoudnt It Be

    X = X + XSpeed
    Y = Y - YSpeed
    No. That would be for the special case of when -y moves in our "up" direction.

    Z.

  30. #30
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can avoid using trigonometry this way:
    VB Code:
    1. Const drag = whatever
    2. Dim x As Single
    3. Dim y As Single
    4. Dim l As Single
    5. Dim nl As Single
    6. Dim f As Single
    7.  
    8. l = sqrt(x^2 + y^2)
    9. nl = l - drag
    10. f = nl / l
    11. x = x * f
    12. y = y * f

    But I haven't tested if this really yields correct results, it's just an "educated guess".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  31. #31
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    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.
    Don't pay attention to this signature, it's contradictory.

  32. #32
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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 )
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  33. #33
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  34. #34
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    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!
    Don't pay attention to this signature, it's contradictory.

  35. #35
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  36. #36
    Member anjulpa's Avatar
    Join Date
    Aug 2001
    Location
    india
    Posts
    43

    Lightbulb

    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
    Last edited by anjulpa; Jan 8th, 2003 at 09:24 AM.

    ppl saw this
    Die, ***** Die !

  37. #37
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Yeah so I guess it's solved

    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:
    1. 'Basically we're checking if the shape has hit the bottom of the
    2. 'form, you can of course use the variables X and Y or something
    3. 'else than the form's height to check this, you can for example
    4. 'make it bounce on walls and stuff...
    5. If Shape1.Top + Shape1.Height > Form1.ScaleHeight * 15 Then
    6.     '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)
    7.     YSpeed = YSpeed * -0.75
    8. End If
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  38. #38
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hey hey looks like I'm a frenzied member now... cool
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width