• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Hover over a Draw rectangle and check for collision by mouse

R

Rhatos

Guest
I've drawn a rectangle in Draw and have been trying to get my the actual box to change colours when my mouse is inside of the rectangle.

I've tried point_in_rectangle with no luck. If anyone could help me, I would really appreciate it.

Draw Event:
Code:
if(global.WeaponSelect){
    //Weapons GUI
    //Layer
    draw_sprite_ext(sBlack,0,camera_get_view_x(cam)-view_w,camera_get_view_y(cam)-view_h,1,1,0,c_white,0.7)
    // Circle Part
    draw_circle_color(oGunHolder.x,oGunHolder.y,6,c_red,c_red,false)

    //Lines and Boxes
    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x+64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x+64,oGunHolder.y-64,oGunHolder.x+100,oGunHolder.y-32,box3,box3,box3,box3,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x,oGunHolder.y-64,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-18,oGunHolder.y-96,oGunHolder.x+18,oGunHolder.y-64,box2,box2,box2,box2,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x-64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32,box1,box1,box1,box1,true)

    //Actual line
    draw_line_color(oGunHolder.x,oGunHolder.y,mouse_x,mouse_y,c_white,c_white)
}
Step Event:
Code:
if(point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32)){
    box1 = c_blue
    game_end()
} else {
    box1 = c_red
}

if(keyboard_check(vk_tab)){
    global.WeaponSelect = true
} else {
    global.WeaponSelect = false
}
 

Simon Gust

Member
note that you are drawing at absolute positions but the functions window_mouse_get_x / y return relative to the view positions.
Either use mouse_x and mouse_y or translate all coordinates and draw them in the draw_gui event.
 
R

Rhatos

Guest
note that you are drawing at absolute positions but the functions window_mouse_get_x / y return relative to the view positions.
Either use mouse_x and mouse_y or translate all coordinates and draw them in the draw_gui event.
So I changed them to mouse_x, and mouse_y with no luck. What exactly do you mean by translate the coordinates and draw them with drawgui?
 
W

Wadlo

Guest
If you changed them to mouse_x and mouse_y, that should have worked... I can't come up with any explanation why not. If I copied your code into a separate project, it would work.

I suppose one explanation could be that the oGunHolder is changing positions between the step and the draw event, but that's unlikely. You can try it though. Try moving your code to change the color into the draw event right after drawing the rectangle and see if it works.

Code:
if(global.WeaponSelect){
    //Weapons GUI
    //Layer
    draw_sprite_ext(sBlack,0,camera_get_view_x(cam)-view_w,camera_get_view_y(cam)-view_h,1,1,0,c_white,0.7)
    // Circle Part
    draw_circle_color(oGunHolder.x,oGunHolder.y,6,c_red,c_red,false)

    //Lines and Boxes
    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x+64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x+64,oGunHolder.y-64,oGunHolder.x+100,oGunHolder.y-32,box3,box3,box3,box3,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x,oGunHolder.y-64,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-18,oGunHolder.y-96,oGunHolder.x+18,oGunHolder.y-64,box2,box2,box2,box2,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x-64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32,box1,box1,box1,box1,true)

    //added this
    if(point_in_rectangle(mouse_x,mouse_y,oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32)){
       box1 = c_blue
       show_message("It's inside the rectangle");
       game_end()
    }

    //Actual line
    draw_line_color(oGunHolder.x,oGunHolder.y,mouse_x,mouse_y,c_white,c_white)
}
 
L

Laurent57

Guest
Hi, have you checked the coordinates in debug mode to be sure?
 

Simon Gust

Member
So I changed them to mouse_x, and mouse_y with no luck. What exactly do you mean by translate the coordinates and draw them with drawgui?
if you draw on the GUI, you have to leave out adding camera_get_x and y to your drawing coordinates as these are automatically added.
Though it should be avoided to draw any game objects as these will follow the camera and draw infront of everything else.

I just see now that in your point_in_rectangle function you switched the arguments.
The arguments for the left and top border (x1 and y1) should always be smaller than the right and bottom border (x2 and y2).
I just tried it and it does infact not work if they are switched.
 
R

Rhatos

Guest
If you changed them to mouse_x and mouse_y, that should have worked... I can't come up with any explanation why not. If I copied your code into a separate project, it would work.

