簡體   English   中英

C#-我的碰撞代碼不會導致播放器停止在其音軌中

[英]C# - My Collision code won't result in the player stopping in his tracks

我一直在研究我的LWJGL引擎之一的C#和SFML重制。 到目前為止,一切都很好,但是,當我嘗試復制/轉換碰撞代碼時,事情進展得並不順利。

這是我的玩家/實體類,邊界框和游戲代碼。

實體:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace FastSrc_CS.Core
{
    public class FSEntity
    {
        public Vector2f Position, Size;
        public Color EColor;

        public RectangleShape sprite;

        public BoundingBox Bounds;

        public bool canMove;
        public float VelX, VelY;

        public FSEntity()
        {

        }

        public virtual void Init(Vector2f pos, Vector2f size, Color color)
        {
            Position = pos;
            Size = size;
            EColor = color;

            sprite = new RectangleShape();
            sprite.FillColor = EColor;
            sprite.Position = Position;
            sprite.Size = Size;
            //sprite.Origin = new Vector2f(size.X / 2, size.Y / 2);

            Bounds = new BoundingBox(Position, Size);
        }

        public void SetVelX(float x)
        {
            VelX = x;
        }

        public void SetVelY(float x)
        {
            VelY = x;
        }

        public Vector2f GetOrigin()
        {
            return new Vector2f(Size.X / 2, Size.Y / 2);
        }

        public virtual void Update()
        {

        }

        public void UpdatePos()
        {
            Position.X += VelX;
            Position.Y += VelY;
            sprite.Position = Position;
            Bounds.UpdateBounds(Position, Size);
        }

        public virtual void Render(RenderWindow w)
        {
            w.Draw(sprite);
        }
    }
}

玩家:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace FastSrc_CS
{
    class Player : Core.FSEntity
    {
        public float speed = 6f;

        public override void Init(Vector2f pos, Vector2f size, Color color)
        {
            canMove = true;
            base.Init(pos, size, color);
        }

        public override void Update()
        {
            base.Update();

            Movement();

            Position.X += VelX;
            Position.Y += VelY;
            sprite.Position = Position;
            Bounds.UpdateBounds(Position, Size);
        }

        public void Movement()
        {
            if (Keyboard.IsKeyPressed(Keyboard.Key.A) && canMove == true)
            {
                SetVelX(-speed);
                SetVelY(0);
            }
            else if (Keyboard.IsKeyPressed(Keyboard.Key.D) && canMove == true)
            {
                SetVelX(speed);
                SetVelY(0);
            }
            else if (Keyboard.IsKeyPressed(Keyboard.Key.W) && canMove == true)
            {
                SetVelY(-speed);
                SetVelX(0);
            }
            else if (Keyboard.IsKeyPressed(Keyboard.Key.S) && canMove == true)
            {
                SetVelY(speed);
                SetVelX(0);
            }
            else
            {
                SetVelX(0);
                SetVelY(0);
            }
        }

        public override void Render(RenderWindow w)
        {
            base.Render(w);
        }
    }
}

邊界框:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace FastSrc_CS.Core
{
    public class BoundingBox
    {
        public FloatRect Rectangle;

        public BoundingBox(Vector2f pos, Vector2f size)
        {
            Rectangle = new FloatRect(pos, size);
        }

        public bool Collide(BoundingBox b)
        {
            bool col = false;

            if (this.Rectangle.Intersects(b.Rectangle))
            {
                col = true;
            }
            else
            {
                col = false;
            }

            return col;
        }

        public void UpdateBounds(Vector2f pos, Vector2f size)
        {
            Rectangle.Width = size.X;
            Rectangle.Height = size.Y;
            Rectangle.Left = pos.X;
            Rectangle.Top = pos.Y;
        }
    }
}

游戲:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastSrc_CS.Core;
using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace FastSrc_CS
{
    public class Game : FSGame
    {
        Player p = new Player();
        Wall w = new Wall();

        public override void Init()
        {
            p.Init(new Vector2f(400, 300), new Vector2f(32, 32), Color.Red);
            w.Init(new Vector2f(100, 100), new Vector2f(32, 32), Color.Blue);

            Entities.Add(p);
            Entities.Add(w);
        }

        public override void Update()
        {
            Console.WriteLine(p.Position.X);

            //COLLISION CODE FOR WALLS
            if (p.Bounds.Collide(w.Bounds))
            {
                //Right Collision
                if (p.VelX > 0)
                {
                    Console.WriteLine("Right Collision");
                    p.canMove = false;
                    p.Position.X = w.Position.X - 32;
                    p.SetVelX(0);
                }
                else if (p.VelX < 0)
                {
                    Console.WriteLine("Left Collision");
                    p.canMove = false;
                    p.Position.X = w.Position.X + 32;
                    p.SetVelX(0);
                }

                if (p.VelX == 0)
                {
                    p.canMove = true;
                }
            }

            Entities.ForEach(k => k.Update());
        }

        public override void Render(RenderWindow w)
        {
            Entities.ForEach(k => k.Render(w));
        }
    }
}

問題出在該區域的“游戲”類之內:

        //COLLISION CODE FOR WALLS
        if (p.Bounds.Collide(w.Bounds))
        {
            //Right Collision
            if (p.VelX > 0)
            {
                Console.WriteLine("Right Collision");
                p.canMove = false;
                p.Position.X = w.Position.X - 32;
                p.SetVelX(0);
            }
            else if (p.VelX < 0)
            {
                Console.WriteLine("Left Collision");
                p.canMove = false;
                p.Position.X = w.Position.X + 32;
                p.SetVelX(0);
            }

            if (p.VelX == 0)
            {
                p.canMove = true;
            }
        }

當我執行此操作時,播放器將停止移動,但會稍微植入牆壁。 我知道您在想什么,“將使p.canMove = true的if語句p.canMove = true沖突的if語句之外。” 我已經嘗試過了,它導致播放器與立方體來回彈跳。 我嘗試過不同的方法,以至於我考慮將這段代碼省去。 我決定退回到Stack Overflow,看看是否有人可以幫助我。 提前致謝。

我認為代碼根本不是您的引擎-給定結果,並且在掃描代碼后,這似乎還不錯。 由於播放器夾在牆上,因此播放器和牆壁的硬編碼值32都是不正確的。 嘗試這個:

        if (p.VelX > 0)
        {
            Console.WriteLine("Right Collision");
            p.canMove = false;
            //move the right of our player to the left of the wall by setting
            //his x to the wall's x minus his width.
            p.Position.X = w.Position.X - p.Size.Width;
            p.SetVelX(0);
        }
        else if (p.VelX < 0)
        {
            Console.WriteLine("Left Collision");
            p.canMove = false;
            //Move the left of our player to the right of the wall, or
            //the wall's x plus the wall's width
            p.Position.X = w.Position.X + w.Size.Width;
            p.SetVelX(0);
         }

希望對您有所幫助。

編輯

實際上,我認為我發現了另一個問題。 當在檢測到碰撞后移動其X后更改玩家的位置時,您忘記調用UpdatePos()方法。 因此,您移動的是他的位置,而不是實際的矩形,這可能會導致錯誤。 而是,在移動他的x之后,立即調用UpdatePos()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM