简体   繁体   English

围绕鼠标点旋转

[英]Rotate Around Point Towards Mouse

I'm thinking this should be a simple problem but I'm having a lot of trouble implementing this because there are so many things involved. 我认为这应该是一个简单的问题,但是由于涉及到很多事情,因此我在实施此方法时遇到了很多麻烦。

I am trying to rotate objects defined by their center around a defined point besides the center to face the location of the mouse 我正在尝试将由其中心定义的对象围绕除中心之外的 定义点 旋转 以面对鼠标的位置

I have the defined point in the world space and the mouse's location in the world space as well as the center of each item. 我在世界空间中定义了点,并在世界空间中找到了鼠标的位置以及每个项目的中心。

Please avoid giving answers on how to rotate towards mouse or how to rotate around a point individually - I'm trying to do both! 请避免给出有关如何朝鼠标旋转或如何围绕某个点旋转的答案-我正在尝试同时做这两个!

If this is a fair interpretation of your question, rotating from "last frame" to "this frame",, 如果这是对您问题的合理解释,请从“最后一帧”转到“此帧”, 在此处输入图片说明

If you know the location of the point that you want the sprite to rotate about and you know the distance away from that point that you want the sprite to be, try this: 如果您知道精灵要旋转的点的位置,并且知道精灵要离开的点的距离,请尝试以下操作:

float radius = ?.?f; // distance from pivot point to sprite
Vector2 spritePivot = new Vector2(?, ?); //location of pivot point
Vector2 mouseToPoint = spritePivot - new Vector2(mouseState.X, mouseState.Y);
mouseToPoint.Normalize();
float spriteAngle = MathHelper.Atan2(mouseToPoint.Y, mouseToPoint.X);
mouseToPoint *= radius;
Vector2 spriteLocation = spritePivot + mouseToPoint;

later, when drawing the sprite, use spriteAngle & spriteLocation as a params in the proper overload of SpriteBatch.Draw() 稍后,在绘制精灵时,在适当的SpriteBatch.Draw()重载中将spriteAngle和spriteLocation用作参数。

Dra 德拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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