Date: Tue, 21 Feb 1995 22:15:40 +0000 From: Matthew Caldwell <sexkittn@BURN.DEMON.CO.UK> Subject: Re: Spinning electrons >Is it possible to keep track of the distance between two sprites? I will >have 20 or so moveable elctron sprites and need to know when they are a >certain distance from the sprite that represents the atoms nucleus.The exact way you'd do this depends on the specifics of how you're representing the electrons and how you want them to behave, but here's the general principle:
Each electron is represented by a sprite, whose centre's position is given by:
point(the locH of sprite spriteNum, the locV of sprite spriteNum)Therefore, the vertical and horizontal distances between the centers of any two sprites N and M are given by:
dV = abs( (the locV of sprite N) - the locV of sprite M ) dH = abs( (the locH of sprite N) - the locH of sprite M )From this, the direct distance between their centres can be easily derived using Pythagoras:
distanceApart = sqrt( dV * dV + dH * dH )You may find it more efficient to skip the sqrt and just compare the squares, though.
If you want to compare the distances between the outer edges rather than the centres, subtract the radii of the sprites from the computed distance (again, it's probably better to precompute this into the desired length for comparison, rather than do these ops for each comparison). This of course depends on the sprites being approximately circular. If you want to compare their rects, deal with the H & V components separately, and subtract half the width & height before squaring. If you want to deal with an irregularly shaped sprite, well, I'll leave that up to you!
(Note also that you can't necessarily rely on the locH & locV to be at the sprites centre - eg, if you're using bitmaps & have relocated the regPoint then you'll have to take this into account...)