[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [pygame] How to rotate a rect
I think what you described is usually called the Manhattan distance.
(Basically because your measuring the "number of blocks" between objects.)
You're right that you don't necessarily need a Sqrt at the end as you would
for a real Euclidean distance, seeing we just want a relative measure, but I
think you want:
distsquared = (object2.x - object1.x) * (object2.y - object1.y) # times
instead of plus
for your "squared distance".
Manhattan distance may work okay too, but the bounding box for collisions
will be kind of like a round diamond shape. (It will be smaller at the
diagnals.) Adding the "*" will make the collision area circular as you
In all cases above your just picking a point in your box and not really
looking at the orientation of the rotated rect.
One way to do that would be to write similar fn's to Rect's collision
detection methods, but for generalized polygons.
From: email@example.com [mailto:firstname.lastname@example.org]On
Behalf Of Paul Sidorsky
Sent: Friday, June 29, 2001 8:43 PM
Subject: Re: [pygame] How to rotate a rect
Leonardo Santagada wrote:
> Making the box is not the problem but how to make fast collision
This may not be suitable for your situation but I'll describe it anyhow
since I haven't seen it mentioned yet:
Give each object a radius or size and then use the distance formula. If
the distance between two objects is less than the combined radiuses (or
half the combined sizes) of the objects, they collide. To avoid
floating point, use the squared distance. Here's pseudo code:
limitsize = (object1.size1 + object2.size2) / 2
limitsize *= limitsize
distsquared = (object2.x - object1.x) + (object2.y - object1.y)
if distsquared <= limitsize:
This works great for spherical objects and can work for others if you
don't need 100% precision. The inaccuracy increases as the sizes get
larger. I'm not sure how practical this method is in "real" games since
I've only tried it on a small scale, but it might be worth a shot.
Paul Sidorsky Calgary, Canada
pygame mailing list
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
pygame mailing list