About Monkey 2 › Forums › Monkey 2 Programming Help › One way platform collision?
This topic contains 7 replies, has 6 voices, and was last updated by
therevills 1 year, 2 months ago.
-
AuthorPosts
-
January 24, 2018 at 3:58 am #13194
Can anyone offer some code? I just can’t get it right.
Basically a one-way jump from beneath the platform on to it. I’m still hacking away at a method. Code below. I’m using tiles 64×64 pixels that I place using an array.
Monkey12345678910111213141516171819202122232425262728Method Update(delta:Float)If Keyboard.KeyHit(Key.Space) And canJump = 1'direction = 3'frame = 4isJumping = 1canJump = 0EndifIf isJumping = 1y = y - jumpHeightjumpHeight = jumpHeight - gravity * deltaIf jumpHeight <= -1.4isFalling = 1isJumping = 0EndifIf isFalling = 1If GameScreen.LevelArray[ x / 64, y / 64] = 1EndifEndifIf GameScreen.LevelArray[x / 64, y / 64] <> 1 And isFalling = 1y += jumpHeightEndifEndifJanuary 24, 2018 at 11:32 am #13202January 24, 2018 at 8:30 pm #13207I am watching soccer atm so no time to prepare some code. But the way I always look at things like this is by thinking in states.
When the playing is in his jump and going up I set a new variable called jumpingup=true. Another variable like jumpingdown=true if he is falling down. With variables like this I can see when to check for or not check for collision. (if jumpingup=true and myuptilecollision() then …)
I will try to remember to look after the match and make some code.
January 24, 2018 at 11:42 pm #13213I hope this can help a bit. It is not a short piece of code but I did try to add comment.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class mapField map:Int[][]Field mapwidth:IntField mapheight:IntField tilewidth:IntField tileheight:IntField screenwidth:IntField screenheight:IntMethod New(screenwidth:Int,screenheight:Int)Self.screenwidth = screenwidthSelf.screenheight = screenheightsetlevel1()End MethodMethod setlevel1()map = New Int[][] (New Int[]( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 1, 1, 1, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 1, 1, 1, 1, 0, 0, 0, 0, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 1, 1, 1, 1, 0, 0, 0, 0, 2 ),New Int[]( 2, 0, 0, 0, 0, 0, 0, 0, 0, 2 ),New Int[]( 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ) )mapwidth = map[0].Lengthmapheight = map.LengthEnd MethodEnd ClassClass player' player x and yField px:FloatField py:Float' player widht and heigthField pw:IntField ph:Int' How fast left move and rightField horspeed:Int=3' gravity variablesField incy:Float=0Field falling:Bool=FalseField jumping:Bool=FalseMethod New(x:Int,y:Int,w:Int,h:Int)px = xpy = ypw = wph = hEnd Method' Player controlsMethod controls()' Left and right movement (horspeed is the speed left and right)For Local i:Int=0 Until horspeed' If we are jumping or falling then we only want to be' stopped by tile number 2If jumping=True Or falling = TrueIf Keyboard.KeyDown(Key.Right) And playertc(1,0,2) = Falsepx += 1End IfIf Keyboard.KeyDown(Key.Left) And playertc(-1,0,2) = Falsepx -= 1End IfElse 'Here we are not jumping or falling so we want the' collision with movement left and right to stop when' hitting any tile number.If Keyboard.KeyDown(Key.Right) And playertc(1,0,99) = Falsepx += 1End IfIf Keyboard.KeyDown(Key.Left) And playertc(-1,0,99) = Falsepx -= 1End IfEnd ifNext' Player jump when press space. We are not jumping or falling right now.If jumping=False And falling=False And Keyboard.KeyHit(Key.Space)jumping=Trueincy = 5End IfEnd Method'' This method takes care of the falling and jumping for the playerMethod gravity()' First check if we have something beneach us' and if not then fall down. Be sure we are' not falling or jumping right now.If falling = False And jumping=FalseIf playertc(0,1,99) = Falsefalling=Trueincy = 0End IfEnd If' Code for when jumping state = trueIf jumping = Trueincy-=.1For Local i:Int = 0 Until Ceil(incy)' If we are not jumping into a solid non jump through tile' then keep jumpingIf playertc(0,-1,2) = Falsepy -= 1Else 'if we touched a tile we can not jump through then init' fallingincy=0jumping = Falsefalling = TrueExitEnd IfNext' If we run out of jump force(incy) then set to fallingIf incy<=0 Thenjumping=falsefalling=Trueincy=0End IfEnd If' Code for when the falling state is trueIf falling=True Thenincy+=.05For Local i:Int=0 Until Ceil(incy)' If we have nothing beneath us or are not inside anything' then keep fallingIf playertc(0,1,99) = False Or playertc(0,0,99) = Truepy+=1Else 'if we hit solid bottomfalling = FalseExitEnd IfNextEnd IfEnd Method' Player versus tile collision (uses offsets x1 and y1)' Here we check if the player is inside any nearby tile around him.' x1 and y1 is the offset. This way we can check the player position' and a little bit around him.Method playertc:Bool(x1:Int=0,y1:Int=0,tile:Int)Local cx:Int = (px + x1) / mymap.tilewidthLocal cy:Int = (py + y1) / mymap.tileheightFor Local y2:Int=cy-1 Until cy+2For Local x2:Int=cx-1 Until cx+2If x2>=0 And x2<mymap.mapwidth And y2>=0 And y2<mymap.mapheight' If we check for tile = 99 then we check for any tile above value 0If tile=99 ThenIf mymap.map[y2][x2] > 0If rectsoverlap(px+x1,py+y1,pw,ph,x2*mymap.tilewidth,y2*mymap.tileheight,mymap.tilewidth,mymap.tileheight) = TrueReturn TrueEnd IfEnd IfElse 'Here we check the map we are inside of with the inputted tile numberIf mymap.map[y2][x2] = tileIf rectsoverlap(px+x1,py+y1,pw,ph,x2*mymap.tilewidth,y2*mymap.tileheight,mymap.tilewidth,mymap.tileheight) = TrueReturn TrueEnd IfEnd IfEnd IfEnd IfNextNextReturn FalseEnd Method' Draw the player on the screenMethod draw(canvas:Canvas)canvas.Color = Color.Bluecanvas.DrawRect(px,py,pw,ph)End Method' Helper functionFunction rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return FalseIf y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return FalseReturn TrueEnd FunctionEnd ClassGlobal myplayer:playerGlobal mymap:mapClass MyWindow Extends WindowMethod New()' Set The title of the window...Title="Tilemap example Array of Arrays....."mymap = New map(Width,Height)mymap.tilewidth = Width/mymap.map[0].Lengthmymap.tileheight = Height/mymap.map.Lengthmyplayer = New player(4*mymap.tilewidth,4*mymap.tileheight,mymap.tilewidth/2,mymap.tileheight/2)End MethodMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender() ' Activate this method' Clear with black colorcanvas.Clear(Color.Black)canvas.Color = Color.White'Draw the tilemapFor Local y:Int=0 Until mymap.map.LengthFor Local x:Int=0 Until mymap.map[0].LengthIf mymap.map[y][x] > 0canvas.DrawRect(x*mymap.tilewidth,y*mymap.tileheight,mymap.tilewidth,mymap.tileheight)End IfNextNext' Do the player controls and drawmyplayer.controls()myplayer.gravity()myplayer.draw(canvas)' if key escape then quitIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()End FunctionJanuary 25, 2018 at 1:03 am #13215Thank you. That’s very good of you.
January 25, 2018 at 4:09 pm #13224I saw link to example of one side platform on Chipmunk homepage…
January 25, 2018 at 6:54 pm #13230Thanks Pakz for posting it, this will worth the study.
Nerobot, perhaps for full physics capabilities the chipmunk will be better.
Or perhaps a combination of custom arcade physics and real physics, would produce very interesting results. There is something weird in arcade physics that are very useful and efficient. I had tried many tests in Processing Box2D with hacks and tuning to achieve that arcade feeling, and not even close enough to the result needed.
Also I have seen earlier that Diddy2 framework has a platform example in the works, check it out or ask TheRevils for more information.
January 26, 2018 at 12:27 am #13245Basically dont check for any collisions when moving upwards should do it…
-
AuthorPosts
You must be logged in to reply to this topic.