C#-我的冲突代码赢了';t导致运动员在比赛中停滞不前

C#-我的冲突代码赢了';t导致运动员在比赛中停滞不前,c#,sfml,sfml.net,C#,Sfml,Sfml.net,我一直在为我的一个LWJGL引擎进行C#&SFML翻拍。到目前为止,一切都很顺利,但是,当我试图复制/转换冲突代码时,事情进展得并不顺利 这是我的玩家/实体类代码、边界框和游戏代码 实体: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SFML.Graphics; using SFML.Sys

我一直在为我的一个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;
        }
    }
}
游戏:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用FastSrc_CS.Core;
使用SFML.Graphics;
使用SFML.System;
使用SFML.Window;
名称空间FastSrc\u CS
{
公共类游戏:FSGame
{
玩家p=新玩家();
墙w=新墙();
公共重写void Init()
{
p、 Init(新矢量2f(400300),新矢量2f(32,32),颜色为红色;
w、 Init(新Vector2f(100100),新Vector2f(32,32),Color.Blue);
增加(p);
实体。添加(w);
}
公共覆盖无效更新()
{
控制台写入线(p.Position.X);
//墙的碰撞代码
if(p.Bounds.Collide(w.Bounds))
{
//右碰撞
如果(p.VelX>0)
{
控制台写入线(“右碰撞”);
p、 canMove=false;
p、 位置X=w位置X-32;
p、 SetVelX(0);
}
否则如果(p.VelX<0)
{
控制台写入线(“左碰撞”);
p、 canMove=false;
p、 位置X=w位置X+32;
p、 SetVelX(0);
}
如果(p.VelX==0)
{
p、 canMove=true;
}
}
Entities.ForEach(k=>k.Update());
}
公共替代无效渲染(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;
            }
        }
//墙的碰撞代码
if(p.Bounds.Collide(w.Bounds))
{
//右碰撞
如果(p.VelX>0)
{
控制台写入线(“右碰撞”);
p、 canMove=false;
p、 位置X=w位置X-32;
p、 SetVelX(0);
}
否则如果(p.VelX<0)
{
控制台写入线(“左碰撞”);
p、 canMove=false;
p、 位置X=w位置X+32;
p、 SetVelX(0);
}
如果(p.VelX==0)
{
p、 canMove=true;
}
}

当我运行这个时,玩家将停止移动,但他将被稍微植入墙中。我知道你在想什么,“将使
p.canMove=true
的if语句移到碰撞的if语句之外。”我已经尝试过了,它会导致播放器与立方体来回弹跳。我尝试了不同的方法,以至于我想把这段代码删掉。我决定返回到堆栈溢出,看看是否有人可以帮助我。提前感谢。

我认为您的代码根本不存在引擎问题-考虑到结果,在扫描代码后,似乎还可以。因为你的玩家夹在墙上,你的玩家和你的墙的硬编码值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);
         }
if(p.VelX>0)
{
控制台写入线(“右碰撞”);
p、 canMove=false;
//通过设置将玩家的右侧移动到墙的左侧
//他的x到墙的x减去他的宽度。
p、 位置X=w.Position.X-p.Size.Width;
p、 SetVelX(0);
}
否则如果(p.VelX<0)
{
控制台写入线(“左碰撞”);
p、 canMove=false;
//将玩家的左侧移动到墙的右侧,或
//墙的x加上墙的宽度
p、 位置.X=w.Position.X+w.Size.Width;
p、 SetVelX(0);
}
我希望这有帮助

编辑


事实上,我想我发现了另一个问题。当您在碰撞检测后移动X后更改玩家的位置时,您忘记调用
UpdatePos()
方法。因此,您移动的是他的位置,而不是他的实际矩形,这可能会导致错误。相反,在你移动他的x后,立即调用
UpdatePos()

,虽然你告诉我使用玩家大小比硬编码更明智,但结果仍然是一样的。老实说,我不知道如何解决我的问题。好吧,读了这篇文章后,我意识到你在说什么。移动位置后,我应该调用
UpdatePos()
。我这么做了,它就像一个符咒。非常感谢你。
        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);
         }