GML How to create collision shape for a line that the player is drawing?

A

André Guilherme

Guest
Hi everyone!

I'm making a game using physics in which the player can draw a line that functions as the ground.
I can already draw the line, but I'm having trouble setting up its collision shape, and the ball never collides with it! (see image)

Here's my code!
When the player holds the left mouse button an obj_drawing_line is created.
Inside it, we have:

paint = true; //while the player is painting this variable is true
lines_xy[0,0] = x; //in this array we'll save the drawing points 'x' in the column 0
lines_xy[0,1] = y; //and the drawing points 'y' in the column 1
line_nr = 0; //Number of total coordinates

fix[0] = noone;
my_fixture = noone;

checking_interval = 1; // The window of frames between coordinates check

//Check if player has stopped painting
if (obj_player.painting == false)
{
paint = false;
}

//If painting sets off the alarm according to the chosen interval of frames between checks
if (paint)
{
if (alarm[0] < 0)
{
alarm[0] = checking_interval;
}
}

//Add to the total coordinates value and save mouse 'x' and 'y' to the array
line_nr += 1;
lines_xy[line_nr,0] = mouse_x;
lines_xy[line_nr,1] = mouse_y;

//Create Fixture

physics_remove_fixture(id, my_fixture);

for (i=0; i < line_nr; i++)
{
fix = physics_fixture_create();

var xx = lines_xy[i,0];
var yy = lines_xy[i,1];
var xx2 = lines_xy[i+1,0];
var yy2 = lines_xy[i+1,1];
physics_fixture_set_edge_shape(fix, xx, yy, xx2, yy2);
physics_fixture_set_density(fix, 0);
physics_fixture_set_restitution(fix, 0.1);
physics_fixture_set_collision_group(fix, 0);
physics_fixture_set_linear_damping(fix, 0);
physics_fixture_set_angular_damping(fix, 0);
physics_fixture_set_friction(fix, 0.1);
physics_fixture_set_sensor(fix, false);
physics_fixture_set_awake(fix, true);
my_fixture = physics_fixture_bind(fix, id);
physics_fixture_delete(fix);
}


draw_set_color(c_black);

for (i = 0; i < array_height_2d(lines_xy); i++)
{
var xx = lines_xy[i,0];
var yy = lines_xy[i,1];

if (i+1 < array_height_2d(lines_xy))
{
var xx2 = lines_xy[i+1,0];
var yy2 = lines_xy[i+1,1];

draw_line(xx, yy, xx2, yy2);
}
}

This object has no sprite, might that have something to do with it?
I don't know... so, help would be much appreciated!

Thank you in advance!
 

Attachments

Jezla

Member
The coordinates for the points on the edge shape are relative to the instance. That is, the function treats the instance's position as 0,0. Your fixture is not being created where you expect it to be. Try subtracting the instance's x and y values from the x,y values of the edge points.

Also, using the physics debug drawing functions is a must when working with physics, it allows you to see directly what is happening, rather than having to guess based on behaviors.
 
A

André Guilherme

Guest
Ahoy there!
Well, just as I was coming in to answer to Karlstens, Jezla flew by with the right answer.

All I did after finding out what the problem was was to set in the create event of the object «x=0; y=0;»
 
A

André Guilherme

Guest
the chain fixture......
Wow, after all the frustration and web searching I did at the time I had this doubt I find it amazingly and ironically hilarious that I didn't found out about that function - which does exactly what I needed!
ahah, well that's life!
 

Jezla

Member
Be aware, that the physics_fixture_add_point() function that you will use with the chain fixture has the same issue as the edge shape. The coordinates in the function arguments are relative to the origin of the instance, even though the manual states that for a chain fixture they are actual room coordinates. Either the manual is in error or the function is bugged.

It shouldn't be a problem for you since you're re-locating your instance to 0, 0, but I thought I would warn you in case you start seeing the same problem again.
 
Top