Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Jumping equations!!!
#1
Does anyone have any useful equations that could help in terms of making a character jump? Anything from gravity to jump acceleration to anything!
earn.
Reply
#2
gravity is accelleration. -9.8 meters per second, to be exact.

but you need not simulate earth's gravity, and translate meters in pixels and all that crap. just make an x & y, a vx and vy, and an ax and ay, for position, velocity and acceleration. add the velocity to the position each frame, and the acceleration to the velocity. when the character jumps, make vy big enough for him to fly in the air, and ax small enough so that's its not too strong coming down.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#3
Yes but do you have an example equation of what manipulations to put on those variables so that they work?
earn.
Reply
#4
play with it

Code:
type player
  x as single
  y as single
  vx as single
  vy as single
  ax as single
  ay as single
end type

'when player presses left:
x = 1

'when player presses right
x = 2

'when jumping, for kicks:
vy = -2
ay = .01 'guesstimate.  plug it in and see what happens

'every frame
player.x = player.x + player.vx
player.y = player.y + player.vy
player.vx = player.vx + player.ax
player.vy = player.vy + player.ay
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#5
Physical equation of a simple jump :

X = Vx*t + X0 ... Vx is the initial speed in x ( constant )and V0 is the initial position in Y

Y= (m*g*t²)/2 + Vy*t + Y0

m is the weight of the "person" , g is a constant ( 9.81 for the earth ) , Vy is the initial speed in y ( constant ) , and Y0 is the initial position in Y

And t is the time ... (witch is of course NOT a constant ! )
But you have too manipulate the constant's values to obtain something good Big Grin

Hope it was clear ... :???:
have the simplest tastes of the World : I satisfy myself with the best !

http://ToufQb.free.fr
Reply
#6
I worked on it and here is what I have so far Smile

Code:
SCREEN 13
LINE (0, 110)-(320, 110), 1
t = TIMER
gravity = 1
mass = 2
inity = 100
velocity = 150

SLEEP
t = TIMER

donejumping% = 9
DO UNTIL donejumping% = 1
    IF TIMER > t + 1 AND y >= (mass * gravity) + velocity THEN
        donejumping% = 1
    END IF

    t2 = TIMER - t + 1
    oldy = y

    WAIT &H3DA, 8
    CIRCLE (30, inity + oldy - velocity), 7, 0

    y = INT((mass * gravity * t2 * t2 * t2) + velocity / t2)

    WAIT &H3DA, 8, 8
    CIRCLE (30, inity + y - velocity), 7, 4
    'PRINT t2
LOOP
y = inity
earn.
Reply
#7
When I was learning Gravity (some years ago) I coded this simple thingy in QBasic 1.0:

Code:
PRINT "========"
PRINT "GRAVITY!"
PRINT "========"
PRINT "(My first test with a gravity engine)"
PRINT
PRINT
PRINT "Hints:"
PRINT "  * Use o <-, p -> & space (jump)."
PRINT "  * It uses 320x200x16."
PRINT

SLEEP: k$ = INKEY$
SCREEN 7, , 0, 0 ' EGA (320x200x16 x 256Kcolour)

' Draw en GET sprite

DIM Sprite%(65)
CIRCLE (8, 8), 7, 13
PAINT (8, 8), 5, 13
GET (0, 0)-(15, 15), Sprite%(0)

' This read some DATAs to draw the screen with nice tiles

DIM Scr%(19, 11)
FOR y% = 0 TO 11
    FOR x% = 0 TO 19
        READ tile%
        Scr%(x%, y%) = tile%
        SELECT CASE tile%
            CASE 0:
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 1, BF' Nada
            CASE 1:
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 14, BF' Ladrillito
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16), 6
                LINE (x% * 16, y% * 16 + 8)-(x% * 16 + 15, y% * 16 + 8), 6
                LINE (x% * 16 + 8, y% * 16)-(x% * 16 + 8, y% * 16 + 8), 6
                LINE (x% * 16, y% * 16 + 8)-(x% * 16, y% * 16 + 15), 6
            CASE 2:
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 2, BF
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 12
                LINE (x% * 16 + 15, y% * 16)-(x% * 16, y% * 16 + 15), 12
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 10, B
            CASE 3:
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 11, BF
                LINE (x% * 16, y% * 16)-(x% * 16 + 15, y% * 16 + 15), 3, B
                CIRCLE (x% * 16 + 8, y% * 16 + 8), 7, 9
                PAINT (x% * 16 + 8, y% * 16 + 8), 1, 9
        END SELECT
    NEXT
NEXT

' No buffering, lotsa flicker, but I don't give a damn ;)

x% = 16000
y% = 0
cx% = x%: cy% = y%
vy% = 0
g% = 9
d% = 0

' Begin

terminado% = 0

