delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/17/10:01:17

From: Rob Farley <Rob AT pulseNOSPAMdesign DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: collision detection
Date: Tue, 14 Oct 1997 17:43:31 +0100
Sender: Rob Farley <Rob AT pulsedesign DOT demon DOT co DOT uk>
Distribution: world
Message-ID: <wa+XKBAzE6Q0IwWa@pulsedesign.demon.co.uk>
References: <C1256524 DOT 002EA05E DOT 00 AT vega DOT ads DOT it>
NNTP-Posting-Host: pulsedesign.demon.co.uk
MIME-Version: 1.0
Lines: 43
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <C1256524 DOT 002EA05E DOT 00 AT vega DOT ads DOT it>, G DOT DegliEsposti AT ads DOT it
writes
>
>
>
>
>
>Hello everybody.
>
>Andrew Deren wrote:
>>
>> Does anyone know how to detect if two rectangles overlap each other at
>> some point. Let's say we have something like this:
>>
>> typedef struct RECT {
>>    int x1, y1, x2, y2;
>> } RECT;
>>
>In many years I have seen it is not possible to solve this problem without
>lots
>of tests, unless you have some special conditions (and usually you have not
>:-)
>
>You have to test for all vertices of A beeing inside B *and* all vertices
>of B
>beeing inside A, otherwise you can have particular cases where it does not
>work.

This maybe going sideways, but, if you use circles instead of rectangles
then all you use is a bit of pythagoras. If you've got a circle with
radius of r1, and a centre at x1,y1, and another of radius r2, with a
centre at x2,y2, then: 

(x1-x2) squared
(y1-y2) squared

Add them up the square root, then, if the answer is smaller than r1+r2
then they are colliding, then after that you may want to do a more
accurate test.

Hope this is of some use.
-- 
Rob Farley

- Raw text -


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