1
\$\begingroup\$

I was wondering if any of you could help me with a specific Gamemaker question. Am using Gamemaker Studio.

I have an object (oSpecialSolid) which rotates and moves across my level. At random intervals, another object (oEmitter, an animated spark or flash of smoke) spawns somewhere within the x and y of oSpecialSolid, give or take 45px.

Unfortunately, oSpecialSolid has some transparent areas (the sprite is not perfectly rectangular, and has empty space at either side) where I would not like the oEmitter to spawn. However, as the object spins, I cannot tell if the x and y values that I am telling it to spawn in will be transparent or not in that moment. I would also like the oEmitter to spawn anywhere on the oSpecialSolid object,not just in a 45 x 45px box in its centre.

What I am looking for is a method of telling oEmitter to only spawn within the collision box of oSpecialSolid. Is there a way to do this? I am using the precise settings for collision.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Firstly, you need find bounds of your object

// top left point
var a = point_direction(0, 0, -sprite_xoffset, -sprite_yoffset) + image_angle;
var d = point_distance(0, 0, -sprite_xoffset, -sprite_yoffset);
var px1 = lengthdir_x(d, a);
var py1 = lengthdir_y(d, a);

// top right point
a = point_direction(0, 0, sprite_width - sprite_xoffset - 1, -sprite_yoffset) + image_angle;
d = point_distance(0, 0, sprite_width - sprite_xoffset - 1, -sprite_yoffset);
var px2 = lengthdir_x(d, a);
var py2 = lengthdir_y(d, a);

// bottom right point
a = point_direction(0, 0, sprite_width - sprite_xoffset - 1, sprite_height - sprite_yoffset - 1) + image_angle;
d = point_distance(0, 0, sprite_width - sprite_xoffset - 1, sprite_height - sprite_yoffset - 1);
var px3 = lengthdir_x(d, a);
var py3 = lengthdir_y(d, a);

// bottom left point
a = point_direction(0, 0, -sprite_xoffset, sprite_height - sprite_yoffset - 1) + image_angle;
d = point_distance(0, 0, -sprite_xoffset, sprite_height - sprite_yoffset - 1);
var px4 = lengthdir_x(d, a);
var py4 = lengthdir_y(d, a);

// calulate bounds
var left = x + min(px1, px2, px3, px4);
var top = y + min(py1, py2, py3, py4);
var right = x + max(px1, px2, px3, px4);
var bottom = y + max(py1, py2, py3, py4);

Now you can take any random position in this area

var posx = irandom_range(left, right);
var posy = irandom_range(top, bottom);

and check precise collision with spawn object using position_meeting()

if position_meeting(posx, posy, obj_spawn)
{
    // The point have collsion with a sprite. Spawn
}

and you need using loop here, like

repeat 100
{
    var posx = irandom_range(left, right);
    var posy = irandom_range(top, bottom);

    if position_meeting(posx, posy, obj_spawn)
    {
        // The point have collsion with a sprite. Spawn
        break;
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Hello @Dmi7ry, this did work! Thanks! \$\endgroup\$ Oct 17, 2015 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .