Copy Link
Add to Bookmark
Report

Collisions

eZine's profile picture
Published in 
Techtutor
 · 3 months ago

 ======================================================================== 
Just4Fun Productions presents:

Part 4 of: the Techtutor
(Topics never covered)


Collisions
========================================================================

Introduction

This time we'll take about collisions. There are multiple types of collisions, but the one we'll talk about here is the collision between 2 sprites. First of all some explanation about a few words I'm going to use:

  • raw-edge = the edge of the complete shape (rectangle)
  • real-edge = the actual "edge" of the image.

so if the image would be a circle the raw-edge would still be a cube that surrounds the image (the sprite), and the real-edge would be the circle edges it self...

---------------------------------------------------------------- 
For contacting Just4Fun or me (PeeBee) use the following ways:

Internet : http://people.zeelandnet.nl/rpb
Email : just4fun@zeelandnet.nl

SnailMail: P.Bestebroer
Anthony Edenlaan 28
4463 JC Goes (Zld.)
Holland

ICQ : 2309110 (probably fastest way to contact me)
----------------------------------------------------------------

How to do it

Collision detection in games is one of the most important things to get right, because you don't want the player to get hit while there's no alien actually touching him... on the other side you don't want the player to be able to go "thru" another object. There are a few ways of collision detection, but not all of them are very effective in terms of speed, and pixel-perfection. A good collision detection would check on all the real- edge pixels of an image, but that would be very S-L-O-W! Just think about it, if you would use 32x32 sprite-images, and you had about 50 sprites on screen, you would have to check 51200 pixels, that's allmost a complete screen! of course this could be done faster by adding some extra image information about where the real-edges are, but it would still be very slow to check it completely.

Another way of checking is a little bit more math-coordinated, you could make some triangles, or maybe even rectangled areas that "surround" the sprites real-edge, this is medium-fast, and medium-perfect so it might be worth checking into that...BUT we need speed! so,

The fast way

A faster way is not as pixel-perfect as should, but close enough to fool the player. The fast way would be to check on collision inside of the raw-edge only. This is fast enough because you'll only have to check on 4 coordinates per sprite. Of course this isn't that perfect because you'r image's may not be completely filled to the raw-edge. For a more effective working of this idea, you'll have to draw you'r real-edges as close to the raw-edges as possible, leaving a minimum of "empty-ness" at the edge of the images. Those few bytes that are empty, won't get in the way of the gameplay because the player won't see it, and doesn't know about it! And you could also make the collision-rectangle a bit smaller then the true sprite-rectangle (leaving some pixels for player-errors).

Examples

No example file is attached, but I'll give a simple collision detection function here:

 { 
Rough check if 2 shapes are colliding. This works faster as a
pixel by pixel check for a collision, and as long as the shapes don't
have to much "space" on the edges the collision will be allmost
perfect.

Expects: X+Y position and the WIDTH + HEIGHT of the first sprite,
X+Y position and the WIDTH + HEIGHT of the second sprite.

Returns: True if collision has been detected.
}
FUNCTION collision(x1,y1,w1,h1, x2,y2,w2,h2 :integer):boolean;
VAR xd,yd : boolean;
BEGIN
ASM
mov xd,false
mov yd,false
END;
if ((x1 <=x2) and (x1+w1>=x2)) then xd:=true else
if ((x1 <=x2+w2) and (x1+w1>=x2+w2)) then xd:=true else
if ((x1 >=x2) and (x1+w1<=x2+w2)) then xd:=true;
if ((y1 <=y2) and (y1+h1>=y2)) then yd:=true else
if ((y1 <=y2+h2) and (y1+h1>=y2+h2)) then yd:=true else
if ((y1 >=y2) and (y1+h1<=y2+h2)) then yd:=true;
collision:=xd and yd;
END;

Final word

The example shown above was taken from my SuperFX Engine, it work's fine and it looks like a pixel-perfect collision detection.

Well that's it for now, The next topic is brand new, and is also new for on the web-site, so keep reading for:

Next Tech topic: Demo recording/playing


PeeBee - September 10th '97

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT