Handling collisions?

Talk about your skill passion! Ask questions, get feedback, post something cool!

Moderator: samw3

User avatar
JoeJoeZ26
Posts: 346
Joined: Fri Jul 25, 2008 8:30 am
Location: Somewhere playing/ thinking up a game

Handling collisions?

Postby JoeJoeZ26 » Sun Jan 19, 2014 10:17 am

Would handling collisions for a 2D, top-down styled game require the aid of a physics engine in order for efficient collisions checking?
User avatar
bugala
Posts: 1279
Joined: Wed Oct 21, 2009 11:43 pm
Location: Finland
Contact:

Re: Handling collisions?

Postby bugala » Sun Jan 19, 2014 12:19 pm

I dont know much about 3D actually, but supposing you mean by 2D game, really a 2D game, and not a 2D game, that is actually using 3D graphics, then no.

Basically idea is that you have like two rectangle paper pieces, other one is player, and other one is enemy (naturally there can be many), then each time you move these paper pieces places on screen (this is how you get the movement in game), you will do so called collision check, which means that it is checking if these two paper pieces are touching each other. If they are, then happens collision, if not, then no collision.

and in practice this is usually (depending upon your choice of programming language) done about:

x = collision(playerpaperpiece, enemypaperpiece)

if x= TRUE, then goto collisionpartofprogram



Most likely, no need to be afraid of this thing.


There are some possibilities that you might need some more advanced methos for collision check, this would be for example if these paper pieces are moving so fast, that it is possible that player first shows on left side of the enemy paper piece, and on next sceenupdate it already shows on right side of enemy paper piece.

In this case simple check of collision, would results FALSE, athough in reality it should have happened, since that piece of paper cant get on other side of enemy, without going through it.

But these are quite rare. Basically if you have very small sized (like couple of pixels only) characters, of if there is possibility to move real fast (as example, mouse pointer)

To get the idea of the speed required, you can right now move your mouse pointer real fast and you can notice that it is drawn only to some of the places, when you slow down that movement a bit, you get pretty good idea about how fast these characters would really need to move before simple collision check wouldnt do anymore.

Naturally, if there are lots of stuff happening on screen and hence your game slows down to only 4 frames per second, then that is different matter again, and then it is much easier getting past collision check.
User avatar
JoeJoeZ26
Posts: 346
Joined: Fri Jul 25, 2008 8:30 am
Location: Somewhere playing/ thinking up a game

Re: Handling collisions?

Postby JoeJoeZ26 » Mon Jan 20, 2014 8:18 pm

Awesome! Thanks so much bugala.

So essentially, collisions can be easily checked every time the screen updates with a simple if - else if statement. That's a lot less stressful than I thought it would be :)
TheLittleSettler
Posts: 120
Joined: Fri Nov 19, 2010 10:08 pm

Re: Handling collisions?

Postby TheLittleSettler » Tue Jan 21, 2014 3:02 am

Just to add to that, the simplest collision check I know of uses circles rather than boxes. That way you just check distance (a^2 + b^2 = c^2) is smaller than the sum of the radius of the two objects involved.

One thing to be aware of, when objects are moving at high speeds you have to take that into account. If they move too fast, they can go through other objects in their path. I know both game maker and unity's default collision system don't handle those situations so it pays to know even if you aren't using your own collision code.

Who is online

Users browsing this forum: No registered users and 4 guests

cron