interesting math, rook

, but i don't think i'll need precise circular collision in a platform game with blocks lol. anyway, since this is turning to a modify-the-code thread, for anyone following/using this code, i've added a double-jump feature; new things are indicated with "*****":
Code: Select all
;-Physics variables
;Gravity is based on the largest value that will keep the velocity <=43 pixels/frame when player is dropped
;from a height of 480 pixels (screen height). If velocity>43 the player will move through a platform and
;collide with the next platform instead. Using formula "Vf^2 = Vi^2 + 2 * g * d" where Vi = 0,d = 480,Vf = 43
;and solving for "g = (Vf^2) / (2 * d)". This produces a range of 0<Gravity<=1.925. The value is further
;limited by the method for collision detection. It limits the velocity that can be obtained when the player
;is "not moving" for a single frame while on top of a platform (this value is 0.5 which will be rounded up to
;a 1 because of floating point to integer conversion). The change in velocity is greatest during a collison
;because the direction of the velocity changes. It uses formula "(dV) = gt and/(1 + 1 / c)" where
;dV = 0.5,t = 1,c = 5 (higher values of c represent a less elastic collision), simplifying and solving
;for "g = .6". The value for g is therefore now limited to the more restrictive range of 0<Gravity<=0.6
;due to the collision method used.
;Velocity for a Jump is based on what is range of values that will not cause the velocity to exceed
;43 pixels/frame if the player Jumps from the highest point on the screen (y = 0). Solving the above equation
;for Vi gives us "Vi = sqr(Vf^2 - 2 * g * d)". Substituting we get "Vi = sqr(1849 - 960 * g)". The legal
;values of velocity are 0 to the max value based on substituting our Gravity value and solving the equation
;for Vi.
#Elasticity = 1.0 / 5.0 ;what fraction of velocity remains after a collision
#Gravity = 0.5 + 0.5 * #Elasticity ;legal values are 0<Gravity<=0.6
#VelocityJump = 13 ;if (maxVelocity,Gravity)=(mV,g), maximum legal values are (35,0.6)<=(mV,g)<(43,0)
Gravity.f = #Gravity ;
Velocity.f = 0
newVelocity.f = 0
;***** - Double jump variables
DJTimer = 30 ;interval between double jumps
DJump = 2 ;2 jumps initially available
;-Load the level
Procedure CountLines(file$)
If ReadFile(0,file$)
l = Lof(0)
m = AllocateMemory(l)
ReadData(0,m,l)
m$ = PeekS(m)
FreeMemory(m)
CloseFile(0)
EndIf;
ProcedureReturn CountString(m$,Chr(10))/2
EndProcedure;
n = CountLines("Level.txt")-1
Dim p.Point(n)
ReadFile(1,"Level.txt")
For I=0 To n
p(I)\x=Val(ReadString(1))
p(I)\y=Val(ReadString(1))
Next
CloseFile(1)
;-Prepare the screen
InitKeyboard()
InitSprite()
OpenScreen(640,480,32,"")
SetFrameRate(60)
;-Prepare the sprites
Enumeration
#Player
#Platform
EndEnumeration
LoadSprite(#Player,"Eraser.bmp")
LoadSprite(#Platform,"Platform.bmp")
playerHeight = SpriteHeight(#Player)
playerWidth = SpriteWidth(#Player)
platformHeight = SpriteHeight(#Platform)
platformWidth = SpriteWidth(#Platform)
diffWidth = (playerWidth - platformWidth) / 2
diffHeight = (playerHeight - platformHeight) / 2
Delay(2000)
y = -200
x = 30
Repeat
FlipBuffers()
ClearScreen(RGB(0,0,0))
;-Display the level
For I = 0 To n
xx = p(I)\x
yy = p(I)\y
DisplayTransparentSprite(#Platform,xx,yy)
Next
DisplayTransparentSprite(#Player,x,y)
;-Jump
;If KeyboardPushed(#PB_Key_Space): Power = 10: EndIf
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Space)
If DJump = 2: Velocity = #VelocityJump: DJump = 1: EndIf ;***** 1 jump remaining
If DJTimer <= 0 And DJump = 1: Velocity = (#VelocityJump / 2): DJTimer = 30: DJump = 0: EndIf ;***** if the timer has reached 0
;and there is 1 remaining jump,
;jump again (with half the velocity)
;and reset settings
EndIf
;-Jump physics
;Velocity = Power - (Gravity * FlightTime) ;Adaptation of "v = u + at"
Velocity - Gravity ;Adaptation of "v=u+dV", change in velocity(dV) = at, where t=1
Ground - 1 ;if equal to 1 then player is on a surface he can jump from
y - Velocity
;-Horizontal movement
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
x - 5
ElseIf KeyboardPushed(#PB_Key_Right)
x + 5
EndIf
;WriteStringN(0,StrF(Velocity,4)+" ("+Str(x)+","+Str(y)+")")
StartDrawing(ScreenOutput())
DrawText(1, 1, Str(DJump))
StopDrawing()
If Velocity<>0: If DJTimer => 0: DJTimer-1: EndIf: EndIf ;***** the timer goes down if we are not on a platform
;-Collision detection
For I = 0 To n
xx = p(I)\x
yy = p(I)\y
If (xx - 19)<x And x<(xx + 15) And (yy - playerHeight)<y And y<(yy + platformHeight) And Velocity<0 ;Collision with TOP of platform
Ground=1
newVelocity = -Velocity * #Elasticity
y = yy - playerHeight
DJump = 2 ;***** 2 jumps available
DJTimer = 30 ;***** reset the delay for the second jump
EndIf
If (xx - 19)<x And x<(xx + 15) And (yy - playerHeight)<y And y<(yy + platformHeight) And Velocity>0 ;Collision with BOTTOM of platform
newVelocity= - Velocity * #Elasticity
y = yy + platformHeight
EndIf
If (xx - diffWidth)<x And x<(xx + platformWidth) And (yy - playerHeight)<y And y<(yy+platformHeight) ;Collision with RIGHT of platform
x = xx + platformWidth
EndIf
If (xx - playerWidth)<x And x<(xx - diffWidth) And (yy - playerHeight)<y And y<(yy+platformHeight) ;Collision with LEFT of platform
x = xx - playerWidth
EndIf
Next I
If newVelocity <>0: Velocity = newVelocity: newVelocity = 0:EndIf
Until KeyboardPushed(#PB_Key_Escape)
End