XNA 4.0 中的 2D BoundingRectangle 旋转 [英] 2D BoundingRectangle rotation in XNA 4.0

查看:58
本文介绍了XNA 4.0 中的 2D BoundingRectangle 旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定需要做什么才能在我的游戏中为精灵对象提供 2D BoundingRectangle,根据用户希望精灵移动的方式旋转和更新位置.

I'm having trouble working out what I need to do in order to have a 2D BoundingRectangle for a sprite object in my game rotate and update position depending on which way the user wants the sprite to travel.

我使用旋转和缩放值绘制精灵,如下所示:

I draw out the sprite with it's rotation and scale values like so:

spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, SpriteEffects.None, 1f);

我当前获取 BoundingRectangle 的设置是:

My current setup for getting the BoundingRectangle is:

public Rectangle BoundingRectangle
{
    get { return new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); }
}

以上是非常基本的,显然不会随着精灵旋转.我已经阅读了一些 QA 帖子并尝试了各种方法,但它们似乎都没有实现我所追求的目标,也没有产生非常奇怪的行为.

The above is very basic and obviously won't rotate with the sprite. I've read a few QA posts and tried various things but none of them seem to achieve what I'm after or create very odd behaviours.

如果您能就我需要做的事情提出一些建议,我将不胜感激.

I'd be grateful for some suggestions on what I need to do.

推荐答案

Rectangle 类不包含旋转信息,因此您需要一些结构来包含它.

The Rectangle class doesn't contain rotation information, so you need some structure to contain that.

一个不错的选择是返回一个包含四个点的列表,这些点基于您现在返回的矩形和对其应用的旋转.

A good choice would be to return a list of four points, which are based on the rectangle you are now returning and the rotation applied to it.

您可以使用 Matrix 类在一组点上应用旋转 像这样:

You can apply apply a rotation on a set of points using the Matrix class like so:

var m = Matrix.CreateRotationZ(angle);
Vector3 rotatedVector = Vector3.Transform(vector, m);

如果你想围绕某个点旋转,比如说你的精灵的中心,还应该做一个翻译:

If you want to rotate around a certain point, lets say the center of your sprite, a translation should also be done:

var rotation = Matrix.CreateRotationZ(angle);
var translateTo = Matrix.CreateTranslation(vector);
var translateBack = Matrix.CreateTranslation(-vector);
var comnbined = translateTo * rotation * translateBack;
Vector3 rotatedVector = Vector3.Transform(vectorToRotate, combined);

请务必查看可以使用矩阵完成的所有其他转换,矩阵是游戏/图形开发中的核心元素.

Be sure to check out all the other transformations that can be done with a matrix, matrices are core elements in game/graphics development.

这篇关于XNA 4.0 中的 2D BoundingRectangle 旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