Rectangular Collision
Monkey Forums/Monkey Programming/Rectangular Collision
| ||
Hi Guys, This is my first post on the website and my first real attempt at a game in monkey X! The basic jist of it is that you control the white square and you have to eat the green squares (Of which there are three). When a green square has been 'eaten' (touched) I want it to move to a random part of the screen. I know how to move it but I am not entirely sure how I would detect this collision. Here is my code. Import mojo Global Game:Game_App Class Game_App Extends App Field playerX:Int Field playerY:Int Field foodX:Float [3] Field foodY:Float [3] Field index:Int Method OnCreate() SetUpdateRate 60 playerX = 320 playerY = 240 For index = 0 To 2 foodX[index] = Rnd(0,640) foodY[index] = Rnd(0,480) Next End Method OnUpdate() If KeyDown(KEY_UP) Then playerY = playerY - 4 If KeyDown(KEY_DOWN) Then playerY = playerY + 4 If KeyDown(KEY_LEFT) Then playerX = playerX - 4 If KeyDown(KEY_RIGHT) Then playerX = playerX + 4 If playerX < 16 Then playerX = 16 If playerX > 624 Then playerX = 624 If playerY < 16 Then playerY = 16 If playerY > 464 Then playerY = 464 End Method OnRender() Cls 0, 0, 0 DrawRect playerX, playerY, 25, 25 SetColor 0,255,0 For index = 0 To 2 DrawRect foodX[index],foodY[index], 16, 16 Next End End Function Main() Game = New Game_App End Any help/hints would be great! Thanks |
| ||
I think a good place to start is by learning some basic linear algebra: http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/ followed up by http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ which is then followed up by http://blog.wolfire.com/2010/07/Linear-algebra-for-game-developers-part-3/ If you want some reference code for the above articles: https://bitbucket.org/Goodlookinguy/xaddon/src/default/basic/spatial/vector2.monkey By the end of those, if you can't figure out how to do some basic collision, this code may help you https://bitbucket.org/Goodlookinguy/xaddon/src/default/basic/spatial/shapes/funcs.monkey I've been doing game programming for only about 7-years now and I still have issues with collision detection and response. It's just something you have to practice at and when all else fails, cheat it by using someone else's library like Box2D, Fling or something lightweight (mojo2 only) like Hardon Collider. |
| ||
The *Monkey Example Blog* got you covered. I would advice you to look at some of the examples to get going. Even if its for mojo 1 only these *Youtube Videos* are great too. As a general note the basics of OOP should be covered too. E.g. an introduction can be found *here* |
| ||
Thank you for the advice! I'll be sure to check all those links out. Thanks again for the help, I wasn't expecting such a rapid and helpful response, definitely makes a change from most forums! |
| ||
Here is another example I made earlier you might try to see it too in case you learn something from it. http://www.monkey-x.com/Community/posts.php?topic=10642 |
| ||
Easy solution: Make a function or method that can tell if a particular point is in a square. Then test it for each corner. If the squares differ greatly in size, this may not work. But you can test points in a square grid. Pro tip: do something cheap and simple, and check whether the game is fun. If it isn't, none of this matters and you need to make a different game. |
| ||
Thanks again for all of the advice, I've got the collision system working and in the end I used the example on the Monkey Example Blog. |