Phoresis 122 Report post Posted April 6, 2007 I've got a 2d rectangle that you can move about the screen. There is also a circular area in the middle of the screen. When you move the rectangle it orients itself so that it is always at a tangeant with the circle, even though it may be some distance away from the circle. Now currently, my collision detection works as follows: Find the length of a vector from the center of the rectangle (minus half the width) to the center of the circle, and check if this is greater than the radius of the circle. If it is then you can move the rectangle. My Problem is this: When the rectangle comes into contact with the circumference of the cirle I want moving the rectangle to kind of wrap it around the edge of the circle, rather than just not being able to move the rectangle at all. Is there a simple way of doing this? Does my post make sense? 0 Share this post Link to post Share on other sites
UltimateWalrus 229 Report post Posted April 6, 2007 I think I know what you mean. What you want to do is, if you determine that the rectangle is colliding with the circle, use trigonometry to place it so that it is at the edge. Use this pseudocode:a = atan2(circley-squarey, circlex-squarex)circlex = radiuses_added_together*cos(a)circley = radiuses_added_together*sin(a)I think that should work. 0 Share this post Link to post Share on other sites
Phoresis 122 Report post Posted April 6, 2007 Quote:Original post by UltimateWalrusI think I know what you mean. What you want to do is, if you determine that the rectangle is colliding with the circle, use trigonometry to place it so that it is at the edge. Use this pseudocode:a = atan2(circley-squarey, circlex-squarex)circlex = radiuses_added_together*cos(a)circley = radiuses_added_together*sin(a)I think that should work.ah yes of course, that's quite simple really. my brain didn't work liek that for some reason.circlex = radiuses_added_together*cos(a)circley = radiuses_added_together*sin(a)should be this though right?:centerofsquarex = distancefromcenterofsquare*cos(a)centerofsquarey = distancefromcenterofsquare*sin(a) 0 Share this post Link to post Share on other sites
l0calh05t 1797 Report post Posted April 6, 2007 either way,a = atan2(circley-squarey, circlex-squarex)[...] cos(a)[...] sin(a)is suboptimal because you just need to normalize circley-squarey, circlex-squarexsin_a = circley-squarey;cos_a = circlex-squarex;n = 1.0 / sqrt(sin_a*sin_a+cos_a*cos_a)sin_a *= n;cos_a *= n;should be faster and more precise (fewer operations)Edit: and it just happens not to be any questionable optimization, it's just basic geometry/trigonometry 0 Share this post Link to post Share on other sites