Results 1 to 9 of 9

Thread: Jumping and falling...

  1. #1

    Thread Starter
    Lively Member H-Zence's Avatar
    Join Date
    Jul 2002
    Posts
    94

    Jumping and falling...

    Hi all:

    To sum up the whole intro part: I'm working on a game called Era of Sin and I am not using any DirectX or OpenGL, just "good" old-fashioned VB. For the first level, I currently have a set of stairs which Syn, the main character, must obviously walk down. However, when he does, he just appears from step to step without smoothly falling. How can I make sin actually fall instead of "magically appearing" from step to step? Also, I have NO idea how to begin making him jump, so a little code for that would be highly appreciated. Thanks
    www.mindset1.com
    Religious Debate Forums

  2. #2
    Lively Member FireSlash518's Avatar
    Join Date
    Nov 2001
    Posts
    67
    Copied from Lucky's VB gaming site

    Gravity
    Every object (with mass) is attracted to every other object in the universe by the force of gravity. This force is proportional to the mass of the objects, and inversely proportional to the square of the distance between the objects:

    F = G(m1)(m2)/r^2

    Force equals G (the Gravitational Constant, 6.67259e-11) multiplied by the mass of the two objects, and divided by the square of the distance between them. Now, we also know that:

    F = ma

    Force equals mass times acceleration. Using these two equations we can, for example, determine the acceleration imposed by one object on another:

    F = (m2)a = G(m1)(m2)/r^2

    a = G(m1)/r^2

    Note: The mass of an object has no effect on the acceleration it feels! It cancels out.

    Now, if we apply this equation using the values present on the surface of the Earth (mass = 6.02e24kg, radius = 6400km)...

    a = (6.67259e-11)(6.02e24kg)/(6400000m)^2 = 9.81m/s^2

    We get 9.81m/s^2, the correct value for acceleration due to gravity on the Earth's surface! For situations directly on the surface of the Earth (or close to it, ie. In the sky), we can assume that this acceleration is always acting straight down - essentially, the Earth is flat. When we're in space, however, the Earth's mass acts as though it were exerting gravity from a point (its center of mass) - it is now possible to fall around the Earth, ORBIT!

    Click here to download example source code demonstrating how to cope with both of these situations.

    Leader of the Maxoverkill Mods
    -Fire§lash

  3. #3

    Thread Starter
    Lively Member H-Zence's Avatar
    Join Date
    Jul 2002
    Posts
    94
    Unfortunately I've seen that code before. It doesn't help me because it doesn't use gravity the way that I am trying to...let me explain. I know there are simpler ways to program a sprite to fall than that, for example using a for/next loop to do the following:

    Dim ForLoop As Integer
    Dim x as Integer ' Sprite X
    Dim y as Integer ' Sprite Y

    Private Sub Form_Load()
    x = 0
    y = 0
    End Sub

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KEYCODE
    If Keycode = vbKeySpace Then
    For ForLoop = y to y-50 Step -1
    Picture1.Top = ForLoop
    Next ForLoop
    Endif
    End Select
    End Sub

    Yet that still doesn't work. I think that delay might be the problem. Can anyone give me code similar or as simple as that? I know it's possible to do. Thanks!
    www.mindset1.com
    Religious Debate Forums

  4. #4
    Zaei
    Guest
    You need to move back in time and create a game loop. For starters, it should look something like this:
    Code:
    Dim lastTime As Long
    lastTime = GetTickCount()
    While running
      If lastTime + 100 > GetTickCount() Then
         lastTime = GetTickCount()
         '// Do your game stuff
      End If
      Render '// draw the screen
      DoEvents
    Wend
    Simply enough, this loop is your game. Everything happens from here. First, we check to see if we should update our game state (every 100ms). If so, we do that. This is where you would do your falling. Also, you will want to check to see what keys are pressed, etc. Next, after we update, or if we dont update, we re-draw the screen. Once that is done, we let windows so its updating, and start the process over.

    Now, for the falling. At each update, you are going to want to move your character. This is fairly easy. You already have your x and y coordinates, but you need a dx and dy coordinates to show the direction of movement. You update these values when a key is pressed in the correct direction. You also need to know where the blocks are. For every game update, you check to see that the block that the character is over (in the x direction) is directly under his sprite. If not, your y direction should increase. Each game update, simply add dx to x, and dy to y. Your character should then fall.

    Z.

  5. #5

    Thread Starter
    Lively Member H-Zence's Avatar
    Join Date
    Jul 2002
    Posts
    94
    *steam comes out of both ears*

    Grrr...does this mean I have to start over again? Because that really looks like it works...but I don't know where exactly to implement it into my code. I tried, but I ended up freezing my computer (that always seems to happen with While/Wend loops in VB for some reason). Grrr...*confusion*...aahhhh! ...so confused!!!!
    www.mindset1.com
    Religious Debate Forums

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

    Re: Jumping and falling...

    Originally posted by H-Zence

    ...just "good" old-fashioned VB.
    i am just getting into VB game programming and everyone says to use Direct Draw or bitblt. i am researching on how to do that. i want to know how to do VB game programming like you are.

    if there is a website or something post it. if there is too much to post, just email me at [email protected] .

  7. #7
    Zaei
    Guest
    Using a game loop like the one I posted above gives you the most freedom with your game. If your computer seems to freeze when you use a long loop of any kind, it is probably because you dont include a call to DoEvents. DoEvents simply tosses control to Windows, so that it can handle its stuff, instead of your application hogging all of the processor time.

    Any object in a game usually needs a few things. One is obviously its position in the game world. Another is its current direction and speed. Each of these can be repersented by vectors, with an x, a y, and sometimes a z component. Ok, so we have a position, and a direction. Now you have to turn user input into a direction. Simple enough. You can use the GetAsyncKeyState() API function for this. Take a look though the MSDN for more info on the call =). Basically, you use this function to query Windows, and it will tell you if a cerain key is down or not. If the left key is down, then your directional x component should be set to a certain positive value (depending on how fast you wish your sprite to move, make this value smaller or larger). If the right key is pressed, the x component should be negative. if BOTH are pressed, it should be zero. Once you have that, you simply add the directional values to your position at each game update, and you move.

    Z.

  8. #8

    Thread Starter
    Lively Member H-Zence's Avatar
    Join Date
    Jul 2002
    Posts
    94

    Question

    OK, OK, I have the code in Era of Sin now...but there's one major problem...it does absolutely nothing. The game's delay time is not changed at all; the code may has well not be there. I think perhaps I've positioned it wrong? I'm getting to the point where I think someone that knows how to do this should review the code, or perhaps i should try something else. Zaei, if you'd like to (and I'd be forever in your debt if you did), could you please look over the Era of Sin project? I could possibly send you the code for the first level (which is currently in it's own project, and is what I'm having trouble with). If not, please try to do your best to help me. THANK YOU...
    www.mindset1.com
    Religious Debate Forums

  9. #9
    Zaei
    Guest
    You can email me the code at [email protected] , and I will take a look at it.

    Z.

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