Newsgroups: comp.ai.games
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!news.kei.com!news.mathworks.com!fu-berlin.de!cs.tu-berlin.de!informatik.uni-bremen.de!nordwest.pop.de!uniol!uni-erlangen.de!lrz-muenchen.de!news.uni-augsburg.de!news
From: Pitbull <schaefer@malaga.math.uni-augsburg.de>
Subject: Re: Collision detection w/ odd bounding boxes
Message-ID: <DLFrDG.L05@Yuri.CC.Uni-Augsburg.DE>
Sender: news@Yuri.CC.Uni-Augsburg.DE (The News Administrator)
Nntp-Posting-Host: jaen.math.uni-augsburg.de
Organization: Computer Center, University of Augsburg, Germany
References:  <4dh2vg$1c7@news.one.net>
Date: Fri, 19 Jan 1996 16:05:38 GMT
Lines: 54

you@somehost.somedomain (Mike) wrote:
>
> I was interested in a collision detection algorithm to test if (x,y) was 
> inclosed by 4 other (x,y) points.
> 
> Example:          x1,y1
>                     /\
>                    /  \                The * would return true
>                   /    \                  and the ? would return
>                  /      \                     false....
>            x4,y4/        \ x2,y2
>                 \        /
>                  \  *   / 
>                   \    /
>                ?   \  /
>                     \/
>                   x3,y3
> 
> 
> Any help?  (BTW: this would need to be called 1500 times per second)
> 

After seeing the other replies, I think even this small post will help
you:

To simplify and speed up things,
replace x3,y3 by 0,0
replace x,y by x-x3,y-y3
and so on for all points.
I assume that this up there is a diamond. 
You need twice the checks otherwise, but it isn't really different.

Diamond means: (xN,yN)-(xN-1,yN-1) =(xN+2,yN+2)-(xN+1,yN+1) .

(x,y) is inside the diamond if

0 <= <(x,y),(x2,y2)>  <= <(x2,y2),(x2,y2)>
0 <= <(x,y),(x4,y4)>  <= <(x4,y4),(x4,y4)>

where
<(x,y),(x2,y2)> = x*x2+y*y2
<(x,y),(x4,y4)> = x*x4+y*y4
<(x2,y2),(x2,y2)> = x2*x2+y2*y2
<(x4,y4),(x4,y4)> = x2*x2+y2*y2

I think you can speed things up a bit by
ordering the 4 checks and 4 computations that are made nicely.

If you have constant values for x2 and y2,and the others,
you can also speed up things by writing an optimized collision 
detection for that object, precalculating the <(x2,y2),(x2,y2)>, etc.

Peter

