delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/29/03:12:10

From: rellwood <rellwood AT aludra DOT usc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Collision Detection
Date: 28 Apr 1997 13:10:46 -0700
Organization: University of Southern California, Los Angeles, CA
Lines: 35
Sender: rellwood AT aludra DOT usc DOT edu
Message-ID: <Pine.SV4.3.94.970428125700.28102C-100000@aludra.usc.edu>
References: <MPG DOT dcddb78f470c89c98968a AT news DOT demon DOT co DOT uk>
NNTP-Posting-Host: aludra.usc.edu
Mime-Version: 1.0
In-Reply-To: <MPG.dcddb78f470c89c98968a@news.demon.co.uk>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Sun, 27 Apr 1997, Alex wrote:
> I a writing a new game and I would like to know to to do collision 
> detection.
> The game will be side scrolling, I already have a partial game engine but 
> I need collision detection so the game knows when I have hit an alien or 
> I when i'm not going over a floor.

There are several ways to do collision detection, and the thing is to
choose the method that is most appropiate for your particular needs.

The best method is the slowest, and that is to AND each nonzero pixel in
sprite A with each nonzero pixel in sprite B.  If any of the ANDs return
true, you have a collision.  (this is under the assumption that zero
represents the transparent parts of the image.)  With this, you get very
accurate collision detection.

A much quicker method is to detect a collision of just the bounding box
around each sprite.  If the boxes overlap the detector returns true.  The
problem with this, of course, is that usually a given sprite doesn't fill
the bounding box completely and the detector will return a true if the
boxes overlap but the sprites don't.  Usually this is an okay trade-off
for the speed advantages.

A third way is to use bounding circles instead.  I have nevery actually
tried this, but I suppose it could be implemented easily enough using good
old Pythagorian Theorum, and just measure the DISTANCES between the two
sprites.  If the result is smaller then a certain threshold, you have a
collision.  This is a bit more accurate then bounding boxes, but it is
somewhat slower because you have to compute square roots. 

Hope this helps.
-Richard Ellwood

-- 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019