Jump to content
  • Advertisement
Sign in to follow this  
Mr. Moon

Collision Detection in a GTA-like Game

This topic is 1428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone,

 

Currently I work on a GTA 1 or 2 - like zombie shooter in Java and I don't know exactly how to handle collisions in this game. My first approach was to preload a zombie image for each direction the zombie can move and a standard pixel perfect collision detection. But since I added some animation for the zombies the image sizes bloated up my memory. Now I try to use a rotate on a zombie at runtime but with that I'm not able to manage the collisions. I've googled a lot but I didn't found any suitable solution. Is there anyone who knows a pixel perfect or similar solution for collision detection on rotated sprites. Simple bounding circles are not quite enough for me.

 

Cheers

 

Mr.Moon

Share this post


Link to post
Share on other sites
Advertisement

What kind of images are you talking about?

 

With a top view game, these should basically be 1D images, not 2d, with regards to each direction.  All you need is the width of the zombie from each angle.  You could also add in color information representing the chances of hitting the zombie along the width if you wanted (e.g. extended arms being less than torso).  

But there shouldn't be any height-from-the-ground information to the bullet, so from the player character's perspective, they're all 1D.

Share this post


Link to post
Share on other sites

What kind of images are you talking about?

 

With a top view game, these should basically be 1D images, not 2d, with regards to each direction.  All you need is the width of the zombie from each angle.  You could also add in color information representing the chances of hitting the zombie along the width if you wanted (e.g. extended arms being less than torso).  

But there shouldn't be any height-from-the-ground information to the bullet, so from the player character's perspective, they're all 1D.

 

Graphic images like .png files. 1D? I've got a X and a Y axis. That's 2D or not? I don't get what you mean. Can you please explain it in more detail?

 

cheers

Share this post


Link to post
Share on other sites

 

Graphic images like .png files. 1D? I've got a X and a Y axis. That's 2D or not? I don't get what you mean. Can you please explain it in more detail?

 

cheers

 

 

You can convert a line of pixels into a lookup table, or you can stack several lines of pixels, representing different angles.

 

aaaaaaaaaaa

bbbbbbbbbbb

cccccccccccc

 

Like this.  That's a 2d image which contains three 1d images.

Share this post


Link to post
Share on other sites

Hi mr moon, my suggestion:

 

First, use bouding circles just to know if the other object is close enough.

 

Then, if it is, draw your rotated zombie's current frame to a temporal texture (your graphics library should have a way of doing this, that is, drawing to memory instead of screen... or worst case scenario you have to rotate the sprite yourself, google will show you rotating algorithms).

 

Use that new generated texture to do your standard pixel pixel perfect collision checking.

 

Hope that helps. Bye.

Share this post


Link to post
Share on other sites

Here is my suggestion:

Every zombie is some kind of convex polygon, circle or something like that. Then you can relatively easy implement collision detection.

I did something similar some time ago I guess - https://github.com/martinradev/2dShooter/tree/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/geometry

https://github.com/martinradev/2dShooter/blob/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/game/interaction/CollisionDetection.java

 

There are probably some bad java approaches but the idea of collision detection is outlined. One thing to notice is that you will have to do further optimizations to get the collision detection faster. One idea is creating a Binary Space Tree of the map.

https://github.com/martinradev/2dShooter/blob/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/structures/BinarySpaceTree.java

Though this is not a true binary space tree and it can be improved.

Share this post


Link to post
Share on other sites

Here is my suggestion:

Every zombie is some kind of convex polygon, circle or something like that. Then you can relatively easy implement collision detection.

I did something similar some time ago I guess - https://github.com/martinradev/2dShooter/tree/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/geometry

https://github.com/martinradev/2dShooter/blob/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/game/interaction/CollisionDetection.java

 

There are probably some bad java approaches but the idea of collision detection is outlined. One thing to notice is that you will have to do further optimizations to get the collision detection faster. One idea is creating a Binary Space Tree of the map.

https://github.com/martinradev/2dShooter/blob/master/VirtualCommando/src/main/java/me/martin/radev/game/virtualcommando/structures/BinarySpaceTree.java

Though this is not a true binary space tree and it can be improved.

 

Nice approach but maybe wrong. You take two polygons and check if any line between two points of one polygon intersects with a line of the other one. But consider the case if you have a small rocket which is actually a polygon too and  the polygon of a zombie. The rocket is by far smaller than the zombie and if the rocket moves very fast, maybe 5 pixels per frame, you got the case that the rocket is suddenly in the mid of the zombie but no collision is detected.

Edited by Mr. Moon

Share this post


Link to post
Share on other sites

 

 

Graphic images like .png files. 1D? I've got a X and a Y axis. That's 2D or not? I don't get what you mean. Can you please explain it in more detail?

 

cheers

 

 

You can convert a line of pixels into a lookup table, or you can stack several lines of pixels, representing different angles.

 

aaaaaaaaaaa

bbbbbbbbbbb

cccccccccccc

 

Like this.  That's a 2d image which contains three 1d images.

 

I'm still not following. How does a "1D image" (whatever that is) help you optimize the process of testing two 2-dimensional shapes to see if they collide?

Share this post


Link to post
Share on other sites

I'm still not following. How does a "1D image" (whatever that is) help you optimize the process of testing two 2-dimensional shapes to see if they collide?

 

Because you're not colliding 2d shapes.

 

You're colliding a ray with a flattened "cross section" of a 2d shape (a 1d shape).

 

The bullet doesn't care about the 2d image.  It only cares about the 1d image it sees when the 2d image is turned on its side.

 

[attachment=24210:zombiebullet1.gif]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!