[Solved] Precise collision checking with sprite collision mask

M

Mike2

Guest
Hi, I'm having problems with precise collision checking with a sprite generated from a surface. I created a circle for collision and an object to collide with. The collision only works as if it was a square, not a circle.


Object A:

Create Event
Code:
    spr = 0
    surfaces = noone;


Draw Event

Code:
    if( surface_exists(surfaces) ){
       draw_surface(surfaces,x,y)
    }else{
        surfaces = surface_create(300,600)
        surface_set_target( surfaces )
        draw_clear_alpha(c_white,1)
        draw_circle(100,100,100,0)
        spr = sprite_create_from_surface(surfaces, 0, 0, 600, 600, true, true, 0, 0);
        mask_index = spr;
        surface_reset_target()
    }

Object B:

Step Event

Code:
    if( place_meeting(x, y, ObjectA) ){
        x = 2   
    }
 

CloseRange

Member
What you want is:
sprite_create_from_surface
you will also want to go into the delete event and delete the sprite and the surface (or you will get memory leaks)
Code:
/// create event
spr = 0;
surfaces = noone;
sprite = noone;
Code:
/// delete event
if(sprite != noone) sprite_delete(sprite);
if(surface != noone) surface_free(surface);
Code:
/// draw event
    if( surface_exists(surfaces) ){
        draw_sprite(x, y, sprite);
    }else{
        surfaces = surface_create(300,600)
        surface_set_target( surfaces )
        draw_clear_alpha(c_white,1)
        draw_circle(100,100,100,0)
        spr = sprite_create_from_surface(surfaces, 0, 0, 600, 600, true, true, 0, 0);
        mask_index = spr;
        surface_reset_target()

       if(sprite == noone) {
           sprite = sprite_create_from_surface(surfaces, 0, 0, surface_get_width(surfaces), surface_get_height(surfaces), false, false, 0,0);
       }
    }
 
M

Mike2

Guest
The collision works now but it seems that Object B is colliding with a bounding box around the circle.
I've put a line now to collide with to see if it works, and the the same. Is there a way to make a pixel perfect collision?


Code:
    if( surface_exists(surfaces) ){
        draw_sprite(sprite,sprite,x, y);
    }else{
        surfaces = surface_create(1000,1000)
        surface_set_target( surfaces )
        draw_clear_alpha(c_white,0)
        draw_line(100, 100, 200, 200);
        surface_reset_target()

       if(sprite == noone) {
            sprite = sprite_create_from_surface(surfaces, 0, 0, surface_get_width(surfaces), surface_get_height(surfaces), false, false, 0,0);
            mask_index = sprite;
       }
    }
 

Slyddar

Member
The collision works now but it seems that Object B is colliding with a bounding box around the circle.
I've put a line now to collide with to see if it works, and the the same. Is there a way to make a pixel perfect collision?
When the sprite is created from the surface it doesn't differentiate the background and your circle, it just makes a rectangular sprite. The mask will therefore always be rectangular when it generates the sprite.

You might be able to use sprite_collision_mask to set a mask after it's been created though. If you know the size of the circle, you could also set a mask sprite up before hand, and then assign it with mask_index, but you would still need to set the kind to circle with sprite_collision_mask.
 
Is there a way to make a pixel perfect collision?
You might be able to use sprite_collision_mask to set a mask
Definitely use sprite_collision_mask() function if you want to use pixel perfect collisions with the sprite you are creating.

Just check the docs to see how it is used, and use the parameter "bboxkind_precise" as the kind of collision type you want.
 
Top