Basic 2D physics
BlitzPlus Forums/BlitzPlus Programming/Basic 2D physics
| ||
Well, Im wondering how I could do basic 2D physics. Just basically collision. For example: I shoot a bullet, the bullet hits a ball. The ball goes flying decently close to real physics in a 2D enviroment. I tried google for some help on this, but I couldnt find anything of use. Thanks |
| ||
Firstly make everything the same object (or Types in Blitz) then you can write some generic physics functions to handle the bullet and the ball. Obviously the types need x and y coords but they also need weight (for collisions) and x speed and y speed. Also make sure the x and y is at the centre of your object. Then when they collide you can decide which way to send both objects by setting their x and y speeds. Each frame a generic MoveObject function can move each object by their x and y speeds automatucally. Also you need some kind of friction so that the x and y speeds slow down each frame until the objects come to rest. If you game is overhead you won't need gravity but if it is side on you will need gravity. In this case every frame you must apply gravity to each object and see if it can fall (i.e. it is mid air now) and increase the y speed by gravity. Gravity will be your y speed "friction". Your x speed friction can be calculated by taking the weight of the object into account and checking if it is on a surface and working out the coefficient of friction (how slidey) the surface is. Then you can decrease the x speed by the surface friction. I wouldn't bother decreasing x speed if the object is in the air as air resistance is generally negligible. There is loads of stuff about physics on the web but you kinda want it at high school level (A level in England) so that you don't have to wave through too much crap. Hope this helps a bit. |
| ||
Hmm well that answered a bit of my questions. I shouldve mentioned its top down. |
| ||
does this help? http://www.blitzbasic.com/codearcs/codearcs.php?code=415 Or how about this? http://www.blitzbasic.com/codearcs/codearcs.php?code=998 |
| ||
Thank you, those should do, for now at least. |
| ||
this is likely the best tutorial iv found for simple 2d physics http://www.rit.edu/~ndr0470/game_physics/ |
| ||
Excellent link there, thanks for that. |
| ||
yeah it was kinda fun! |