I suppose one explanation could be that the oGunHolder is changing positions between the step and the draw event, but that's unlikely. You can try it though. Try moving your code to change the color into the draw event right after drawing the rectangle and see if it works.

Code:
if(global.WeaponSelect){
    //Weapons GUI
    //Layer
    draw_sprite_ext(sBlack,0,camera_get_view_x(cam)-view_w,camera_get_view_y(cam)-view_h,1,1,0,c_white,0.7)
    // Circle Part
    draw_circle_color(oGunHolder.x,oGunHolder.y,6,c_red,c_red,false)

    //Lines and Boxes
    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x+64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x+64,oGunHolder.y-64,oGunHolder.x+100,oGunHolder.y-32,box3,box3,box3,box3,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x,oGunHolder.y-64,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-18,oGunHolder.y-96,oGunHolder.x+18,oGunHolder.y-64,box2,box2,box2,box2,true)

    draw_line_color(oGunHolder.x,oGunHolder.y,oGunHolder.x-64,oGunHolder.y-48,c_red,c_red)
    draw_rectangle_color(oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32,box1,box1,box1,box1,true)

    //added this
    if(point_in_rectangle(mouse_x,mouse_y,oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32)){
       box1 = c_blue
       show_message("It's inside the rectangle");
       game_end()
    }

    //Actual line
    draw_line_color(oGunHolder.x,oGunHolder.y,mouse_x,mouse_y,c_white,c_white)
}
I mean, the gun is always moving unless the weapon switching menu is brought up. So if WeaponSelect is true it'll stop moving.

if you draw on the GUI, you have to leave out adding camera_get_x and y to your drawing coordinates as these are automatically added.
Though it should be avoided to draw any game objects as these will follow the camera and draw infront of everything else.

I just see now that in your point_in_rectangle function you switched the arguments.
The arguments for the left and top border (x1 and y1) should always be smaller than the right and bottom border (x2 and y2).
I just tried it and it does infact not work if they are switched.
When I draw it on the GUI it's no longer correctly positioned. The rectangles that are drawn stem out of the center of the gun and set to move with the Gun's x and y, but when I switch it to GUI it is no longer in the correct place. I thought the arguments were smaller, unless I'm really not thinking straight here.
 
W

Wadlo

Guest
if(point_in_rectangle(mouse_x,mouse_y,oGunHolder.x-96,oGunHolder.y-64,oGunHolder.x-60,oGunHolder.y-32))
 
R

Rhatos

Guest
if(point_in_rectangle(mouse_x,mouse_y,oGunHolder.x-96,oGunHolder.y-64,oGunHolder.x-60,oGunHolder.y-32))
Wow, that worked perfectly. Could you explain to me why that worked? Since I would think that since in the draw event it's set like this:
Code:
draw_rectangle_color(oGunHolder.x-60,oGunHolder.y-64,oGunHolder.x-96,oGunHolder.y-32,box1,box1,box1,box1,true)
That the position would have to be the same?
 
W

Wadlo

Guest
It's like @Simon Gust said, you need the least (most negative in this case) x value and the least y value first.
oGunHolder.x-96 is less than oGunHolder.x-60.

The fact that the order has to be such is a bit confusing, and in my opinion, should be changed by the gamemaker team. (Changing it would probably make the function call a tiny bit slower, but it would be worth it to save us the confusion.) Maybe somebody will see this suggestion.

Well, I'm glad you figured it out. Good luck with your game!
 
R

Rhatos

Guest
It's like @Simon Gust said, you need the least (most negative in this case) x value and the least y value first.
oGunHolder.x-96 is less than oGunHolder.x-60.

The fact that the order has to be such is a bit confusing, and in my opinion, should be changed by the gamemaker team. (Changing it would probably make the function call a tiny bit slower, but it would be worth it to save us the confusion.) Maybe somebody will see this suggestion.

Well, I'm glad you figured it out. Good luck with your game!
Thanks for the help!
 
R

Rhatos

Guest
Uhh okay, final question. How can I check with this now, if a line is going through the rectangle. Since I just realised that with this method it works fine if the cursor is within the rectangle, but the weapon switching is using a line to select the weapons.
 
Top