- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:只是想让我的问题更清楚。我几乎很难看到像 Matrix.CreateTransformationZ 这样的东西如何在矩阵乘法的背景下工作,更重要的是它对屏幕空间/世界空间的作用,这样我才能得到更清晰的图片。因此,也许有人可以更改代码或给我一个简短的片段来测试我可以在哪里使用它来绕轴旋转和/或绕轴轨道运行。我还更改了示例。
所以我仍然很难想象矩阵如何与 xna 屏幕空间一起工作。
我给你举个例子:
public class Game1 : Microsoft.Xna.Framework.Game
{
Texture2D shipTexture, rockTexture;
Vector2 shipPosition = new Vector2(100.0f, 100.0f);
Vector2 rockPosition = new Vector2(100.0f, 29.0f);
int count;
float shipRotation, rockRotation;
float rockSpeed, rockRotationSpeed;
bool move = true;
const int rock = 0;
const int ship = 1;
Color[] rockColor;
Color[] shipColor;
float testRot = 0.0f;
Vector2 shipCenter; int shipWidth, shipHeight;
Vector2 rockCenter; int rockWidth, rockHeight;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
#region maincontent
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
rockSpeed = 0.16f;
rockRotationSpeed = 0.3f;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
shipTexture = Content.Load<Texture2D>("Images\\ship");
rockTexture = Content.Load<Texture2D>("Images\\asteroid");
rockWidth = rockTexture.Width; rockHeight = rockTexture.Height;
shipWidth = shipTexture.Width; shipHeight = shipTexture.Height;
rockCenter = new Vector2(rockWidth / 2, rockHeight / 2);
shipCenter = new Vector2(shipWidth / 2, shipHeight / 2);
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
rockColor = new Color[rockTexture.Width * rockTexture.Height];
rockTexture.GetData(rockColor);
shipColor = new Color[shipTexture.Width * shipTexture.Height];
shipTexture.GetData(shipColor);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(rockTexture, rockPosition,
null, Color.White, testRot, rockCenter, 1.0f,
SpriteEffects.None, 0.0f);
spriteBatch.Draw(shipTexture, shipPosition,
null, Color.White, shipRotation, shipCenter,
1.0f, SpriteEffects.None, 0.0f);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
#endregion
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
testRot += 0.034906585f;
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateAsteroid(gameTime);
RotateShip(gameTime);
MoveShip(gameTime);
// TODO: Add your update logic here
CheckCollisions();
base.Update(gameTime);
}
#region Collisions
public Color PixelColor(int objectNum, int pixelNum)
{
switch (objectNum)
{
case rock:
return rockColor[pixelNum];
case ship:
return shipColor[pixelNum];
}
return Color.White;
}
public bool PixelCollision(Matrix transformA, int pixelWidthA, int pixelHeightA, int A,
Matrix transformB, int pixelWidthB, int pixelHeightB, int B)
{
Matrix temp = Matrix.Invert(transformB);
Matrix AtoB = transformA * Matrix.Invert(transformB);
Vector2 columnStep, rowStep, rowStartPosition;
columnStep = Vector2.TransformNormal(Vector2.UnitX, AtoB);
rowStep = Vector2.TransformNormal(Vector2.UnitY, AtoB);
rowStartPosition = Vector2.Transform(Vector2.Zero, AtoB);
for (int rowA = 0; rowA < pixelHeightA; rowA++)
{
// begin at the left
Vector2 pixelPositionA = rowStartPosition;
// for each column in the row (move left to right)
for (int colA = 0; colA < pixelWidthA; colA++)
{
// get the pixel position
int X = (int)Math.Round(pixelPositionA.X);
int Y = (int)Math.Round(pixelPositionA.Y);
// if the pixel is within the bounds of B
if (X >= 0 && X < pixelWidthB && Y >= 0 && Y < pixelHeightB)
{
// get colors of overlapping pixels
Color colorA = PixelColor(A, colA + rowA * pixelWidthA);
Color colorB = PixelColor(B, X + Y * pixelWidthB);
// if both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
return true; // collision
}
// move to the next pixel in the row of A
pixelPositionA += columnStep;
}
// move to the next row of A
rowStartPosition += rowStep;
}
return false; // no collision
}
public Matrix Transform(Vector2 center, float rotation, Vector2 position)
{
return Matrix.CreateTranslation(new Vector3(-center, 0.0f)) *
Matrix.CreateRotationZ(rotation) *
Matrix.CreateTranslation(new Vector3(position, 0.0f));
}
public static Rectangle TransformRectangle(Matrix transform, int width, int height)
{
Vector2 leftTop = new Vector2(0.0f, 0.0f);
Vector2 rightTop = new Vector2(width, 0.0f);
Vector2 leftBottom = new Vector2(0.0f, height);
Vector2 rightBottom = new Vector2(width, height);
Vector2.Transform(ref leftTop, ref transform, out leftTop);
Vector2.Transform(ref rightTop, ref transform, out rightTop);
Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
Vector2.Transform(ref rightBottom, ref transform, out rightBottom);
Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom));
Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom));
return new Rectangle((int)min.X, (int)min.Y,
(int)(max.X - min.X), (int)(max.Y - min.Y));
}
private void CheckCollisions()
{
Matrix shipTransform, rockTransform;
Rectangle shipRectangle, rockRectangle;
rockTransform = Transform(rockCenter, rockRotation, rockPosition);
rockRectangle = TransformRectangle(rockTransform, rockWidth, rockHeight);
shipTransform = Transform(shipCenter, shipRotation, shipPosition);
shipRectangle = TransformRectangle(shipTransform, shipWidth, shipHeight);
if (rockRectangle.Intersects(shipRectangle)) // rough collision check
if (PixelCollision( // exact collision check
rockTransform, rockWidth, rockHeight, rock,
shipTransform, shipWidth, shipHeight, ship))
move = false;
}
#endregion
#region Moves_and_Rotations
private void UpdateAsteroid(GameTime gameTime)
{
float timeLapse = (float)gameTime.ElapsedGameTime.Milliseconds;
if (move == true)
{
if ((rockWidth + rockPosition.X >= Window.ClientBounds.Width))
{
rockSpeed *= -1.0f;
rockPosition.X += rockSpeed * timeLapse;
}
else if ((rockPosition.X <= 0))
{
rockSpeed *= -1.0f;
rockPosition.X += rockSpeed * timeLapse;
}
else
rockPosition.X += rockSpeed * timeLapse;
const float SCALE = 50.0f;
rockRotation += rockRotationSpeed * timeLapse / SCALE;
rockRotation = rockRotation % (MathHelper.Pi * 2.0f);
}
}
private float RotateShip(GameTime gameTime)
{
float rotation = 0.0f;
float speed = gameTime.ElapsedGameTime.Milliseconds / 300.0f;
if (!move)
return rotation;
KeyboardState keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.Right))
rotation = speed;
else if (keyboard.IsKeyDown(Keys.Left))
rotation = -speed;
shipRotation += rotation;
shipRotation = shipRotation % (MathHelper.Pi * 2.0f);
return shipRotation;
}
private void MoveShip(GameTime gameTime)
{
const float SCALE = 20.0f;
float speed = gameTime.ElapsedGameTime.Milliseconds / 100.0f;
KeyboardState keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.Up))
{
shipPosition.X += (float)Math.Sin(shipRotation) * speed * SCALE;
shipPosition.Y -= (float)Math.Cos(shipRotation) * speed * SCALE;
}
else if (keyboard.IsKeyDown(Keys.Down))
{
shipPosition.X -= (float)Math.Sin(shipRotation) * speed * SCALE;
shipPosition.Y += (float)Math.Cos(shipRotation) * speed * SCALE;
}
}
#endregion
}
我从 XNA Game Creators 那里得到了这个,它只是一种进行像素检测的方法。
在上面的 Transform 方法中,矩阵乘法发生在我猜的矩形上。屏幕空间/世界空间到底发生了什么?
作者为什么要将这个矩阵乘以另一个矩阵的逆矩阵? (他提到,不知怎的,这使得它相对于其他 Assets )
最佳答案
屏幕空间大概与客户端空间相同。客户端空间从左上角的 (0,0) 到右下角的 (宽度, 高度)。 “向上”是Y-。
投影空间从左下角的 (-1,-1) 到右上角的 (1,1)。这就是 GPU 用于最终渲染的内容。 SpriteBatch 会为您处理此问题(相比之下:BasicEffect 要求您提供投影矩阵)。
世界空间是您想要的任何东西。这是您的游戏发生的坐标系。在您的示例中,这似乎与以下相同客户空间。
传统上,在执行此类操作时,您会在自己的空间中定义一个对象。在您的示例中,岩石和船舶矩形被硬编码到函数 TransformRectangle
中作为变量的初始值topLeft
通过bottomRight
.
然后,每个对象就有一个世界矩阵。这会将对象从其自己的空间移动到其在世界空间中的位置。在您的示例中,这是 shipTransform
和rockTransform
。世界变换也在 SpriteBatch.Draw
内完成,基于您传入的参数(使用纹理本身作为初始对象)。
然后你就有了一个 View 矩阵 - 你可以将其视为你的相机。你的例子没有其中之一。但是,例如,如果您想要平移 View 以跟随玩家,则可以在此处使用根据玩家位置创建的平移矩阵(并将其传递给 SpriteBatch.Begin
)。
最后,您有一个投影矩阵,它将您的世界空间转换为投影空间,以便 GPU 可以渲染您的场景。
现在这里可能存在的问题是 SpriteBatch 内部定义了一个投影矩阵,用于将客户端空间转换为投影空间(因此它基本上“假设”世界空间是客户端空间)。在您的示例中这不是问题,因为两个空格相同。
如果您的世界空间与客户端空间不相同,并且您想使用SpriteBatch,则必须创建一个额外的矩阵以从世界空间转换为客户端空间并将其插入到 View 之间和 Project 矩阵(即:将其与 View 相乘并将其传递到 SpriteBatch.Begin
)。
如果您的世界空间定义的“向上”(或“向右”)方式与 SpriteBatch 不同,那么您必须记住 SpriteBatch.Draw
使用的原始对象。将“up”定义为 Y-。
关于XNA - 关于世界空间和屏幕空间之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3538820/
一个关于Lua和元表的初学者问题,以一个简单的Hello-World为例,涉及len事件,不幸的是它没有返回预期的结果(我使用的是从 Ubuntu 官方存储库安装的 Lua 5.1)。 案子 这是示例
我正在开发一个 OpenGL 应用程序。我需要一个地球自转模型(蓝色的水,绿色的土地)。我的问题不是处理 OpenGL/图形……而是处理数据。 我在哪里可以获得这些数据? (各个国家的边界)。如果
我试图了解在 OpenGL 中创建空间: 对象空间 世界空间 相机空间 投影空间 我对这些阶段的理解是否正确? “立方体”是在笛卡尔坐标系的中心创建的,直接在程序内部输入顶点坐标。 坐标转换为“世界”
我有一个Entity和Bundle的Components,我想附加到将成为第一个实体的子实体的实体上。我可以使用Commands生成带有我的组件的实体,但是我无法获得实际的Entity,这意味着我不能
我想创建一个游戏,它有一个无尽的(实际上是一个非常大的)世界,玩家可以在其中移动。我是否会抽出时间来实现游戏是一回事,但我发现这个想法很有趣,并且希望就如何实现它提供一些意见。 关键是要有一个所有数据
以下代码片段来自维基百科,是标准 Hello World! 的序言! Brainfuck 中的程序... 1. +++++ +++++ initialize counter (
就目前情况而言,这个问题不太适合我们的问答形式。我们期望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在尝试获取特定日期剩余的确切秒数、分钟数等。这听起来可能很愚蠢,但为什么结果会翻倍呢?这看起来不对,是吗? setInterval(function() { var startDate =
如果我有两个嵌套元素,假设: Foo 然后我对外部元素应用旋转,假设顺时针旋转 45 度: Foo 现在我想将内部 div 向下移动一点。 Foo http://j
在 WindowsForms 世界中,您可以获得可用图像编码器/解码器的列表 System.Drawing.ImageCodecInfo.GetImageDecoders() / GetImageEn
因此,作为我的计算机体系结构类(class)的一部分,我需要熟悉 Assembly,或者至少足够舒适,我正在尝试读取用户的输入然后重新打印(暂时),这是我的我是如何尝试用伪代码来展示它的: 声明 ms
在 HLSL 中有很多矩阵乘法,虽然我了解如何以及在何处使用它们,但我不确定它们是如何导出的或它们的实际目标是什么。 所以我想知道是否有在线资源可以解释这一点,我特别好奇将世界矩阵乘以 View 矩阵
我正在使用 http://jvectormap.owl-hollow.net/ 中的 jVectorMap一切正常。但世界地图的标准尺寸非常小。如果有人想打例如波斯尼亚和黑塞哥维那,他需要大眼镜!有可
我在做游戏。游戏由一个无限平面组成。单位必须在一个离散的正方形上,因此可以使用简单的 Location { x :: Int, y :: Int } 来定位它们。 可能有很多种Unit s。有些可能是
所以我创建了一个顶点着色器,它接受一个角度并计算旋转。尽管模型围绕世界中心而不是它自己的轴/原点旋转,但存在一个问题。 旁注:这是 2D 旋转。 如何让模型绕着自己的轴旋转? 这是我当前的顶点着色器:
所以我创建了一个顶点着色器,它接受一个角度并计算旋转。尽管模型围绕世界中心而不是它自己的轴/原点旋转,但存在一个问题。 旁注:这是 2D 旋转。 如何让模型绕着自己的轴旋转? 这是我当前的顶点着色器:
我最近萌生了创建标签云的想法,就像地球形状的动画一样。我从 ngdc.noaa.gov 中提取了海岸线坐标,并编写了一个小脚本,将其显示在我的浏览器中。现在您可以想象,整个海岸线由大约 48919 个
我使用 3d 模式渲染我的 2d 游戏,因为相机旋转和放大/缩小比 2d 模式容易得多。 现在我遇到了一个我似乎想不出如何解决的问题: 如何使我的世界的 2d 平面以 1 个纹理像素匹配屏幕上的 1
我正在寻找一种简单快捷的方法来清除整个 Phaser 屏幕,就像在 HTML5 canvas 中,您可以通过将 Canvas 的宽度重置为自身来删除所有内容。我无法通过搜索找到任何此类方法 - 只有
是否可以为这样的游戏创建一个 2.5D 世界(双龙)http://www.youtube.com/watch?v=xb9P0YTeq5Y使用box2d? 你知道任何工作示例吗? 预先感谢您的时间。 最
我是一名优秀的程序员,十分优秀!