TGC Codebase Backup



Simple Jumping/Gravity by RUCCUS

1st Mar 2005 20:07
Summary

Finally, a code base entry that can help the newcommers get what they want. Simple jumping up and down, moving around, and that all important gravitational pull!



Description

SJG is a very simple snippet to include into your game. It's very well commented and should be very easy to comprehend for even the newest programmer. Here's a basic list of what it contains:
- A jumping feature that includes speed set for the jump and height set for the jump
- The ability to use this code with not only matrix grounds but object grounds aswell
- The ability to move as you jump
- The disablement of multijumping
- A simple gravity feature that pulls you down whenever you aren't at the desired ground level.

Be sure to look for advanced gravitational pull featuring increased and decreased speed in jumps and falls and objects having a breaking point and weight feature in the next version, Advanced Jumping/Gravity. Coming soon to a code base near you!



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Simple Jumping
REM By RUCCUS of The Game Creators (2005-Present)

REMSTART Have you been looking all over the place for a good
jumping and gravity example? Look no further. Simply implement
the jump and gravity code from this example into your game and
watch as your character jumps and obeys the laws of gravity!
Goodluck!                                               Remend

REM Start the syncronization process
SYNC ON    : SYNC RATE 300
REM Create, colour and position a player, box and floor
MAKE OBJECT BOX 1,10,10,10
MAKE OBJECT BOX 2,300,.1,300
MAKE OBJECT BOX 3,20,20,20
POSITION OBJECT 1,0,10,0
POSITION OBJECT 2,0,0,0
POSITION OBJECT 3,0,0,30
COLOR OBJECT 1,RGB(250,250,000)
COLOR OBJECT 2,RGB(000,000,255)
COLOR OBJECT 3,RGB(000,255,255)
REM Define the needed variables
JUMPVELOCITY# = 3
HEIGHT# = 0
JUMPING# = 0
REM Position the camera so we can see everything perfectly
POSITION CAMERA 0,100,-200
REM Start the main loop
DO
REM Jumping: Step 1
REMSTART If the user isn't jumping (if the Jumping var is set to
0) then set their jump velocity or speed to 3 units per second. REMEND
IF JUMPING# = 0 THEN JUMPVELOCITY# = 3
IF OBJECT POSITION Y(1) > 1 THEN MOVE OBJECT DOWN 1,1
REM Store object 1's last known "good" coordinates
OLDX# = OBJECT POSITION X(1)
OLDY# = OBJECT POSITION Y(1)
OLDZ# = OBJECT POSITION Z(1)
REM Set the height variable to the user's current y position
HEIGHT# = OBJECT POSITION Y(1)
REM Set up the main controls
IF UPKEY()=1 THEN MOVE OBJECT 1,.5
IF DOWNKEY()=1 THEN MOVE OBJECT 1,-.5
IF LEFTKEY ()=1 THEN YROTATE OBJECT 1,OBJECT ANGLE Y(1)-1
IF RIGHTKEY()=1 THEN YROTATE OBJECT 1,OBJECT ANGLE Y(1)+1
REM Jumping: Step 2
REMSTART If the user presses the spacekey and   they     aren't
currently jumping, set the height var to their  y   position
and set the Jumping var to 1 so we know they are now jumping.     REMEND
IF SPACEKEY()=1  AND JUMPING# = 0 THEN HEIGHT# = OBJECT POSITION Y(1):JUMPING# = 1
REMSTART Check if Jumping is set to 1, if so move the user up
at a rate equal to their Jump Velocity.                  REMEND
IF JUMPING# = 1 THEN MOVE OBJECT UP 1,JUMPVELOCITY#
REMSTART If the user's position is grater than 100, make the
falling variable equal to 1 and set their Jump Velocity to a
negative so they being to move down. Then move them down at the
Jump Velocity rate.                                REMEND
IF OBJECT POSITION Y(1) >= 100 THEN FALLING# = 1 : JUMPVELOCITY# = -3 :  MOVE OBJECT DOWN 1,JUMPVELOCITY#
REMSTART If the user's position is less than 0 (ground level)
then reposition them so they are back to ground level.  REMEND
IF OBJECT POSITION Y(1)< 0 THEN POSITION OBJECT 1,OBJECT POSITION X(1),OLDY#,OBJECT POSITION Z(1)
REMSTART Finally, if the user collides with any object, they must
not be jumping, thus set their Jumping var to 0 so they can jump
again. :)                                          REMEND
IF OBJECT COLLISION (1,0) THEN JUMPING# = 0
REM End the loop and refresh the screen.
SYNC
LOOP
REM LOOK FORWARD TO VERSION 2, IT'LL BE BETTER! :)
REM     - RUCCUS