计算缩放到碰撞检测

时间:2014-08-04 21:03:18

标签: c# xna monogame

当我自学c#时,我一直在使用Monogame。我按照微软XNA教程检测了2d的碰撞。 Here

到目前为止,我在Monogame中所做的非常基本。有2个精灵。球员和煤炭纹理。当你开始游戏时煤炭出现在一个随机点,你可以移动玩家。我扩大了纹理。

Microsoft教程没有处理缩放问题,我想知道是否有办法通过碰撞检测方法获得我将纹理缩放的量。

这是我的代码。

这是Game1课程。碰撞检测方法位于底部。

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input.Touch;
using System.Diagnostics;
#endregion

namespace Supersum
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player player;
    Coal coal;
    Random random;

    KeyboardState currentKeyboardState;
    KeyboardState previousKeyboardState;

    GamePadState currentGamePadState;
    GamePadState previousGamePadState;


    float playerMoveSpeed;

    public Game1()
        : base()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 480;
        graphics.ApplyChanges();
        Content.RootDirectory = "Content";

    }


    protected override void Initialize()
    {
        player = new Player();
        coal = new Coal();
        random = new Random();


        playerMoveSpeed = 8.0f;

        base.Initialize();
    }


    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
        Vector2 coalPosition = new Vector2(random.Next(800), random.Next(480));
        player.Initialize(Content.Load<Texture2D>("tempChar"), playerPosition);
        coal.Initialize(Content.Load<Texture2D>("Coal"), coalPosition);



    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }


    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();


        previousGamePadState = currentGamePadState;
        previousKeyboardState = currentKeyboardState;

        currentKeyboardState = Keyboard.GetState();
        currentGamePadState = GamePad.GetState(PlayerIndex.One);


        UpdatePlayer(gameTime);
        UpdateCollision();

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointWrap, null, null, null);

        coal.Draw(spriteBatch);
        player.Draw(spriteBatch);

        spriteBatch.End();


        base.Draw(gameTime);
    }

    private void UpdatePlayer(GameTime gameTime)
    {
        if (currentKeyboardState.IsKeyDown(Keys.Left)||currentKeyboardState.IsKeyDown(Keys.A))
        {
            player.Position.X -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Right)||currentKeyboardState.IsKeyDown(Keys.D))
        {
            player.Position.X += playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Up)||currentKeyboardState.IsKeyDown(Keys.W))
        {
            player.Position.Y -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Down)||currentKeyboardState.IsKeyDown(Keys.S))
        {
            player.Position.Y += playerMoveSpeed;
        }
    }

    private void UpdateCollision()
    {
        Rectangle rectangle1;
        Rectangle rectangle2;

        rectangle1 = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Width * 2, player.Height * 2);
        rectangle2 = new Rectangle((int)coal.Position.X, (int)coal.Position.Y, coal.Width * 4, coal.Height * 4);

        if (rectangle1.Intersects(rectangle2))
        {
            Console.Write("Intersected\n");
        }
        else {
            Console.Write("Not Intersected\n");
        }

    }
}
}

这些是我的球员和煤炭的课程。

播放器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Supersum
{
class Player
{
    public Texture2D PlayerTexture;
    public Vector2 Position;
    public bool Active;
    public int Health;

    public int Width
    {
        get { return PlayerTexture.Width; }
    }

    public int Height
    {
        get { return PlayerTexture.Height; }
    }

    public void Initialize(Texture2D texture, Vector2 position)
    {
        PlayerTexture = texture;
        Position = position;
        Active = true;
        Health = 100;
    }

    public void Update()
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f);
    }
 }
}

现在煤炭

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Graphics;

namespace Supersum
{
class Coal
{
    public Texture2D coalTexture;
    public Vector2 Position;

    public int Width
    {
        get { return coalTexture.Width; }
    }

    public int Height
    {
        get { return coalTexture.Height; }
    }

    public void Initialize(Texture2D texture, Vector2 position)
    {
        coalTexture = texture;
        Position = position;
    }

    public void Update()
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(coalTexture, Position, null, Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 0f);
    }
}
}

2 个答案:

答案 0 :(得分:0)

不要对你的音阶进行硬编码。

您应该向float scale;class Player添加class Coal,然后在目前使用比例2的所有地方使用该成员4

答案 1 :(得分:0)

为缩放创建一个私有字段,并在Draw和Height / Width中使用它。这样,您就不必在碰撞检测中使用任何缩放

class Coal
{
    public Texture2D coalTexture;
    public Vector2 Position;
    private float scale = 4f;

    public int Width
    {
        get { return coalTexture.Width * scale; }
    }

    public int Height
    {
        get { return coalTexture.Height * scale; }
    }

    [...]

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(coalTexture, Position, null, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
    }
}

如果他们容易在多个地方进行更改和/或使用(通常在制作游戏时需要进行大量调整),您还可以创建一个包含所有比例的静态类:

public static class ScaleConstants
{
    public static float Player = 2f;
    public static float Coal = 4f;
}

哪会将来自(例如)return coalTexture.Width * scale;的调用更改为return coalTexture.Width * ScaleConstants.Coal;