Results 1 to 9 of 9

Thread: Bit of a math question here...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool

    In this game I'm doing, i need to throw an object given values for angle of elevation and velocity. The problem is that I need to draw the object's path. I'm thinking using those numbers to graph a parabola or something to that effect. Could someone help me out on the formula? It would be very helpful.
    On Error Resume Screaming...

  2. #2
    New Member
    Join Date
    Feb 2001
    Location
    Portugal
    Posts
    8

    Lightbulb

    Using real time fisics laws you wold have to do this:

    in 2 dimensions

    . .
    . .
    . .
    / .
    / .
    ____ /__________________

    you will need the angle 'A'
    the vectorial initial velocity ('v0x,v0y)

    and the position after 't' seconds will be:

    x=x0 + v0x * t
    y=y0 + v0y * t -1/2 * G * t ^ 2

    (x0,y0) initial position
    (G) = 9.8 (g Force)

    if the initial velocity is V then

    v0x= V * cos(A)
    v0y= V * sin(A)

    .....

    if you need help email-me to [email protected]

    the parabola formula is

    y=a*x^2 +b*x +c

  3. #3
    New Member
    Join Date
    Feb 2001
    Location
    Portugal
    Posts
    8
    if you are interested here is a litle vb probram that simulates some fisics laws.
    Attached Files Attached Files

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool

    Okay, let me refine my question. I worked on it myself while waiting for someone to reply. I am in Physics right now in school so I had those two formulas also and everything is worked out except for the time variable. The problem is I don't want to use real time, like tickcounts or whatever. I am trying to work out a way with figuring out how many seconds per pixel and then doing the equation for each pixel but I'm messing up somewhere. I don't know if anyone can understand that but it's worth a shot; I'm giving myself a major headache.
    On Error Resume Screaming...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool Well, I think I figured it out.

    Here's my function:

    Public Function Throw(Angle As Double, Velocity As Double) As Boolean
    'frmSpace.Cls
    Dim Vx As Double, Vy As Double, h As Double, d As Double, t As Double, second As Double, x As Double, y As Double
    Angle = Angle * (3.14159265358979 / 180)
    Vx = Cos(Angle) * Velocity
    Vy = Sin(Angle) * Velocity

    Do
    h = Vy * t - 0.5 * 6.3 * (t * t)
    d = Vx * t
    y = frmSpace.ScaleHeight / 2 - h
    x = frmSpace.ScaleWidth / 2 + d
    If y < 0 Or y > frmSpace.ScaleHeight Or x < 0 Or x > frmSpace.ScaleWidth Then Exit Do
    SetPixelV frmSpace.hdc, x, y, vbYellow
    t = t + 0.025
    Sleep 10
    Loop



    End Function

    The reason I was getting all complicated with time was an attempt to have my line drawn at a velocity proportional to that of the actual datum. The sleep API saved my butt, because all I do to change the speed of the line is change the number for sleep. I guess the code should actually have a variable name instead of 10.
    On Error Resume Screaming...

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    The problem with creating curves from mathematical functions is that your screen is based on integer maths (pixels) and your curve is probably not.

    If I were you I would try to do an inverse mapping rather than a forward mapping - it's hard to explain it in this context but basically it means you calculate what a pixel is meant to be like from an algorithm, rather than using an algorithm to decide where pixels should go. It means you don't get gaps in your line, and you do the minimum number of pixel plots.

    You can divide you parabola into either 1 section (if it has an initial angle of elevation of 45 degrees or less) or 3 sections (if it's between 45 and 90 degrees). If its velocity is always 45 or less degrees from the horizontal, then you can calculate from each X coordinate what the corresponding Y coordinate should be, and plot each pixel at those coordinates.

    If it is split into 3 sections, those sections of the line will look something like this (excuse the ASCII art):

    Code:
    
    |    |            X    X             |
    |    |      X                X       |
    |    |  X                        X   |
    |    |X                             X|
    |   X|                               |X
    |  X |                               | X
    |    |                               |
    | X  |                               |  X
    |____|_______________________________|______ 
         A                               B

    Say the X's are points on your function. The vertical lines A and B divide the curve up into 3 sections. Before A and after B, the absolute value of the angle is greater than 45 degrees. In these regions, you take each y coordinate and calculate the closest corresponding x coordinate. This is so that you don't get gaps in the line. In the middle section, you do it normally as I said for 1 section.
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You could set scalemode to 0-user and do scrolling and zooming of the view, and the coordinates will hit without convertions. The derived formulas for constant accleration is
    sum of displacement in position, displacement in velocity by time and half of displacement in acceleration by time^2 is zero, for each dimension component in a n dimensioned coordinate systems
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool er...

    sorry, but i don't really follow you guys.

    on the bright side, i did figure something out.

    i'll try and attach my project so you can see it work. give me a few days to finish off the tank class. right now i'm having a little trouble with calling the fire method. gonna check around, see what i'm doing wrong, then I'll put it up.

    be back in a few! (i hope. )
    On Error Resume Screaming...

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