0

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;
            }
        }
    }
}
Max Play
  • 3,717
  • 1
  • 22
  • 39
Darre
  • 21
  • 2
  • 2
    Raylib seems to provide their own Rectangle structure. Passing yours will not work, because it is not of the correct type. You may have to convert it to a `Raylib_cs.Rectangle`. – Max Play Sep 18 '22 at 16:40
  • 1
    What kind of error are you getting? A compilation error, a runtime exception or not the expected behavior? Please, explain the error. What is the error message? – Olivier Jacot-Descombes Sep 18 '22 at 16:40
  • But I'm using Raylib.DrawRectangle, isn't that the raylib rectangle structure? – Darre Sep 18 '22 at 16:50
  • The error message is "error CS1503: Argument 1: cannot convert from 'Grunder.Rectangle' to 'Raylib_cs.Rectangle" – Darre Sep 18 '22 at 16:52

1 Answers1

0

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;
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188