Gravity? Collision Platformer.
BlitzMax Forums/BlitzMax Beginners Area/Gravity? Collision Platformer.
| ||
Well I'm not quite sure how to make gravity happen? I've been thinking of doing if not collision then ypox:+2 or something like that. But my problem is when I look at the collision script i dont quite understand how to use it, could anyone should me a small example of: Function CollideRect:Object[](x,y,w,h,collidemask%,writemask%,id:Object=Null) Let's say i have a DrawRect for player and one for the ground... my second problem after that will be jumping.. more advance stuff in due time. Thansk in advance |
| ||
For gravity, you shouldn't just alter the y by a fixed value. What you need is a yspeed which starts at 0, then when there is no collision you increase yspeed by a small amount. Every frame you change the yposition of the character by yspeed. This what when you jump, you just set yspeed to a negative value and then gravity will make it go to zero (the top of the jump arc) and then beyond as the character falls down. You may wish to consider having a "terminal velocity" which is a maximum yspeed so you can never fall too fast (e.g. through platforms etc). Hopefully someone can help you with the CollideRect code as it's a bit tricky. |
| ||
Yes what you really need is X,Y as the position of your object, then XAdd,YAdd as the speed at which it is moving, then you add 2 to the YAdd. Then when you do X:+XAdd and Y:+Yadd it will gradually increase the Y speed as the object falls. Handling collision and bouncing is a little more tricky. When the object hits the surface, depending on the dampening qualitities of the surface the YAdd should be inverted to become negative so the object bounces up the screen. Of course you keep adding 2 to the gravity so it slows down and gradually falls again. But if you want the object to come to a rest eventually you have to lose some of the `energy` when it bounces e.g. YAdd:-10 or something. I also recommend using floats otherwise you'll see quite jagged movement. |
| ||
Here you go. This is Jump code at its simplest' Jump Code for Kaisuo ' By AMon SuperStrict Graphics 800 , 600 Global Dude:TImage = LoadImage("dude.png") Global Jump:Int = 0 ' Tracks whether we can jump Global Gravity:Float = 0.3 'Gravity applied to jumpheight Global JumpHeight:Float = 7 ' How High we can jump Global dudeX:Float = 380 Global dudeY:Float = 520 While Not KeyHit(KEY_ESCAPE) Cls DrawImage Dude, DudeX , dudey , 0 DrawLine 0 , 570 , 800 , 570 If KeyHit(KEY_SPACE) Jump = 1 End If DoJump() If dudeY >= 570 - 50 dudeY = 570-50 Jump = 0 JumpHeight = 7 End If Flip Wend Function DoJump() If Jump = 1 dudeY:-JumpHeight JumpHeight:-Gravity If JumpHeight <=-5 Then jumpHeight = -5 End If End Function Replace the image with anything. :) |
| ||
A simple example of CollideRectStrict Local x:Int Local y:Int Graphics 640,480 ResetCollisions() 'SetColor 0,0,0 <--------------uncomment this out and see what happens DrawRect 300,300,100,100 CollideRect 300,300,100,100,0,1 '<---comment this out to see what happens Repeat x = MouseX() y = MouseY() 'check collision b/w a 10x10 rect against rect above If CollideRect(x ,y , 10 , 10 , 1 , 0) SetColor 255,0,0 DrawRect x , y , 5 , 5 '--note that collision is not against this SetColor 255,255,255 EndIf Flip Until KeyHit(KEY_ESCAPE) Or AppTerminate() Also check out this tutorial http://www.2dgamecreators.com/tutorials/gameprogramming/collision/T1%20Collision2.html |
| ||
Thanks everyone, sorry for the late replies(real life..) I've been trying to figure out the collision between my two rectangle. I don't quite understand it yet with what you guys provided me. As for increasing gravity, well gonna add it after I can actually touch the ground and move right and left lol(same goes for jumping). So let's start with my collision... I'm not quite sure why my piece of code won't let me modify gravity..? SuperStrict Graphics 800,600,0 '******************************************************************************************** Global GameRoom:Int =0 Global GameExit:Int =False ResetCollisions() Local P1:TPlayer=New TPlayer; P1.Create(10.0 ,10.0 ) While Not GameExit=True If(KeyHit(KEY_ESCAPE))Or(AppTerminate())Then GameExit=True P1.Updates() MakeSolid(0,560,800,25) Flip 1 Cls Wend End Type TPlayer Field health:Int, mana:Int Field xpos:Float, ypos:Float Field maskw:Int=70, maskh:Int=120 Field gravity:Float=1.0 Field jump:Int=False Method Create( xstart:Float, ystart:Float ) xpos=xstart ypos=ystart End Method Method Updates() If KeyDown(KEY_UP) Then ypos :- 5.0 If KeyDown(KEY_DOWN) Then ypos :+ 5.0 If KeyDown(KEY_LEFT) Then xpos :- 5.0 If KeyDown(KEY_RIGHT) Then xpos :+ 5.0 If KeyHit(KEY_SPACE) Then jump = True ypos :+ gravity Self.Drawing() End Method Method Drawing() DrawRect xpos,ypos,maskw,maskh CollideRect xpos,ypos,maskw,maskh,0,1 End Method End Type Function MakeSolid( x:Int, y:Int, w:Int, h:Int ) DrawRect(x,y,w,h) If(CollideRect(x,y,w,h,1,0))Then P1.gravity=0 EndIf End Function Not Quite sure how to design my level should I make a function which draws all the masked SOLID background that would collide with my player's rectangle mask? *shake his head and goes take an advil* xD |
| ||
So you've bought Bmax now then? |
| ||
hmm, what if i have roar ;D |