我们目前正在为大学开发一款 Xna 游戏。 在放入我们自己的纹理等之前,我们首先要测试一些东西。
所以我们看了很多教程并浏览了很多代码。 现在我们正在运行这个小程序: http://www.konter.at/Konter_Game.rar
这就是游戏屏幕,我们在其中加载玩家、背景以及我们想要使用的所有内容。
namespace Casual_Game
{
public class GameplayScreen : GameScreen
{
Player player;
Camera camera;
SpriteFont font;
private List<Layer> layers;
private SpriteBatch _spriteBatch;
Rectangle recPlayer, recGround;
bool colis = false;
Collision collision;
public override void LoadContent(ContentManager content, InputManager input)
{
camera = new Camera(Game1.reference.GraphicsDevice.Viewport) { Limits = new Rectangle(0, 0, 4800, 720) };
collision = new Collision();
_spriteBatch = new SpriteBatch(Game1.reference.GraphicsDevice);
base.LoadContent(content, input);
player = new Player();
player.LoadContent(content, input);
font = content.Load<SpriteFont>("Font1");
layers = new List<Layer>
{
new Layer(camera) { Parallax = new Vector2(0.0f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.1f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.2f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.3f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.4f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.5f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.6f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(0.8f, 1.0f) },
new Layer(camera) { Parallax = new Vector2(1.0f, 1.0f) }
};
// Add one sprite to each layer });
layers[1].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer2") });
layers[2].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer3") });
layers[3].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer4") });
layers[4].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer5") });
layers[7].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer8") });
layers[8].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer9") });
// Add a few duplicates in different positions
layers[7].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer8"), position = new Vector2(900, 0) });
layers[7].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer8"), position = new Vector2(1400, 0) });
layers[7].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer8"), position = new Vector2(2700, 0) });
layers[8].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer9"), position = new Vector2(1600, 0) });
layers[8].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer9"), position = new Vector2(3200, 0) });
layers[8].Sprites.Add(new Sprite { texture = content.Load<Texture2D>("Textures/Layer9"), position = new Vector2(4800, 0) });
}
public override void UnloadContent()
{
base.UnloadContent();
}
public override void Update(GameTime gameTime)
{
player.Update(gameTime);
recGround = new Rectangle((int)layers[8].Sprites[0].Position.X, (int)layers[8].Sprites[0].Position.Y, layers[8].Sprites[0].Texture.Width, layers[8].Sprites[0].Texture.Height);
recPlayer = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Image.Width, player.Image.Height);
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Right))
camera.Move(new Vector2(200.0f * (float)gameTime.ElapsedGameTime.TotalSeconds, 0.0f), true);
if (keyboardState.IsKeyDown(Keys.Left))
camera.Move(new Vector2(-200.0f * (float)gameTime.ElapsedGameTime.TotalSeconds, 0.0f), true);
if (keyboardState.IsKeyDown(Keys.Down))
camera.Move(new Vector2(0.0f, 400.0f * elapsedTime), true);
if (keyboardState.IsKeyDown(Keys.Up))
camera.Move(new Vector2(0.0f, -400.0f * elapsedTime), true);
if (recPlayer.Intersects(recGround))
{
if (collision.PixelCollision(player.Image, layers[8].Sprites[0].Texture, recPlayer, recGround))
{
player.Collision = true;
colis = true;
}
else
{
player.Collision = false;
colis = false;
}
}
else
{
player.Collision = false;
colis = false;
}
camera.LookAt(player.Position);
base.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
Vector2 parallax = new Vector2(1.0f);
_spriteBatch.Begin(SpriteSortMode.Deferred,null , null, null, null, null, camera.GetViewMatrix(parallax));
foreach (Layer layer in layers)
layer.Draw(_spriteBatch);
player.Draw(_spriteBatch);
spriteBatch.DrawString(font, "Player X : " + player.Position.X.ToString(),new Vector2(10, 10), Color.Black);
spriteBatch.DrawString(font, "CameraX :" + camera._position.X.ToString(), new Vector2(10, 25), Color.Black);
spriteBatch.DrawString(font, "CameraY :" + camera._position.Y.ToString(), new Vector2(10, 40), Color.Black);
spriteBatch.DrawString(font, "Col : " + colis.ToString(), new Vector2(10, 55), Color.Black);
_spriteBatch.End();
}
}
你看到它是一个具有不同图层的视差背景。第 [8] 层是我们的玩家稍后运行的“地面”。
现在的问题是碰撞检测无法正常工作。 看起来检测加载速度很慢之类的。
我们认为,玩家将在检测到时落地。所以他停在地面上就没有任何值(value)。所以必须要exate。
这是碰撞类:
public bool PixelCollision(Texture2D sprite1, Texture2D sprite2, Rectangle player, Rectangle enemy)
{
Color[] colorData1 = new Color[sprite1.Width * sprite1.Height];
Color[] colorData2 = new Color[sprite2.Width * sprite2.Height];
sprite1.GetData<Color>(colorData1);
sprite2.GetData<Color>(colorData2);
int top, bottom, left, right;
top = Math.Max(player.Top, enemy.Top);
bottom = Math.Min(player.Bottom, enemy.Bottom);
left = Math.Max(player.Left, enemy.Left);
right = Math.Min(player.Right, enemy.Right);
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
Color A = colorData1[(y - player.Top) * (player.Width) + (x - player.Left)];
Color B = colorData2[(y - enemy.Top) * (enemy.Width) + (x - enemy.Left)];
if (A.A != 0 && B.A != 0)
return true;
}
}
return false;
}
}
如果有人能帮助我们解决这个问题,我们将非常高兴。 我们并不是那么多的程序员。所以我们认为询问不需要任何费用。 :)
K.
编辑: Player.更新和绘制:
public override void Update(GameTime gameTime)
{
moveAnimation.IsActive = true;
inputManager.Update();
if (inputManager.KeyDown(Keys.Right, Keys.A))
{
moveAnimation.currentFrame = new Vector2(moveAnimation.CurrentFrame.X, 2);
velocity.X = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else if (inputManager.KeyDown(Keys.Left, Keys.A))
{
moveAnimation.currentFrame = new Vector2(moveAnimation.CurrentFrame.X, 1);
velocity.X = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
else
{
velocity.X = 0;
moveAnimation.IsActive = false;
}
if (inputManager.KeyDown(Keys.Up, Keys.W) && jump)
{
velocity.Y = -jumpSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; //Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;
jump = false;
}
if (!jump)
velocity.Y += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
velocity.Y = 0;
moveAnimation.Position = position += velocity;
jump = moveAnimation.Position.Y >= 480;
if (jump)
position.Y = 480;
base.Update(gameTime);
moveAnimation.Update(gameTime);
}
public override void Draw(SpriteBatch spriteBatch)
{
moveAnimation.Draw(spriteBatch);
}
图层绘制:
public class Layer
{
SpriteBatch spr1teBatch;
public Layer(Camera camera)
{
_camera = camera;
Parallax = Vector2.One;
Sprites = new List<Sprite>();
spr1teBatch = new SpriteBatch(Game1.reference.GraphicsDevice );
}
public Vector2 Parallax { get; set; }
public List<Sprite> Sprites { get; private set; }
public void Draw(SpriteBatch spriteBatch)
{
spr1teBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _camera.GetViewMatrix(Parallax));
foreach(Sprite sprite in Sprites)
sprite.Draw(spr1teBatch);
spr1teBatch.End();
}
public Vector2 WorldToScreen(Vector2 worldPosition)
{
return Vector2.Transform(worldPosition, _camera.GetViewMatrix(Parallax));
}
public Vector2 ScreenToWorld(Vector2 screenPosition)
{
return Vector2.Transform(screenPosition, Matrix.Invert(_camera.GetViewMatrix(Parallax)));
}
private readonly Camera _camera;
}
最佳答案
让我们想象一下您正在绘制的内容:
您在屏幕上的recPlayer.x、recPlayer.y处绘制玩家。然后在recGround.x、recGround.y处绘制图层。但这个位置是由 View 矩阵变换的。
您现在要做的是迭代两个矩形的所有相交像素。生成的 x 和 y 值位于屏幕空间中。我突出显示了 x 和 y 的示例位置。您现在需要在这个位置检索机器人纹理的颜色。
播放器的颜色可以很容易地检索,因为它已经在屏幕空间中。纹理中的位置为 xTex = x - recPlayer.x
和 yTex = y - recPlayer.y
。颜色数组中的索引可以通过index = yTex * width + xTex
计算。您已经完成了此操作来访问颜色。
检索地面的颜色有点棘手,因为它不像玩家那样位于屏幕空间中。它可能是经过翻译并稍微缩放的。您还没有发布代码,说明图层的 Sprite 是如何实际绘制的。我假设,它是在recGround.x、recGround.y位置绘制的(然后进行转换)。我们需要做的是计算 x/y 在地面世界空间中的位置。所以我们使用您已有的方法:
var screenPosition = new Vector2(x, y);
var groundPosition = ScreenToWorldPos(screenPos);
这基本上会反转图层的变换。如果向右平移 20 px,则新的 groundPosition 将位于原始位置左侧 20 px(因为这是实际位于该位置的像素的位置)。
您可以像对播放器那样使用 xTex = x - recGround.x
等访问颜色。
我刚刚意识到的另一个问题是相交矩形的计算。当然,你必须使用变换后的矩形来获取真实位置。
旧帖子:
问题是玩家和图层没有在同一个坐标系中绘制。
玩家是在“默认”系统中绘制的。这些图层是根据 View 矩阵进行变换绘制的,因此可以进行平移、缩放等操作。
在PixelCollision
中,您迭代所有可能相交的屏幕像素。这些像素与播放器一样位于“默认”系统中。因此,您可以使用以下方法计算纹理位置:
texX = x - player.x
texY = y - player.y
但是,这不适用于图层。您必须将 x
和 y
从屏幕空间转换到图层的世界空间。
layerPos = ScreenToWorld(new Vector2(x, y));
然后访问layerPos.x - heaven.x
和layerPos.y -enemy.y
处的颜色。如果您在系统中的 enemy.x、enemy.y
处绘制图层,则此方法有效。如果您有额外的变换或将它们绘制在其他地方,则需要考虑这一点。
关于Xna 像素碰撞和多层。早期碰撞,错误检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13251308/