c# raylib collision, could not convert

Viewed 22

I'm trying to get my collision detection to work. I'm new to programming and am just testing some stuff out. The problem I'm getting is when I'm putting in the two different rectangles that are supposed to detect collisions with each other. I've marked the problem with //THIS IS WHERE I GET THE ERROR!

ERROR CODE IS "(local variable) Rectangle player01 'player01' is not null here.

Argument 1: cannot convert from 'Grunder.Rectangle' to 'Raylib_cs.Rectangle' [labyrint0]"

using Raylib_cs;

namespace Grunder
{
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle floor01 = new Rectangle(50, 500, 700, 50, Color.WHITE);
            Rectangle player01 = new Rectangle(300, 300, 50, 50, Color.DARKBLUE);

            // Start graficMotor
            Raylib.InitWindow(800, 600, "My Raylib Window");

            Raylib.SetTargetFPS(60);

            while (!Raylib.WindowShouldClose())
            {
                Raylib.BeginDrawing();

                Raylib.ClearBackground(Color.BEIGE);

                floor01.Draw();
                player01.Draw();
                player01.IsPlayer();

                Raylib.EndDrawing();

                if (Raylib.CheckCollisionRecs(player01, floor01)) //THIS IS WHERE I GET THE ERROR!
                {
                    Raylib.DrawText("Kollision", 100, 50, 50, Color.ORANGE);
                }
            }
        }
    }

    public class Rectangle
    {
        public int posX;
        public int posY;
        public int length;
        public int width;
        Color color;

        int playerSpeed = 5;
        bool collisionRight;
        bool collsiionLeft;
        bool collisionUp;
        bool collisionDown;

        public Rectangle(int _posX, int _posY, int _length, int _width, Color _color)
        {
            posX = _posX;
            posY = _posY;
            length = _length;
            width = _width;
            color = _color;
        }

        public void Draw()
        {
            Raylib.DrawRectangle(posX, posY, length, width, color);
        }

        public void IsPlayer()
        {

           // Not used: int playerMaxSpeed = 5;
            if (Raylib.IsKeyDown(KeyboardKey.KEY_RIGHT))
            {
                posX += playerSpeed;
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_LEFT))
            {
                posX -= playerSpeed;
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_UP))
            {
                posY -= playerSpeed;
            }

            if (Raylib.IsKeyDown(KeyboardKey.KEY_DOWN))
            {
                posY += playerSpeed;
            }
        }
    }
}
1 Answers

As you can see from the error message, there are two different Rectangle types involved: Grunder.Rectangle and Raylib_cs.Rectangle. Raylib accepts only Raylib_cs rectangles.

Obviously, you have declared your own Rectangle class having a color property.

You could add it an implicit conversion. Something like this (depends on your implementation):

public static implicit operator Raylib_cs.Rectangle(Grunder.Rectangle r)
    => new Raylib_cs.Rectangle(r.x, r.y, r.width, r.height);

Note that Raylib_cs.Rectangle is a struct, so that you cannot derive your implementation from it. But alternatively, you could make your Rectangle a wrapper for Raylib_cs.Rectangle. Something like this

namespace Grunder;

public class Rectangle
{
    public Raylib_cs.Rectangle Rect;
    public Raylib_cs.Color Color;

    public Rectangle(float x, float y, float width, float height, Raylib_cs.Color color)
    {
        Rect.x = x;
        Rect.y = y;
        Rect.width = width;
        Rect.height = height;
        this.Color = color;
    }

    public Rectangle(Raylib_cs.Rectangle r, Raylib_cs.Color color)
    {
        Rect = r;
        Color = color;
    }

    public float X { get => Rect.x; set => Rect.x = value; }
    public float Y { get => Rect.y; set => Rect.y = value; }
    public float Width { get => Rect.width; set => Rect.width = value; }
    public float Height { get => Rect.height; set => Rect.height = value; }

    public static implicit operator Raylib_cs.Rectangle(Rectangle r)
        => r.Rect;
}
Related