need some physics here...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isotope11
    New Member
    • Mar 2007
    • 10

    need some physics here...

    Would someone have code that emulates projectile motion.
    This is what I've got:

    Shape1 - the projectile

    The variables are:

    Velocity - the muzzle velocity
    Angle - the angle in which the projectile is fired

    Other variables are:

    Height - how high is the projectile in the specific time
    Range - how far is the projectile from the point of origin

    I know all these variables, but I just forgot the formula. Please help me.
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by isotope11
    Would someone have code that emulates projectile motion.
    This is what I've got:

    Shape1 - the projectile

    The variables are:

    Velocity - the muzzle velocity
    Angle - the angle in which the projectile is fired

    Other variables are:

    Height - how high is the projectile in the specific time
    Range - how far is the projectile from the point of origin

    I know all these variables, but I just forgot the formula. Please help me.
    Hello, isotope11!

    Seems like you have work cut out...

    Is this C++?

    You may need to figure outhow to solve this manually firsthand before loading into a program. Perhaps builing a pseudocode, after you have mastered a manual result can help.

    How's that for a deal?

    Dököll

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      If you know Quick Basic by chance I bet there's an example in Gorillas.bas... or was it Gorilla.bas :) Search the net for it.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Surely you just apply the current velocity, and adjust for 9.8 metres-per-second-per-second acceleration downward.

        I do remember two of the three fundamental equations of motion we learned in physics at high school...

        v = u + at
        s = ut + ½at2 (don't know how to superscript the "2").

        v = final velocity
        u = initial velocity
        a = acceleration
        t = time
        s = distance (actually "displaceme nt" I think)


        By the way, since it has always been one of the most basic (no pun intended) functions to which computers have been applied, perhaps you could try looking up "ballistics " somewhere like Wikipedia.

        Comment

        • brazel
          New Member
          • May 2007
          • 11

          #5
          Good day!

          try this link

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            That link just takes one to the home.net home page.

            Comment

            • brazel
              New Member
              • May 2007
              • 11

              #7
              Good day!

              Sir

              Sorry that link, I think that link is that link that I get b4.

              Comment

              • brazel
                New Member
                • May 2007
                • 11

                #8
                [CODE=vb]'************** *************** *************** *****
                ' Basic gravity demo! Feel free to modify this
                ' code, steal it, etc. I don't give a crap! It's
                ' a falling circle for crying out loud :)
                '
                ' - Lucky
                ' Lucky's VB Gaming Site
                ' http://members.home.net/theluckyleper
                '************** *************** *************** *****

                Option Explicit

                'The gettickcount API delcaration
                Private Declare Function GetTickCount Lib "kernel32" () As Long

                Const PI = 3.14159 'Mmmm.. Pi
                Const ORBIT_GRAVITY = 1000 'Acceleration due to gravity (during the "orbit" sequence)
                Const FALL_GRAVITY = 0.1 'Acceleration due to gravity (during the "fall" sequence)
                Const OBJECT_RADIUS = 10 'How FALL is our "ball"?

                Const ORBIT_PLN_RADIU S = 15 'How big is our planet during an "orbit" sequence?
                Const ORBIT_PLN_X = 200 'X-Coordinate of the planet during an "orbit"
                Const ORBIT_PLN_Y = 200 'Y-Coordinate of the planet during an "orbit"
                Const ORBIT_OBJ_X = 280 'Starting X-Coord of our object during an "orbit"
                Const ORBIT_OBJ_Y = 220 'Starting Y-Coord of our object during an "orbit"
                Const ORBIT_OBJ_SPEED = 4 'Starting speed during an "orbit"
                Const ORBIT_OBJ_HEADI NG = 0 'Starting heading during an "orbit"

                Const FALL_PLN_RADIUS = 500 'How big is our planet during a "fall" sequence?
                Const FALL_PLN_X = 200 'X-Coordinate of the planet during a "fall"
                Const FALL_PLN_Y = 600 'Y-Coordinate of the planet during a "fall"
                Const FALL_OBJ_X = 375 'Starting X-Coord of our object during a "fall"
                Const FALL_OBJ_Y = 345 'Starting Y-Coord of our object during a "fall"
                Const FALL_OBJ_SPEED = 7 'Starting speed during a "fall"
                Const FALL_OBJ_HEADIN G = 15 * PI / 8 'Starting heading during a "fall"

                Const MS_DELAY = 25 'Milliseconds per frame (25 = 40 frames per second)

                Dim mlngTimer As Long 'Holds system time since last frame was displayed
                Dim msngHeading As Single 'Current direction in which object is moving
                Dim msngSpeed As Single 'Current speed with which object is moving
                Dim msngObjX As Single 'Current X coordinate of object within form
                Dim msngObjY As Single 'Current Y coordinate of object within form
                Dim mblnRunning As Boolean 'Is the render loop running?
                Dim mblnOrbit As Boolean 'Are we orbiting?
                Dim mblnFall As Boolean '..or are we falling?

                Private Sub Form_Load()

                'Initialize the variables
                mlngTimer = GetTickCount()
                mblnRunning = True
                mblnOrbit = False
                mblnFall = False
                shpObject.Width = OBJECT_RADIUS
                shpObject.Heigh t = OBJECT_RADIUS

                'Display the form
                frmGravity.Show

                'Start the render loop
                Do While mblnRunning
                'Check if we've waited for the appropriate number of milliseconds
                If mlngTimer + MS_DELAY <= GetTickCount() Then
                mlngTimer = GetTickCount() 'Reset the timer variable
                If mblnOrbit Then Physics_Orbit 'Make the object orbit
                If mblnFall Then Physics_Fall 'Make the object fall
                If mblnOrbit Or mblnFall Then DrawObject 'Draw the object
                End If
                'Allow other events to occur
                DoEvents
                Loop

                End Sub

                Private Sub cmdOrbit_Click( )

                'Set up our booleans
                mblnOrbit = True
                mblnFall = False

                'Place the planet
                shpPlanet.Width = ORBIT_PLN_RADIU S
                shpPlanet.Heigh t = ORBIT_PLN_RADIU S
                shpPlanet.Left = ORBIT_PLN_X - shpPlanet.Width / 2
                shpPlanet.Top = ORBIT_PLN_Y - shpPlanet.Heigh t / 2

                'Place the object
                msngSpeed = ORBIT_OBJ_SPEED
                msngHeading = ORBIT_OBJ_HEADI NG
                msngObjX = ORBIT_OBJ_X
                msngObjY = ORBIT_OBJ_Y

                End Sub

                Private Sub cmdFall_Click()

                'Set up our booleans
                mblnOrbit = False
                mblnFall = True

                'Place the planet
                shpPlanet.Width = FALL_PLN_RADIUS
                shpPlanet.Heigh t = FALL_PLN_RADIUS
                shpPlanet.Left = FALL_PLN_X - shpPlanet.Width / 2
                shpPlanet.Top = FALL_PLN_Y - shpPlanet.Heigh t / 2

                'Place the object
                msngSpeed = FALL_OBJ_SPEED
                msngHeading = FALL_OBJ_HEADIN G
                msngObjX = FALL_OBJ_X
                msngObjY = FALL_OBJ_Y

                End Sub

                Private Sub DrawObject()

                'Move the object (according to its speed and heading)
                msngObjX = msngObjX + msngSpeed * Sin(msngHeading )
                msngObjY = msngObjY - msngSpeed * Cos(msngHeading )

                'Display the object
                shpObject.Left = msngObjX - OBJECT_RADIUS / 2
                shpObject.Top = msngObjY - OBJECT_RADIUS / 2

                End Sub

                Private Sub Physics_Orbit()

                Dim sngXComp As Single 'Resultant X and Y components
                Dim sngYComp As Single
                Dim sngPlnX As Single 'X Distance to planet
                Dim sngPlnY As Single 'Y Distance to planet
                Dim sngDist As Single 'The linear distance to planet
                Dim sngGravDir As Single 'In which direction is gravity acting (towards the planet!)

                'Find the distance to planet
                sngPlnX = ORBIT_PLN_X - msngObjX
                sngPlnY = msngObjY - ORBIT_PLN_Y
                sngDist = Sqr(sngPlnX ^ 2 + sngPlnY ^ 2)

                'Calculate the gravity direction, and adjust for arctangent by adding Pi if necessary
                If sngPlnY > 0 Then sngGravDir = Atn(sngPlnX / sngPlnY)
                If sngPlnY < 0 Then sngGravDir = Atn(sngPlnX / sngPlnY) + PI

                'Find the components
                sngXComp = msngSpeed * Sin(msngHeading ) + (ORBIT_GRAVITY / (sngDist ^ 2)) * Sin(sngGravDir)
                sngYComp = msngSpeed * Cos(msngHeading ) + (ORBIT_GRAVITY / (sngDist ^ 2)) * Cos(sngGravDir)

                'Determine the resultant speed
                msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)

                'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
                If sngYComp > 0 Then msngHeading = Atn(sngXComp / sngYComp)
                If sngYComp < 0 Then msngHeading = Atn(sngXComp / sngYComp) + PI

                End Sub

                Private Sub Physics_Fall()

                Dim sngXComp As Single 'Resultant X and Y components
                Dim sngYComp As Single

                'Find the components
                sngXComp = Sin(msngHeading ) * msngSpeed
                sngYComp = Cos(msngHeading ) * msngSpeed - FALL_GRAVITY 'Don't forget to apply gravity straight down!

                'Determine the resultant speed
                msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)

                'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
                If sngYComp > 0 Then msngHeading = Atn(sngXComp / sngYComp)
                If sngYComp < 0 Then msngHeading = Atn(sngXComp / sngYComp) + PI

                'Stop the ball when it hits the "ground"
                If msngObjY > FALL_OBJ_Y Then msngSpeed = 0

                End Sub

                Private Sub Form_Unload(Can cel As Integer)

                'Terminate the render loop
                mblnRunning = False

                End Sub[/CODE]
                Last edited by Killer42; May 16 '07, 07:49 AM. Reason: Added [CODE=vb] tag

                Comment

                • isotope11
                  New Member
                  • Mar 2007
                  • 10

                  #9
                  I have QuickBasic, but it is installed in my old CPU.

                  Comment

                  • isotope11
                    New Member
                    • Mar 2007
                    • 10

                    #10
                    It is s = ut + ½at ^ 2

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by isotope11
                      It is s = ut + ½at ^ 2
                      Yeah, I suppose I could have written it as something like ½a(tt), too. I'm just used to using the superscripted "2" to represent the square.

                      By the way, do you remember the third? There were three fundamental equations that we used back in high school physics to calculate all this stuff, but I can only ever recall two of them.

                      Ahah!

                      How does this look... s = ut + ½at²

                      Comment

                      • JonLT
                        New Member
                        • Jul 2007
                        • 41

                        #12
                        this might help

                        newPosition = oldPosition + (Velocity * dt)
                        Velocity += (Force * (1.0/Mass)) *dt

                        the positions, velocity and force are vectors (x,y,z) so be sure to use the right math

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by JonLT
                          this might help

                          newPosition = oldPosition + (Velocity * dt)
                          Velocity += (Force * (1.0/Mass)) *dt

                          the positions, velocity and force are vectors (x,y,z) so be sure to use the right math
                          This thread is a couple of months old now, so I'd guess the issue would have been resolved by now. isotope11, if you're still around, how did it turn out?

                          Comment

                          Working...