It's 425 lines, but you can ignore most of it... I started on a 2d tutorial, but then lost all the code. Besides, I couldn't make it work, for some reason :S The concept is fairly easy to understand, hopefuly this will enlighten you

Once you understand it, you shouldn't have any trouble converting it to 2d
remstart
project: jumping tutorial written for Dextro
code by: Hawkeye (email - [email protected], msn is the same address)
started: 5-1-06 17:08
version: v0.7
The important stuff is inside the function HandlePlayer(). Don't bother with the stuff after all the functions,
that's just my standard startup codez. Basically, you're using a velocity vector to control the up-and-down
movements. Jumping is just a suspension of gravity. It's easy once you get the hang of it ;)
remend
sync on : sync rate 0
backdrop on : color backdrop rgb(0,0,0)
hide mouse
autocam off
set display mode 800,600,32
set window on
set window position 120,100
`// teh constants and globals
#constant C_STANDARDGRAVITY -100
#constant C_FLOORHEIGHT 0
global spacehit as boolean
`// teh types
type TYPE_XYZ
x as float
y as float
z as float
endtype
type TYPE_PLAYER
obj as integer
vel as TYPE_XYZ
movespeed
jumpstrength
endtype
`// declare
p as TYPE_PLAYER
p.obj = FindOpenObject()
make object cone p.obj, 10
p.movespeed = 100
p.jumpstrength = 50 :`try messing with this number a bit for different results :)
global world as integer : world = findopenobject()
make object box world, 100, 1, 100
position object world, 0, -6, 0
global frameTime# = 1.0
startTime = timer()
` / / M A I N L O O P \ \
do
set cursor 0,0
position mouse 400,400
goSub HandleDeltaTime
HandlePlayer()
HandleCamera()
PrintInfo()
sync
loop
`// gosub routines
HandleDeltaTime:
frameTime# = (frameTime# * 0.8) + ((timer() - startTime) * 0.2)
startTime = timer()
return
`// function routines
function HandlePlayer()
`basic move stuff, you should understand this. Don't worry about ths " * (frameTime# * 0.001)" part,
`that's just my deltatime system (well, actually it's Nicolas Thompson's routine :-)
if keystate( DIK_W )=1 then move object p.obj, p.movespeed * (frameTime# * 0.001)
if keystate( DIK_S )=1 then move object p.obj, -p.movespeed * (frameTime# * 0.001)
if keystate( DIK_A )=1 then move object left p.obj, p.movespeed * (frameTime# * 0.001)
if keystate( DIK_D )=1 then move object right p.obj, p.movespeed * (frameTime# * 0.001)
`if we've hit the floor, then reset the player's y velocity to zero. MAKE SURE TO DO THIS BEFORE THE JUMPING STUFF!
if opy(p.obj) <=0 : p.vel.y = 0 : position object p.obj, opx(p.obj), 0, opz(p.obj) : endif
`jumping - if we're on the floor, then we can jump.
if keystate( DIK_SPACE )=1 and spacehit=0 and opy(p.obj) <=0
`this is the important thing here - set the velocity to the player's jumpstrength. We'll move him later on.
p.vel.y = p.jumpstrength
spacehit=1
endif
if keystate( DIK_SPACE )=1 and spacehit=1 then spacehit=0
`gravity - if the player is in the air, then apply gravitational force to the player's y velocity
`remember that we defined gravity as a negative force! therefore we're using INC instead of DEC
if opy(p.obj) > 0 then inc p.vel.y, C_STANDARDGRAVITY * (frameTime# * 0.001)
`now, move the player as according to his Y VECTOR VELOCITY
move object up p.obj, p.vel.y * (frameTime# * 0.001)
remstart
understand all of that? I hope so. In short, you have to keep a velocity variable for your player. When
he hits the floor, you cancel the velocity in that direction so he doesn't fly through the walls. When he
wants to jump, you set the y velocity to his jumpstrength - jumping is simply a suspension of gravity. It's
the only way to fly, man...
remend
endfunction
function HandleCamera()
set camera to follow opx(p.obj), opy(p.obj), opz(p.obj), oay(p.obj), 25, 6, 10, 0
point camera opx(p.obj), opy(p.obj), opz(p.obj)
endfunction
function PrintInfo()
print "framerate: " + str$( screen fps() )
print "p.vel.y: " + str$( p.vel.y )
endfunction
` /////////////////////////////////////////////////////////////////
`// random function stuph - ignore all of this for now
function FindOpenObject()
repeat
inc i
if object exist(i)=0 then found=1
until found
endfunction i
function FindOpenMesh()
repeat
inc i
if mesh exist(i)=0 then found=1
until found
endfunction i
function FindOpenImage()
repeat
inc i
if image exist(i)=0 then found=1
until found
endfunction i
function FindOpenSprite()
repeat
inc i
if sprite exist(i)=0 then found=1
until found
endfunction i
function FindOpenSound()
repeat
inc i
if sound exist(i)=0 then found=1
until found
endfunction i
function opx(asdf)
local r as float
r = object position x(asdf)
endfunction r
function opy(asdf)
local r as float
r = object position y(asdf)
endfunction r
function opz(asdf)
local r as float
r = object position z(asdf)
endfunction r
function oax(asdf)
local r as float
r = object angle x(asdf)
endfunction r
function oay(asdf)
local r as float
r = object angle y(asdf)
endfunction r
function oaz(asdf)
local r as float
r = object angle z(asdf)
endfunction r
`kudos to philip for the following code block :)
` This code was downloaded from The Game Creators
` It is reproduced here with full permission
` http://www.thegamecreators.com
Rem PJY - simple function to calculate the length of a 3d vector using DBPro's inbuilt 3d maths commands
FUNCTION threeD_distance(x1# AS float, x2# AS float, y1# AS float, y2# AS float, z1# AS float, z2# AS float)
temp = 1
null = make vector3(temp)
set vector3 temp, x1# - x2#, y1# - y2#, z1# - z2#
length_vector = abs(length vector3(temp))
null = delete vector3(temp)
ENDFUNCTION length_vector
`using sparky's collision dll? then uncomment the line below
`delete memblock 10
`using newton physics? then uncomment the line below
`delete mesh 10
DIK_Constants:
`some CONTSTANTS for the keycodes to make life a little easier
` This code was downloaded from The Game Creators
` It is reproduced here with full permission
` http://www.thegamecreators.com
#constant DIK_0 11
#constant DIK_1 2
#constant DIK_2 3
#constant DIK_3 4
#constant DIK_4 5
#constant DIK_5 6
#constant DIK_6 7
#constant DIK_7 8
#constant DIK_8 9
#constant DIK_9 10
#constant DIK_A 30
#constant DIK_ABNT_C1 115
#constant DIK_ABNT_C2 126
#constant DIK_ADD 78
#constant DIK_APOSTROPHE 40
#constant DIK_APPS 221
#constant DIK_AT 145
#constant DIK_AX 150
#constant DIK_B 48
#constant DIK_BACK 14
#constant DIK_BACKSLASH 43
#constant DIK_BACKSPACE 14
#constant DIK_C 46
#constant DIK_CALCULATOR 161
#constant DIK_CAPITAL 58
#constant DIK_CAPSLOCK 58
#constant DIK_CIRCUMFLEX 144
#constant DIK_COLON 146
#constant DIK_COMMA 51
#constant DIK_CONVERT 121
#constant DIK_D 32
#constant DIK_DECIMAL 83
#constant DIK_DELETE 211
#constant DIK_DIVIDE 181
#constant DIK_DOWN 208
#constant DIK_DOWNARROW 208
#constant DIK_E 18
#constant DIK_END 207
#constant DIK_EQUALS 13
#constant DIK_ESCAPE 1
#constant DIK_F 33
#constant DIK_F1 59
#constant DIK_F2 60
#constant DIK_F3 61
#constant DIK_F4 62
#constant DIK_F5 63
#constant DIK_F6 64
#constant DIK_F7 65
#constant DIK_F8 66
#constant DIK_F9 67
#constant DIK_F10 68
#constant DIK_F11 87
#constant DIK_F12 88
#constant DIK_F13 100
#constant DIK_F14 101
#constant DIK_F15 102
#constant DIK_G 34
#constant DIK_GRAVE 41
#constant DIK_H 35
#constant DIK_HOME 199
#constant DIK_I 23
#constant DIK_INSERT 210
#constant DIK_J 36
#constant DIK_K 37
#constant DIK_KANA 112
#constant DIK_KANJI 148
#constant DIK_L 38
#constant DIK_LALT 56
#constant DIK_LBRACKET 26
#constant DIK_LCONTROL 29
#constant DIK_LEFT 203
#constant DIK_LEFTARROW 203
#constant DIK_LMENU 56
#constant DIK_LSHIFT 42
#constant DIK_LWIN 219
#constant DIK_M 50
#constant DIK_MAIL 236
#constant DIK_MEDIASELECT 237
#constant DIK_MEDIASTOP 164
#constant DIK_MINUS 12
#constant DIK_MULTIPLY 55
#constant DIK_MUTE 160
#constant DIK_MYCOMPUTER 235
#constant DIK_N 49
#constant DIK_NEXT 209
#constant DIK_NEXTTRACK 153
#constant DIK_NOCONVERT 123
#constant DIK_NUMLOCK 69
#constant DIK_NUMPAD0 82
#constant DIK_NUMPAD1 79
#constant DIK_NUMPAD2 80
#constant DIK_NUMPAD3 81
#constant DIK_NUMPAD4 75
#constant DIK_NUMPAD5 76
#constant DIK_NUMPAD6 77
#constant DIK_NUMPAD7 71
#constant DIK_NUMPAD8 72
#constant DIK_NUMPAD9 73
#constant DIK_NUMPADCOMMA 179
#constant DIK_NUMPADENTER 156
#constant DIK_NUMPADEQUALS 141
#constant DIK_NUMPADMINUS 74
#constant DIK_NUMPADPERIOD 83
#constant DIK_NUMPADPLUS 78
#constant DIK_NUMPADSLASH 181
#constant DIK_NUMPADSTAR 55
#constant DIK_O 24
#constant DIK_OEM_102 86
#constant DIK_P 25
#constant DIK_PAUSE 197
#constant DIK_PERIOD 52
#constant DIK_PGDN 209
#constant DIK_PGUP 201
#constant DIK_PLAYPAUSE 162
#constant DIK_POWER 222
#constant DIK_PREVTRACK 144
#constant DIK_PRIOR 201
#constant DIK_Q 16
#constant DIK_R 19
#constant DIK_RALT 184
#constant DIK_RBRACKET 27
#constant DIK_RCONTROL 157
#constant DIK_RETURN 28
#constant DIK_RIGHT 205
#constant DIK_RIGHTARROW 205
#constant DIK_RMENU 184
#constant DIK_RSHIFT 54
#constant DIK_RWIN 220
#constant DIK_S 31
#constant DIK_SCROLL 70
#constant DIK_SEMICOLON 39
#constant DIK_SLASH 53
#constant DIK_SLEEP 223
#constant DIK_SPACE 57
#constant DIK_STOP 149
#constant DIK_SUBTRACT 74
#constant DIK_SYSRQ 183
#constant DIK_T 20
#constant DIK_TAB 15
#constant DIK_U 22
#constant DIK_UNDERLINE 147
#constant DIK_UNLABELED 151
#constant DIK_UP 200
#constant DIK_UPARROW 200
#constant DIK_V 47
#constant DIK_VOLUMEDOWN 174
#constant DIK_VOLUMEUP 176
#constant DIK_W 17
#constant DIK_WAKE 227
#constant DIK_WEBBACK 234
#constant DIK_WEBFAVORITES 230
#constant DIK_WEBFORWARD 233
#constant DIK_WEBHOME 178
#constant DIK_WEBREFRESH 231
#constant DIK_WEBSEARCH 229
#constant DIK_WEBSTOP 232
#constant DIK_X 45
#constant DIK_Y 21
#constant DIK_YEN 125
#constant DIK_Z 44
return

I am but mad north north-west; when the wind is southerly I know a hawk from a handsaw - Hamlet, Hamlet