Colition Detector

Started by
6 comments, last by lede 8 years, 11 months ago

I am working and c++, and have worked on a Colition Detector. It isnt working out properly. It is a 2d sidescroller game and its a sandbox. I want to know how to make one, as my system is not working curently. mainplayer.x, mainplayer.y, mainplayer.speed, mainplayer.width, and mainplayer.height are the major player variables im using. I am trying to get the block a player is walking or falling twords, check if you can walk through it, and stop him/her if it isnt. Each block is 32x32 pixels, and the player moves pixel by pixel. Any help?

Advertisement

We can't help you very well without more details and seeing the actual code ourselves. Anyways...

It's been a long time since I have done 2D. But I am sure it's collision detection works the same in 3D.

What you are doing is you are using a bounding box to simplify your calculations. When you want to test to see if something collides, you are going to check against an intersection in the bounding box.

A fully-functional collision detection algorithm for a game whose internal structure we know nothing about (and you whose skill level we know nothing about) isn't anything anyone is going to be able to give you a solution to in an internet forum.

I would suggest googling for "2d collision detection platformer", or else asking a more specific and targeted question about an implementation detail (e.g. "why isn't this bounding box intersection code I wrote functioning properly, here's the code, and here's what I see when I step through the debugger", or "here is how I'm storing my blocks, here's how I move my player, how I can detect a collision with the blocks?").

Is it strictly necessary for you to code up your own collision system from scratch? Collision physics is deceptively difficult, and you can waste a lot of time trying to implement it correctly.

I don't know about C++ specifically, but there are plenty of out-of-the-box working solutions out there. Is it necessary to re-invent the wheel for your project?

OK, does anyone have one of these systems to show.me?

http://box2d.org/about/

Is it strictly necessary for you to code up your own collision system from scratch? Collision physics is deceptively difficult, and you can waste a lot of time trying to implement it correctly.

I don't know about C++ specifically, but there are plenty of out-of-the-box working solutions out there. Is it necessary to re-invent the wheel for your project?

It's not hard if you need something simple, and it's good for learning. But other wise... yeah a library.

If you really want to write your own collision code. Check out Collision Detection for Games. It's a book you can find on amazon. Possibly the best markup I can find that explains to you how everything works, the math, and provides pseudo code to get you rolling.

If your just dealing with a 2D game then Box2D is the de facto standard of physics engines. I've used this engine in several different projects, oddly enough none of them in c/c++, and found it a great resource. It can be a little daunting to get up and running but once you get the physics world running it is a rock solid engine. Make sure to follow the introduction tutorials.

As for your original question have you though about using a sphere collider? Using a sphere makes the calculations a little easier to perform and instead of having to check four different points you could just check the distance from the center of your sphere to any object and see if it is less then the radius of your sphere. In the cases where your object's distance is less then the radius of the collider you would then want to handle the collision. Once you have detected the collision you will want to apply the game rules to the object, which is a different topic.

This topic is closed to new replies.

Advertisement