2D collision +velocities
Monkey Forums/Monkey Programming/2D collision +velocities
| ||
So I was trying to impement 2d array-like map from Pakz thread and it was okay untill I introduced velocity to the engine. Falling speed would accelerate and if it was fast enough it would ommit collision with any block that is not bigger that the velocity of the player's falling speed and if it was bigger the player would just get stuck in that block. Solution: Add velocity parameter to RectsOverLap function so it counts ahead. Codefile thf.monkey Strict Import mojo Import imp.controls Const mapwidth:Int=20 Const mapheight:Int=20 Global tilewidth:Float Global tileheight:Float Global _player1:_player '╔══════════════════════════════════════════════════════════════════════════════════════════╗ ' Class SYS_Executable Extends App Method OnCreate:Int() SetUpdateRate(60) tilewidth = DeviceWidth()/mapwidth tileheight = DeviceHeight()/mapheight _player1 = New _player Return 0 End Method Method OnUpdate:Int() _player1.Control() Return 0 End Method Method OnRender:Int() Cls 000,000,000 For Local y:Int = 0 Until mapheight For Local x:Int = 0 Until mapwidth SetColor 255,255,255 If Level001[y][x] = 1 Then DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight) SetColor 060,060,255 DrawRect(x*tilewidth,y*tileheight,5,5) End End _player1.Draw() Return 0 End Method End Class '╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ ' Function Main:Int() ; New SYS_Executable() ; Return 0 ; End Function Function RectsOverLap:Bool(x1:Float,y1:Float,w1:Float,h1:Float,x2:Float,y2:Float,w2:Float,h2:Float) If x1 >= (x2 + w2) Or (x1 + w1) < x2 Then Return False If y1 >= (y2 + h2) Or (y1 + h1) < y2 Then Return False Return True End Function Global Level001:Int[][] = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,9,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,1,0,1,0,0,1,1,1,1,1,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ] Content of Imp/controls Strict Import thf Class _player Field _pX:Float Field _pY:Float Field _pXv:Float = 0 Field _pYv:Float = 0 Field _pS:Int = 0 Field _pW:Int = 8 Method New() For Local y:Int = 0 Until mapheight For Local x:Int = 0 Until mapwidth If Level001[y][x] = 9 Then _pX = x*tilewidth ; _pY = y*tileheight End End End Method Method Control:Void() _pYv += 0.5 _pXv *= 0.8 If KeyHit(KEY_SPACE) Then _pYv = -10 If KeyDown(KEY_A) Then _pXv -= 1 If KeyDown(KEY_D) Then _pXv += 1 For Local y:Int = 0 Until mapheight For Local x:Int = 0 Until mapwidth If RectsOverLap(_pX-_pW,_pY-_pW*2+_pYv,_pW*2,_pW*2,x*tilewidth,y*tileheight,tilewidth,tileheight) And Level001[y][x] = 1 If _pYv > 0 Then _pYv = 0 If _pYv < 0 Then _pYv = 0 '_pY = y*tileheight Endif If RectsOverLap(_pX-_pW+_pXv,_pY-_pW*2-1,_pW*2,_pW*2,x*tilewidth,y*tileheight,tilewidth,tileheight) And Level001[y][x] = 1 If _pXv < 0 Then _pXv = 0 Endif If RectsOverLap(_pX-_pW+_pXv,_pY-_pW*2-1,_pW*2,_pW*2,x*tilewidth,y*tileheight,tilewidth,tileheight) And Level001[y][x] = 1 If _pXv > 0 Then _pXv = 0 Endif End End _pX += _pXv _pY += _pYv End Method Method Draw:Void() SetColor 255,255,255 DrawCircle _pX,_pY-_pW,_pW SetAlpha 0.4 SetColor 255,000,000 DrawRect _pX-_pW,_pY-_pW*2+_pYv,_pW*2,_pW*2 SetColor 000,255,000 DrawRect _pX-_pW+_pXv,_pY-_pW*2-1,_pW*2,_pW*2 SetColor 000,000,255 DrawRect _pX-_pW+_pXv,_pY-_pW*2-1,_pW*2,_pW*2 SetAlpha 1 End Method End Class |