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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 45

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,

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!


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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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