Windows triangle wall collision

P

personwhoisarealguy

Guest
I'm try to figure out how to do collision checking between a triangle shaped object,thats constantly changing its image angle(image_angle).Do you have any useful code I could use (studio 1.4).I'm useing h&vspeed for movement(If that matters).
 

poliver

Member
does image angle affect collision mask?

you can use precise collision mask.

or set up collisions with collision_point() at individual vertices
 
Last edited:

CMAllen

Member
Triangle is not a collision shape that GM sprites natively support, so to do a triangle shape, you'd have to do some point_in_triangle() checks. For it to work, you'll need to pre-calculate the relative position of the 3 vertices from the origin point, modify them by the object's image_angle value, and add them to the object's position for the 3 coordinate pairs used by the function. You can use a precise collision check, but that's going to have serious performance impact.

However, if you're using the physics engine, you can do 3-point collision shapes.
 
P

personwhoisarealguy

Guest
What im looking for is just a script that stops the triangle from going through a 32 by 32 square.Got anything useful.
 

poliver

Member
just to check on vertices:
Code:
if (collision_point(bbox_left, bbox_top, obj_wall, false, false) ||
   collision_point(bbox_left, bbox_bottom, obj_wall, false, false) ||
   collision_point(bbox_right, bbox_top + sprite_height/2, obj_wall, false, false))
to check on edges:
Code:
if (collision_line(bbox_left, bbox_top, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_bottom, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_top, bbox_left, bbox_bottom, obj_wall, false, false))
you can also check the whole area as CMAllen suggested

needs to be ran from objects you're colliding with/not the triangle
Code:
point_in_triangle(x, y, triangle.bbox_left, triangle.bbox_top, triangle.bbox_left, triangle.bbox_bottom, triangle.bbox_right, triangle.bbox_top, triangle.bbox_top + sprite_height/2);
edit:
oh wait, the sprit_height/2 bit is not quite right, need to plugin the angle there
 
Last edited:

CMAllen

Member
just to check on vertices:
Code:
if (collision_point(bbox_left, bbox_top, obj_wall, false, false) ||
   collision_point(bbox_left, bbox_bottom, obj_wall, false, false) ||
   collision_point(bbox_right, bbox_top + sprite_height/2, obj_wall, false, false))
to check on edges:
Code:
if (collision_line(bbox_left, bbox_top, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_bottom, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_top, bbox_left, bbox_bottom, obj_wall, false, false))
I like that idea better. It's pretty simple and straightforward.
 

poliver

Member
got bad feeling think that the
Code:
+
bit might stop working when you start rotating
tryin to figure it out atm but not too good with trig.
 
P

personwhoisarealguy

Guest
just to check on vertices:
Code:
if (collision_point(bbox_left, bbox_top, obj_wall, false, false) ||
   collision_point(bbox_left, bbox_bottom, obj_wall, false, false) ||
   collision_point(bbox_right, bbox_top + sprite_height/2, obj_wall, false, false))
to check on edges:
Code:
if (collision_line(bbox_left, bbox_top, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_bottom, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
   collision_line(bbox_left, bbox_top, bbox_left, bbox_bottom, obj_wall, false, false))
you can also check the whole area as CMAllen suggested

needs to be ran from objects you're colliding with/not the triangle
Code:
point_in_triangle(x, y, triangle.bbox_left, triangle.bbox_top, triangle.bbox_left, triangle.bbox_bottom, triangle.bbox_right, triangle.bbox_top, triangle.bbox_top + sprite_height/2);
edit:
oh wait, the sprit_height/2 bit is not quite right, need to plugin the angle there
im getting a compile error with this line,
if (collision_line(bbox_left, bbox_top, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
collision_line(bbox_left, bbox_bottom, bbox_right, bbox_top + sprite_height/2, obj_wall, false, false) ||
collision_line(bbox_left, bbox_top, bbox_left, bbox_bottom, obj_wall, false, false))

Do you know what to put for the y3 varible.
 
Top