Here's a character controller for you to tinker with. It's not as simple as I'd hoped but I've tried to put all the basics for a platformer in there. I've left moving platforms out for now but if you get stuck I'm sure I'll hear from you
// Project: character_controller
// Created: 2014-12-21
// set window properties
SetWindowTitle( "character_controller" )
SetWindowSize( 1024, 768, 0 )
SetPrintSize( 16 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
// display sprite shapes (not just physics sprites)
SetPhysicsDebugOn()
// create a character controller
gosub init_character_controller
char as charType
char = CreateCharacterController(512, 600, 20, 40)
// create some platforms
CreatePlatform(0, 1024, 10, 0, 620)
CreatePlatform(0, 300, 10, 400, 300)
CreatePlatform(0, 300, 10, 0, 520)
// create a ladder
CreateLadder(0, 30, 320, 600, 300)
do
Print( "fps:"+str(ScreenFPS(), 0) )
`Print(GetRawLastKey())
ControlCharacter(char, GetRawKeyState(65), GetRawKeyState(68), GetRawKeyState(87), GetRawKeyState(83), GetRawKeyPressed(87))
Sync()
loop
function ControlCharacter(char ref as charType, moveL as float, moveR as float, moveU as float, moveD as float, jump as float)
// use input to set velocity of character in X
move# = moveR - moveL
char.vx = char.vx + move# * char.speedX * tbm
if char.vx>char.maxVx then char.vx = char.maxVx
if char.vx<-char.maxVx then char.vx = -char.maxVx
// stop character when no input for X
if abs(move#)<0.1
char.vx = char.vx * (1.0 - 0.2 * tbm)
endif
// add gravity to vertical velocity
char.vy = char.vy + gravity * tbm
if char.vy>char.maxVy then char.vy = char.maxVy
// Ladder
ladder = GetSpriteHitGroup(g_ladder, char.x, char.y)
if ladder=0 then ladder = GetSpriteHitGroup(g_ladder, char.x, char.y + char.height*0.49)
if ladder>0
// move up/down ladder
char.vy = (moveD - moveU) * char.speedY
// move to centre of ladder when not moving in X
if abs(move#)<0.1
lx# = GetSpriteXByOffset(ladder)
dx# = lx# - char.x
char.x = char.x + dx#*0.1*tbm
endif
endif
// Check for ground
ground = PhysicsRayCastGroup(g_platform, char.x, char.y, char.x, char.y + char.height*0.5)
if ground>0
if char.vy>-0.5
if ladder=0
// reset position and velocity
char.y = GetRayCastY() - char.height*0.5
char.vy = 0
endif
endif
endif
// Jump
if ladder=0
if ground>0
if jump>0.5
// set jump velocity
char.vy = char.jump
endif
endif
endif
// add velocity to sprite position
char.x = char.x + char.vx * tbm
char.y = char.y + char.vy * tbm
// smooth move to desired position
cx# = GetSpriteXByOffset(char.spr)
cy# = GetSpriteYByOffset(char.spr)
dx# = char.x - cx#
dy# = char.y - cy#
newX# = cx# + dx#*0.5*tbm
newY# = cy# + dy#*0.5*tbm
SetSpritePositionByOffset(char.spr, newX#, newY#)
endfunction
init_character_controller:
// create groups
#constant g_platform 0
#constant g_ladder 1
// create velocity constant
#constant gravity 0.5
// create timer based movement
#constant tbm GetFrameTime()*60.0
// create character controller user defined type
type charType
spr as integer
cSpr as integer
x as float
y as float
vx as float
vy as float
maxVx as float
maxVy as float
width as float
height as float
jump as float
speedX as float
speedY as float
endtype
return
function CreateCharacterController(x as float, y as float, width as float, height as float)
char as charType
spr = CreateSprite(0)
SetSpriteVisible(spr, 0)
SetSpriteSize(spr, width, height)
SetSpriteShape(spr, 2)
SetSpritePositionByOffset(spr, x, y)
// set some initial values
char.spr = spr
char.x = x
char.y = y
char.width = width
char.height = height
// these can be used to change how your character moves
char.speedX = 1.5
char.speedY = 1.5
char.jump = -10
char.maxVx = 5
char.maxVy = 12
endfunction char
function CreatePlatform(img, width as float, height as float, x as float, y as float)
spr = CreateSprite(img)
SetSpriteSize(spr, width, height)
SetSpritePosition(spr, x, y)
SetSpritePhysicsOn(spr, 1)
SetSpriteGroup(spr, g_platform)
endfunction
function CreateLadder(img, width as float, height as float, x as float, y as float)
spr = CreateSprite(img)
SetSpriteSize(spr, width, height)
SetSpritePosition(spr, x, y)
SetSpritePhysicsOn(spr, 1)
SetSpriteGroup(spr, g_ladder)
endfunction
Markus, Josh's code actually does skip the physics system if it's used every frame you are right but for just repositioning a sprite for an occasional teleport it should be fine.