简体   繁体   中英

Placing Object Around a Rotating point

I have two objects, object1 and object2 . object1 rotates around its center and object2 will be positioned around it. The problem is getting the positioning of object2 correct; it's not 100% accurate.

Here is my code:

angle = atan2(
    cEnemy->position.y - (float) position.y,
    cEnemy->position.x - (float) position.x) / 3.14159265f * 180);

position.x and position.y represent object1 's position.

That's my angle there for which object1 uses to rotate with. Now I'm stuck on how to go about positioning object2 .

I presume that I need to first get the central points of object1 which equal:

object2.x = (position.x + widthOfObject1 / 2);
object2.y = (position.y + heightOfObject1 / 2);

But then I'm just unsure about what to-do with the angle that object1 will face, and how to use that to position object2 correctly. I am pretty sure that I have to use sin or cos here, but I am unsure were. My idea is to position object2 so that not matter what angle it's at, object2 will also be in-front of object1 by a small margin.

Any help would be appreciated!

It seems like what you want to do is object2 is at a fixed distance D from Position P and you want to rotate it around P by angle A

So a unit vector along A is [ cos(A) , sin(A) ]

so a vector along A of magnitude D is [ D cos(A) , D sin(A) ]

So the position of object 2 should be

object2.x = object1.x + D * cos(A)
object2.y = object1.y + D * sin(A)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM