# Collision with path

8 replies to this topic

### #1 CGPM

CGPM

GMC Member

• GMC Member
• 51 posts
• Version:GM8

Posted 21 June 2012 - 11:34 PM

How can you check if an instance is colliding with a path that is drawn on the screen? I'm not sure if this is the right topic to put this in.

Edited by CGPM, 21 June 2012 - 11:53 PM.

• 0

### #2 eyeCube

eyeCube

GMC Member

• GMC Member
• 206 posts

Posted 22 June 2012 - 05:50 AM

Are you using paths?
Are you simply drawing the path?
Is the path an object?

It is a complex path, right? Not just a line or two? Because if it's only a line or two, you could simply use collision_line(). If it's more complex, but not too large, you could use a repeat() with collision_point(), but that's not a very good solution.

• 0

### #3 ramses12

ramses12

6

• GMC Member
• 5769 posts
• Version:GM8.1

Posted 22 June 2012 - 08:57 AM

If it is straight, a collision_line loop won't hurt.
Otherwise, you'll probably need to do the math to make a collision_arc function.
• 0

### #4 zehevi

zehevi

GMC Member

• GMC Member
• 637 posts
• Version:GM8

Posted 22 June 2012 - 12:54 PM

The most percise, yet slow, way to achieve this would be

drawing the path into a surface
creating a sprite from that surface using sprite_create_from_surface()
assigning the sprite into an object (sprite_index = xxx)
collision checking with that instance.

Take note that this is the slowest method to use every step, but if you are creating the path only once then it might be the fastest method.
• 1

### #5 CGPM

CGPM

GMC Member

• GMC Member
• 51 posts
• Version:GM8

Posted 22 June 2012 - 01:08 PM

I little about the path: its 5000 by 600 (longer than tall), smooth with 8 presision, and has at least 20 points. I think I'll use zehevi's technique unless anyone has something better.

EDIT: Okay, that's what I'm doing.

Edited by CGPM, 23 June 2012 - 01:44 AM.

• 0

### #6 CGPM

CGPM

GMC Member

• GMC Member
• 51 posts
• Version:GM8

Posted 23 June 2012 - 01:16 PM

Does anyone see anything wrong with this code? Because apparently Game Maker does.

```temp1=surface_create(room_width,room_height)
surface_set_target(temp1)
draw_clear_alpha(0,0)
draw_set_color(c_yellow)
draw_path(line1,0,0,true) temp2=sprite_create_from_surface(temp1,0,0,surface_get_width(temp1),surface_get_height(temp1),true,false,0,0)
sprite_index=temp2
surface_free(temp1)```

Edited by CGPM, 24 June 2012 - 07:34 PM.

• 0

### #7 ramses12

ramses12

6

• GMC Member
• 5769 posts
• Version:GM8.1

Posted 23 June 2012 - 02:19 PM

GM doesn't handle sprites too good. And a room_width / room_height sprite is enormous.
Plus, you forgot to add draw_clear_alpha(0,0); after setting the drawing target to the surface.
• 0

### #8 CGPM

CGPM

GMC Member

• GMC Member
• 51 posts
• Version:GM8

Posted 24 June 2012 - 04:03 PM

I think I have an idea. Instead of one giant 5000 by 600 sprite, I'll divide it into 5 different sprites, using a while loop and surface_get_width(temp1)/5.

EDIT: Wait, that means creating 5 objects per room, and collision checking with each. I can't do that.

Edited by CGPM, 24 June 2012 - 06:32 PM.

• 0

### #9 zehevi

zehevi

GMC Member

• GMC Member
• 637 posts
• Version:GM8

Posted 01 July 2012 - 05:20 PM

```temp1=surface_create(room_width,room_height);
surface_set_target(temp1);
draw_clear_alpha(0,0);
draw_set_color(c_yellow);
draw_path(line1,0,0,true) ;
surface_reset_target();
temp2=sprite_create_from_surface(temp1,0,0,surface_get_width(temp1),surface_get_height(temp1),0,false,0,0);
sprite_index=temp2;surface_free(temp1);```

You shouldn't destroy a surface you are using, it might cause some serious problems.
The reason it wouldn't work is because you set 'remove back' to true.
Remove back cannot recognize alpha channel and in most cases will remove all the image.

The code above should work.

P.s.
You really should consider splitting the image into ,at least, 3 because most graphic cards won't support such large surface.
As for your collision problem, you should create a parent for those instances/objects and drop your collision code in collision event with that parent, (this way you will have a collision with 1 object instead of 3).
Another thing, do not use a while loop for such cases, while loop is dangerous as it might loop forever. Use a repeat loop or a for loop

Good luck,
Zehevi.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users