Macroquad custom 2d collisions

I'm trying to make custom 2d collisions for 2 rectangles in macroquad but I keep running into the same error.

heres my code:

use macroquad::prelude::*;

#[derive(Debug, Clone)]
struct rectangle {
    x: f32,
    y: f32,
    w: f32,
    h: f32,
    color: macroquad::prelude::Color,  // should be <T>, I don't wanna deal with the custom color type, but macroquad has some bugs
    speed: f32,  // should be <U> , I want users to have more customiazation over speed, but macroquad has some bugs with this...

}


#[macroquad::main("InputKeys")]
async fn main() {
    let mut x: f32 = screen_width() / 2.0_f32;
    let mut y: f32 = screen_width() / 2.0_f32;
    let mut gravity_speed: f32 = 1.0_f32;
    let speed: f32 = 3.5_f32;
    let mut gameover: bool = false;

    let player_rect = rectangle { x: x, y: y, w: 5.0_f32, h: 5.0_f32, color: GREEN, speed: 3.5_f32 };
    let obstacle_rect = rectangle { x: 100.0_f32, y: 100.0_f32, w: 7.0_f32, h: 7.0_f32, color: RED, speed: 0.0_f32 };

    loop {
        clear_background(GRAY);

        if is_key_down(KeyCode::W) {
                y -= player_rect.speed;
        }
        if is_key_down(KeyCode::A) {
                x -= player_rect.speed;
        }
        if is_key_down(KeyCode::D) {
                x += player_rect.speed;
        }
        if is_key_down(KeyCode::S) {
                y += player_rect.speed
        }


        draw_rectangle(player_rect.x, player_rect.y, player_rect.w, player_rect.h, player_rect.color);

        draw_rectangle(obstacle_rect.x, obstacle_rect.y, obstacle_rect.w, obstacle_rect.h, obstacle_rect.color);

        rectangle_collisions(player_rect, obstacle_rect);

       /*if gameover == true {
            clear_background(GRAY);
            draw_text("YOU LOST",20.0, 105.0, 150.0, BLACK);
        }*/

        next_frame().await;
    }

}

fn rectangle_collisions(rect1: rectangle, rect2: rectangle2) {
    if rect1.x < rect2.x + rect2.w && rect1.x + rect1.w > rect2.x && rect1.y < rect2.y + rect2.h && rect1.h + rect1.y > rect2.y {
        println!("COLLISSSSIOOONNNN DETECTED!!!!!")
    }
}

Take the rectangles by reference instead:

rectangle_collisions(&player_rect, &obstacle_rect);


fn rectangle_collisions(rect1: &rectangle, rect2: &rectangle) {

That way you are not giving them away to the function and can use them again next loop.

It would also solve the immediate problem if you were to derive(Copy) on the rectangle type, but that is less generally applicable and may not work with your plans to make the type generic.

@kpreid, your awnser worked, but I still can't move the rectangle, anyone know why?

You’re updating the local variables x and y, but those aren't used anywhere inside the loop; rather you draw the rectangle based on player_rect.x and player_rect.y which aren't updated anywhere.

1 Like

player_rect.x is set to the local variables, which means I am updating player_rect.x (same with the y var), but nontheless your awnser worked.

That's not how variables work. player_rect.x and y are set once, before the loop, to the values of x and y, but when you update x and y, the values in player_rect don't automatically get updated. It doesn't work like in a spreadsheet.

1 Like

got it.