WHILE NOT terminado%
    ' Gravity: We accelerate our ball
    vy% = vy% + g%
    ' Collision... Should we stop the ball?
    ' We just look if there's a tile in the supposed next position of
    ' our ball.
    ' If there is, I adjust the ball to a correct position (so it doesn't
    ' collide with the tile), and I make it bounce using d%\30
    
    ' Bounce on floor
    ' ===============
    IF vy% > 0 THEN
        IF x% MOD 1600 = 0 THEN
            IF Scr%(x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 THEN
                y% = ((y% + vy%) \ 1600) * 1600' Esto ajusta al borde superior de la 'casilla'.
                vy% = -d% \ 30
                d% = 0
            END IF
        ELSE
            IF Scr%(x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 OR Scr%(1 + x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 THEN
                y% = ((y% + vy%) \ 1600) * 1600' Esto ajusta al borde superior de la 'casilla'.
                vy% = -d% \ 30
                d% = 0
            END IF
      END IF

    ' Bounce on ceilings
    ' ==================
    ELSEIF vy% < 0 THEN
        IF x% MOD 1600 = 0 THEN
            IF Scr%(x% \ 1600, (y% + vy%) \ 1600) <> 0 THEN
                y% = (1 + (y% + vy%) \ 1600) * 1600' Esto ajusta al borde superior de la 'casilla'.
                vy% = d% \ 30
                d% = 0
            END IF
        ELSE
            IF Scr%(x% \ 1600, (y% + vy%) \ 1600) <> 0 OR Scr%(1 + x% \ 1600, (y% + vy%) \ 1600) <> 0 THEN
                y% = (1 + (y% + vy%) \ 1600) * 1600' Esto ajusta al borde superior de la 'casilla'.
                vy% = d% \ 30
                d% = 0
            END IF
        END IF
    END IF
    ' Draw
    rx% = x% \ 100: ry% = y% \ 100
    rcx% = cx% \ 100: rcy% = cy% \ 100
    LINE (rcx%, rcy%)-(rcx% + 15, rcy% + 15), 1, BF
    PUT (rx%, ry%), Sprite%, OR
    cx% = x%: cy% = y%
    ' Move ball vertically:
    y% = y% + vy%: IF y% < 0 THEN y% = 0: vy% = 0' Para los saltos salvajes.
    d% = d% + ABS(vy%)
    LOCATE 25, 1: PRINT USING "x:### y:### vy:#### d:###"; x% \ 100; y% \ 100; vy% \ 100; d% \ 100;
    ' This is just for bounces. In a platform game it is not used. It is just
    ' a variable that stores the height from where the ball falls to calculate
    ' the bouncing speed:
  
    IF vy% < 7 AND vy% > -7 THEN d% = 0
  
    ' Sync
    WAIT &H3DA, 8
    WAIT &H3DA, 8, 8
  
    ' Keyboard
    k% = INP(&H60)
    SELECT CASE k%
        CASE 1
            terminado% = NOT terminado%
        CASE 24
            ' Let's check if we can move
            puedo% = NOT 0
            IF x% = 0 THEN puedo% = 0
            IF x% MOD 1600 = 0 THEN       ' Tile border
                IF y% MOD 1600 = 0 THEN    ' I just look at the tile to the left
                    IF Scr%((x% \ 1600) - 1, y% \ 1600) <> 0 THEN puedo% = 0
                ELSE                       ' I look two tiles:x-1,y and x-1,y+1
                    IF Scr%((x% \ 1600) - 1, y% \ 1600) <> 0 OR Scr%((x% \ 1600) - 1, (y% \ 1600) + 1) <> 0 THEN puedo% = 0
                END IF
            END IF
            IF puedo% THEN
                x% = x% - 100
            END IF
    
        CASE 25
            ' Vemos si podemos mover
            puedo% = NOT 0
            IF x% = 30400 THEN puedo% = 0
            IF x% MOD 1600 = 0 THEN
                IF y% MOD 1600 = 0 THEN
                    IF Scr%((x% \ 1600) + 1, y% \ 1600) <> 0 THEN puedo% = 0
                ELSE
                    IF Scr%((x% \ 1600) + 1, y% \ 1600) <> 0 OR Scr%((x% \ 1600) + 1, (y% \ 1600) + 1) <> 0 THEN puedo% = 0
                END IF
            END IF
            IF puedo% THEN
                x% = x% + 100
            END IF
        CASE 57
            ' Vemos si salto o no:
            IF x% MOD 1600 = 0 THEN
                IF Scr%(x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 THEN
                    '?((y%+vy%)\1600)*1600
                    y% = ((y% + vy%) \ 1600) * 1600 ' Adjust to upper bound of our tile
                    vy% = -500
                    d% = 0
                END IF
            ELSE
                IF Scr%(x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 OR Scr%(1 + x% \ 1600, 1 + (y% + vy%) \ 1600) <> 0 THEN
                    y% = ((y% + vy%) \ 1600) * 1600
                    vy% = -500
                    d% = 0
                END IF
            END IF
    END SELECT
    ' Clear buffer
    k$ = INKEY$
WEND

SCREEN 0, 0, 0, 0: WIDTH 80, 25

' Map:

DATA 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DATA 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DATA 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
DATA 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DATA 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2
DATA 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 1, 0, 0, 0, 1, 2
DATA 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 2
DATA 2, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1
DATA 1, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 2
DATA 1, 0, 0, 3, 2, 1, 2, 0, 2, 2, 0, 0, 3, 0, 0, 3, 3, 0, 2, 1
DATA 1, 0, 0, 3, 1, 2, 3, 0, 2, 2, 0, 0, 3, 0, 0, 3, 3, 0, 2, 2
DATA 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

' Jua jua :) bebe konga.

You may learn something Wink
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#8
Learn Spanish? j/k
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#9
Nice game Smile

Okay I editted the code a little from my old program. Anyways you need CosmoX to run this, but I'm just trying to figure out why at the very beginning of the jump, the guy drops down about 10 pixels and then starts going upwards? Anyone know?

Code:
'$INCLUDE: 'cosmox/cosmox.bi'
CSInstallKeyBoard
isxms% = CSDetectXMS
IF isxms% = 0 THEN
    PRINT "Get a better goddamn computer."
    CSRemoveKeyBoard
    END
END IF
xhandle% = CSAllocateXMS(64)

SCREEN 13
LINE (0, 110)-(320, 110), 1
gravity% = 1
mass% = 5
inity% = 100
velocity% = 200
y% = inity%
t! = TIMER

X% = 5
jumping% = 0
DO
    oldy% = y%
    oldx% = X%

    WAIT &H3DA, 8
    CIRCLE (oldx%, oldy% - 7), 7, 0
    PAINT (oldx%, oldy% - 7), 0

    newtimer! = TIMER
    LOCATE 1, 1: PRINT y%
    IF jumping% = 1 THEN
        t2! = newtimer! - t! + 1

        y% = inity% + (mass% * gravity%) + INT((mass% * gravity% * t2! * t2! * t2!) + velocity% / t2!) - velocity% + 1

        LOCATE 4, 1: PRINT inity% + (mass% * gravity%) + (mass% * gravity%) + velocity% / t2! - velocity%

        IF newtimer! > t! + 1 AND y% >= inity% + (mass% * gravity%) THEN
            y% = inity%
            jumping% = 0
            t! = TIMER
        END IF
    END IF

    IF CSKey(1) THEN EXIT DO
    IF CSKey(75) THEN IF X% > 10 THEN X% = X% - 3
    IF CSKey(77) THEN IF X% < 310 THEN X% = X% + 3
    IF CSKey(72) THEN
        t! = TIMER
        jumping% = 1
    END IF

    WAIT &H3DA, 8, 8
    CIRCLE (X%, y% - 7), 7, 1
    PAINT (X%, y% - 7), 9, 1
LOOP
CSDeallocateXMS (xhandle%)
CSRemoveKeyBoard

And never mind the XMS crap there, I thought I needed that when the program kept saying "overflow", but I realized I forgot to put the right signs after the right variables...
earn.
Reply
#10
Oh now that I understand the equation a little more, and what was wrong, it works pretty nicely. Here ya are Smile

Code:
'$INCLUDE: 'cosmox/cosmox.bi'
CSInstallKeyBoard
isxms% = CSDetectXMS
IF isxms% = 0 THEN
    PRINT "Get a better goddamn computer."
    CSRemoveKeyBoard
    END
END IF
xhandle% = CSAllocateXMS(64)

SCREEN 13
gravity% = 3
mass% = 3
inity% = 150
velocity% = 200
y% = inity%
t! = TIMER


LINE (0, inity% + 1)-(320, inity% + 5), 2, BF
PAINT (0, inity% + 6), 10, 2
PAINT (0, inity%), 77, 2

X% = 5
jumping% = 0
DO
    oldy% = y%
    oldx% = X%

    WAIT &H3DA, 8
    CIRCLE (oldx%, oldy% - 7), 7, 77
    PAINT (oldx%, oldy% - 7), 77

    newtimer! = TIMER
    IF jumping% = 1 THEN
        t2! = newtimer! - t! + 1

        y% = inity% - (mass% * gravity%) + INT((mass% * gravity% * t2! * t2! * t2!) + velocity% / t2!) - velocity% + 1

        IF y% >= inity% THEN
            y% = inity%
            jumping% = 0
            t! = TIMER
        END IF
    END IF

    IF CSKey(1) THEN EXIT DO
    IF CSKey(75) AND X% > 10 THEN X% = X% - speed%
    IF CSKey(77) AND X% < 310 THEN X% = X% + speed%
    IF CSKey(29) THEN speed% = 2 ELSE speed% = 1
    IF CSKey(72) AND jumping% = 0 THEN
        t! = TIMER
        jumping% = 1
    END IF

    WAIT &H3DA, 8, 8
    CIRCLE (X%, y% - 7), 7, 1
    PAINT (X%, y% - 7), 9, 1
LOOP
CSDeallocateXMS (xhandle%)
CSRemoveKeyBoard
earn.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)