Results 1 to 18 of 18

Thread: [Distance = Velocity x Time] at any angle and in 3D???

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    [Distance = Velocity x Time] at any angle and in 3D???

    I'm writing an OpenGL 3D screensaver that will bounce off the sides of the screen traveling at different angles and on the Z axis as well.

    The factors involved are:
    X, Y, Z, angle, velocity, time
    and maybe I missed some.

    How would I go about calculating all of those factors?

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    The time is actually delta time:

    New_Time = timeGetTime - Initial_Time
    Delta_Time = New_Time - Current_Time
    Current_Time = New_Time

    Where initial time is the time obtained before your main loop.

    Velocity is done in many ways, depending on how you want to integrate. I have a sub that does many ways:

    VB Code:
    1. Public Sub Integrate2D(Obj As OBJECT2D, dt As Single, Integrator As CONST_INTEGRATOR)
    2.  
    3.     Dim k1 As POINT2D, k2 As POINT2D, k3 As POINT2D, k4 As POINT2D
    4.     Dim l1 As VECTOR2D, l2 As VECTOR2D, l3 As VECTOR2D, l4 As VECTOR2D
    5.    
    6.     With Obj
    7.    
    8.         .Force.Net.X = .Mass * (Air_Resistance(0.5, AIR_DENSITY, 0.1, .Velocity.X))
    9.                                
    10.         .Force.Net.Y = .Mass * (-EARTH_GRAVITY + _
    11.                                 Air_Resistance(0.5, AIR_DENSITY, 0.1, .Velocity.Y))
    12.        
    13.         .Acceleration.X = .Force.Net.X / .Mass
    14.         .Acceleration.Y = .Force.Net.Y / .Mass
    15.        
    16.         Select Case Integrator
    17.        
    18.             Case FORWARD_EULER
    19.        
    20.                 .Position.X = .Position.X + .Velocity.X * dt
    21.                 .Velocity.X = .Velocity.X + .Acceleration.X * dt
    22.            
    23.                 .Position.Y = .Position.Y + .Velocity.Y * dt
    24.                 .Velocity.Y = .Velocity.Y + .Acceleration.Y * dt
    25.                
    26.             Case SECOND_ORDER_EULER
    27.            
    28.                 .Position.X = .Position.X + .Velocity.X * dt + 0.5 * .Acceleration.X * dt * dt
    29.                 .Velocity.X = .Velocity.X + .Acceleration.X * dt
    30.            
    31.                 .Position.Y = .Position.Y + .Velocity.Y * dt + 0.5 * .Acceleration.Y * dt * dt
    32.                 .Velocity.Y = .Velocity.Y + .Acceleration.Y * dt
    33.            
    34.             Case VERLET
    35.            
    36.                 .Velocity.X = .Position.X - Old_Position.X + .Acceleration.X * dt * dt
    37.                 Old_Position.X = .Position.X
    38.                 .Position.X = .Position.X + .Velocity.X
    39.                
    40.                 .Velocity.Y = .Position.Y - Old_Position.Y + .Acceleration.Y * dt * dt
    41.                 Old_Position.Y = .Position.Y
    42.                 .Position.Y = .Position.Y + .Velocity.Y
    43.            
    44.             Case VELOCITY_VERLET
    45.                
    46.                 Old_Acceleration.X = .Acceleration.X
    47.                 .Position.X = .Position.X + .Velocity.X * dt + 0.5 * Old_Acceleration.X * dt * dt
    48.                 .Velocity.X = .Velocity.X + 0.5 * (Old_Acceleration.X + .Acceleration.X) * dt
    49.            
    50.                 Old_Acceleration.Y = .Acceleration.Y
    51.                 .Position.Y = .Position.Y + .Velocity.Y * dt + 0.5 * Old_Acceleration.Y * dt * dt
    52.                 .Velocity.Y = .Velocity.Y + 0.5 * (Old_Acceleration.Y + .Acceleration.Y) * dt
    53.            
    54.             Case SECOND_ORDER_RUNGE_KUTTA
    55.            
    56.                 k1.X = dt * .Velocity.X
    57.                 k1.Y = dt * .Velocity.Y
    58.                 l1.X = dt * .Acceleration.X
    59.                 l1.Y = dt * .Acceleration.Y
    60.                
    61.                 k2.X = dt * (.Velocity.X + k1.X / 2)
    62.                 k2.Y = dt * (.Velocity.Y + k1.Y / 2)
    63.                 l2.X = dt * .Acceleration.X
    64.                 l2.Y = dt * .Acceleration.Y
    65.                
    66.                 .Position.X = .Position.X + k2.X
    67.                 .Position.Y = .Position.Y + k2.Y
    68.                 .Velocity.X = .Velocity.X + l2.X
    69.                 .Velocity.Y = .Velocity.Y + l2.Y
    70.            
    71.             Case THIRD_ORDER_RUNGE_KUTTA
    72.            
    73.                 k1.X = dt * .Velocity.X
    74.                 k1.Y = dt * .Velocity.Y
    75.                 l1.X = dt * .Acceleration.X
    76.                 l1.Y = dt * .Acceleration.Y
    77.                
    78.                 k2.X = dt * (.Velocity.X + k1.X / 2)
    79.                 k2.Y = dt * (.Velocity.Y + k1.Y / 2)
    80.                 l2.X = dt * .Acceleration.X
    81.                 l2.Y = dt * .Acceleration.Y
    82.                
    83.                 k3.X = dt * (.Velocity.X - k1.X + 2 * k2.X)
    84.                 k3.Y = dt * (.Velocity.Y - k1.Y + 2 * k2.Y)
    85.                 l3.X = dt * .Acceleration.X
    86.                 l3.Y = dt * .Acceleration.Y
    87.                
    88.                 .Position.X = .Position.X + k1.X * 1 / 6 + k2.X * 2 / 3 + k3.X * 1 / 6
    89.                 .Position.Y = .Position.Y + k1.Y * 1 / 6 + k2.Y * 2 / 3 + k3.Y * 1 / 6
    90.                 .Velocity.X = .Velocity.X + l1.X * 1 / 6 + l2.X * 2 / 3 + l3.X * 1 / 6
    91.                 .Velocity.Y = .Velocity.Y + l1.Y * 1 / 6 + l2.Y * 2 / 3 + l3.Y * 1 / 6
    92.                
    93.             Case FORTH_ORDER_RUNGE_KUTTA
    94.            
    95.                 k1.X = dt * .Velocity.X
    96.                 k1.Y = dt * .Velocity.Y
    97.                 l1.X = dt * .Acceleration.X
    98.                 l1.Y = dt * .Acceleration.Y
    99.                
    100.                 k2.X = dt * (.Velocity.X + k1.X / 2)
    101.                 k2.Y = dt * (.Velocity.Y + k1.Y / 2)
    102.                 l2.X = dt * .Acceleration.X
    103.                 l2.Y = dt * .Acceleration.Y
    104.                
    105.                 k3.X = dt * (.Velocity.X + k2.X / 2)
    106.                 k3.Y = dt * (.Velocity.Y + k2.Y / 2)
    107.                 l3.X = dt * .Acceleration.X
    108.                 l3.Y = dt * .Acceleration.Y
    109.                
    110.                 k4.X = dt * (.Velocity.X + k3.X)
    111.                 k4.Y = dt * (.Velocity.Y + k3.Y)
    112.                 l4.X = dt * .Acceleration.X
    113.                 l4.Y = dt * .Acceleration.Y
    114.  
    115.                 .Position.X = .Position.X + k1.X / 6 + k2.X / 3 + k3.X / 3 + k4.X / 6
    116.                 .Position.Y = .Position.Y + k1.Y / 6 + k2.Y / 3 + k3.Y / 3 + k4.Y / 6
    117.                 .Velocity.X = .Velocity.X + l1.X / 6 + l2.X / 3 + l3.X / 3 + l4.X / 6
    118.                 .Velocity.Y = .Velocity.Y + l1.Y / 6 + l2.Y / 3 + l3.Y / 3 + l4.Y / 6
    119.                
    120.         End Select
    121.        
    122.     End With
    123.  
    124. End Sub

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Thanks Jacob. I was really looking for something more direct and in C++ if possible. The end result would need to be the new X Y and Z position. I can set the velocity to anything I want and I'll probably variate that.

    BTW, I love your Firefox smile.

  4. #4
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Why, you can't convert the code?

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    It just doesn't look to be what I need, that's all. Maybe it is and I just don't see it.

  6. #6
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Just noticed a little something. For now stick with the Forward Euler method, and the dt is NOT delta time. It is a time step (anywhere between 1/60 to 1/1000). To involve delta time, you do this:

    VB Code:
    1. New_Time = timeGetTime
    2.         Delta_Time = (New_Time - Current_Time) / 1000
    3.         Current_Time = New_Time
    4.        
    5.         If Delta_Time > 0.25 Then Delta_Time = 0.25
    6.        
    7.         Accumulator = Accumulator + Delta_Time
    8.        
    9.         While (Accumulator >= Time_Step)
    10.            
    11.             Accumulator = Accumulator - Time_Step
    12.              
    13.             Integrate2D Obj, Time_Step, FORWARD_EULER
    14.            
    15.             Time = Time + Time_Step
    16.            
    17.         Wend

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Your code really helped me understand partially what I need to do. PositionX and PositionY are calculated according to their Velocity. The problem is, let's say the object is moving straight right (Angle=360). Then my Angle changes to 45. How do I recalculate my Velocities?

  8. #8
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Why would your angle change to 45? If you're bouncing objects around then:

    -Your absolute speed should remain constant even if a 3D projection appears to have an accelleration.
    -When your object hits a wall your velocity components should be negated thats it..
    Hitting a vertical wall negates the X component of your velocity vector
    Hitting a Horizontal wall negates the Y component
    Hitting the screen surface/back-wall negates the Z Component..

    From there its basic trig/vector addition.. The dV = 0 in magnitude, only in direction does it change.. The apparent change in the actual speed only occurs because the projection onto the XY plane changes..

    PS To clarify: In physics speed and velocity mean different things.. Velocity needs a direction, so 5km/h [N] <> 5km/h [S]..
    Speed only deals with scalar magnitudes so 5km/h [N] = 5km/h [S]..

  9. #9
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Yeah, triggernum is right. That would get the job done. You have to check for certain bounderies and then see if there is a "collision" then change the values accordingly.

    Quote Originally Posted by aewarnick
    Your code really helped me understand partially what I need to do. PositionX and PositionY are calculated according to their Velocity. The problem is, let's say the object is moving straight right (Angle=360). Then my Angle changes to 45. How do I recalculate my Velocities?
    So what you are saying is that the objects get acted on by "forces", right? In that case, you would take the "force" vector and add to the current velocity of the object but add it in small amounts so it gradually changes to the new direction over a time period. I think would also need to scale the result each time you add it (keep the magnitude the same) so that the object doesn't speed up/down when it is changing directions, unless you want it to.

    Take your example of moving to the right, then you want to change the direction of 45 degrees. You need to do vector addition on a scaled vector with the direction of 45 degrees, then add it until the direction of the object is 45. So, you would see it gradually move into the right direction.

    I think thats what you ment, if not just clarify

  10. #10
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    What kind of forces do you want to act on these objects? Aside from the walls/balls what else do balls interact with? I'm picturing fluid dynamics type stuff.. Whirlpools, standing waves etc.. Or possibly pinball type obstacles if you doubt your differential equations..
    Or if you want supercool effects consider a ball with elastic properties that vary DRASTICALLY with temperature, and have heat sources, coolers and obstacles much smaller than your ball diameter (like poles).. You could then get a morphing aboeboid type effect..
    I wrote something like that in QBasic ages ago.. I'll see if I can dig it up, but no promises..
    (I'll have to slow it down I think because if it ran nice on my 286 I doubt you'd be able to see anything on a modern pc)

  11. #11

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Thanks for responding. Though it'd be nice to create something amazing, I don't know enouph math to do it. Most of what you guys are talking about is french to me.

    I am creating a very very simple screen saver that will change direction (not speed or velocity) when it hits a wall: almost just like the windows "ss3dfo.scr" screen saver does.

    What I need is help on is calculating a new angle after it hits a wall.

  12. #12
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Depends on how your vectors are set up.. Do you save them as .vX and .vY or as a speed and angle.. Do it the first way and use the logic I posted earlier.. Or if you have too much invested in the second method, just remember is in any given plane ANGLE OF INCIDENCE = ANGLE OF REFLECTION and you can just keep track of the proper sign manually without doing it with real trig..
    So none of this making 90 degrees 45 degrees ok

  13. #13
    New Member dreiecon's Avatar
    Join Date
    Aug 2006
    Location
    Australia
    Posts
    8

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    There are 2 ways to get true distance using physics equations.
    d is distance
    u is inital velocity
    v is final velocity
    t is time
    d=((u+v)/2)*t

    u is inital velocity
    t is time
    a is acceleration.
    d=(u*t)+(0.5*(a*(t^2)))

    These are just incase you guys wish to use them.
    [VBCODE]
    Private Sub form1_load()
    If programming = fun then
    MsgBox ("Programming is so much fun!")
    Else
    MsgBox ("Learn to program n00b")
    End If
    End Sub[/VBCODE]

  14. #14

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    "Hitting a vertical wall negates the X component of your velocity vector
    Hitting a Horizontal wall negates the Y component
    Hitting the screen surface/back-wall negates the Z Component.."

    Is it really that simple? Remember that I'm starting from scratch here. All I need is xVel, yVel and zVel, and of course, xPos, yPos and zPos?

  15. #15
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Quote Originally Posted by aewarnick
    "Hitting a vertical wall negates the X component of your velocity vector
    Hitting a Horizontal wall negates the Y component
    Hitting the screen surface/back-wall negates the Z Component.."

    Is it really that simple? Remember that I'm starting from scratch here. All I need is xVel, yVel and zVel, and of course, xPos, yPos and zPos?
    Yes, it really is that simple. Like you said, all you need is xVel, yVel, zVel, xPos, yPos and zPos for each object, and in the loop just add the speeds to the positions (xPos+=xVel and so on). Then just check the bounds and if its out of the bounds, change the xVel, yVel or zVel (xVel= -xVel)depending on what bound it hit .

  16. #16

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    Makes sense. Thank you ALL!

  17. #17
    New Member dreiecon's Avatar
    Join Date
    Aug 2006
    Location
    Australia
    Posts
    8

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    With the sums I posted up before,
    I left 1 important thing out.

    Velocity is not just a speed, it's also a direction.
    E.G.
    There are 2 ways to get true distance using physics equations.
    d is distance
    u is inital velocity
    v is final velocity
    t is time
    u=0
    v=5 South
    t=10 Seconds
    d=((u+v)/2)*t
    d=((5+0)/2)*10
    d=25 Meters South! Not just 25 Meters.

    This can also be used in a 3D Game if you know your XYZ Axis.

    Hope this helped. =D
    [VBCODE]
    Private Sub form1_load()
    If programming = fun then
    MsgBox ("Programming is so much fun!")
    Else
    MsgBox ("Learn to program n00b")
    End If
    End Sub[/VBCODE]

  18. #18
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: [Distance = Velocity x Time] at any angle and in 3D???

    The thing is he isn't really using Newton's Laws.. The direction is the only part of his velocity vector he needs to change for his description.. If he wanted to make it more realistic he could put elastic type bumpers on the walls like a pool table and really exemplify the delta_speed according to your equations, but really its not needed.. Looking at a pool table, its obvious that every action has an equal/opposite reaction, but accellerations happen so quickly (High third derivative which is actually called the "jerk") that neglecting it isn't noticable.. Heck Newton's laws don't even give true results in the best of cases, but NASA uses them where ever they yield a close enough result and Einstein's equations when Newton doesn't suffice..
    Reminds me of an ASM game I made in highschool called RIP where ghosts bounced around like that and destroyed gravestones until you killed them with a crosshair..

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