Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


collision detection

Posted on 2003-02-27
Medium Priority
Last Modified: 2010-04-07
Hi guys there...I'm actually working on a 2D fighting game ..sort of street fighter..I'm just wondering how can I write the codes for collision detections between the characters? thanks
Question by:singersinger

Expert Comment

ID: 8053134
You may try to create areas for every part of the char's body. Areas should be as simple as rect's or circles - then you can test if this areas are intersecting, when you notice head intersecting with fist - you can aply correct animation

Expert Comment

ID: 8053456
Hello singersinger,

     To calculate whether or not a collision has happened, you can use Linear Geometry. Let's say you wanted to know if polygon A had collided with polygon B. If there was a collision, then one of the vertices of polygon A would end up sticking into polygon B (picture a triangle hitting a side of a square). So, you just need to find out if a vertex of polygon A is inside polygon B. To do this, you need to use the coordinates of each vertex of polygon A in the linear equations of polygon B ([y <= mx + b] or [y =< mx + b] depending on the line's location). If one of the vertices returns "true" on all of the linear equations, then you have a collision. Good luck.

LVL 46

Accepted Solution

Kent Olsen earned 200 total points
ID: 8096113
I'm jumping in late, but maybe I can help.

For the first hack, draw both characters in your intended graphics but for collision detection pretend that both are circles.  As they move, check for a collision simply by checking for the circles intersecting.

If in your game, both characters are on the same level (both standing on the street, etc) then you can detect the circle collision by:

if (PlayerLeftX+FieldWith >= PlayerRightX-FieldWidth)

In general terms, the equation is really:

if (abs(PlayerLeftX - PlayerRightX) <= 2*FieldWidth)

If their positions are not horizontal, a bit of trig is required, but not much.  Then your general purpose equation looks like:

if (Length(Point(PlayerLeftX,PlayerLeftY), Point(PlayerRightX,PlayerRightY) <= 2*FieldWidth)

You now have the basics for collision detection.  But it's kind of crude, so instead of thinking of each player as a circle, think of each as collection of shapes.

Each player is a circle or oval big enough for the torso.  At the center of the circle move straight up and place another circle for the head.  Now when you check for collisions check the neither of PlayerLeft's circles have touched PlayerRight's circles.  Keep refining this by adding more shapes at key locations and more tests.

Have you ever noticed that in a lot of "contact" games, pieces can touch in odd places or at odd angles and contact isn't detected?  Or that if players are too close, when they hit or kick at each other they seem to miss?  This is because the collision detection only checks for certain collisions.

Put your characters side by side and have one "punch" at the other one.  When the arm is extend, check to see if the fist is in one of the other player's shapes.  If the arm is long enough, it is behind the player and there is no collision.  Many games work this way.  They detect the collision at the full extent of the arm/leg and not necessarily at enough points along the movement path.

Anyway, there's a lot of the basics.

Good Luck,


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8164632
Good article for this theme
(with some source):


Expert Comment

ID: 8535667
Hi singersinger...

I have developed a Game Engine for 2D Games...

In my engine, you gut all you need, from collision detecting to manipulate images, read and write the images simple and more.

If you are interested, I could mail it to you.

Yours Sincerely Snurre

Expert Comment

ID: 9464258
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question