GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2013-07-25 21:20:22

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Fluid and dynamic collisions

Hi I've been working for quite a while on a game called zuwaka which is a fast paced platformer racing game. The game features nice and smooth physics.

In this little engine, I am trying to remake it from scratch to make it more efficient and more complex.

You can download it here : http://www.2shared.com/file/0puMaIxF/Zuwaka.html

- In the engine, place lines by clicking, holding and releasing the right mouse button;
- Then create a dynamic ball by using you left mouse button. You can input different starting speed and direction by clicking and dragging the mouse button.

So far, everything is working according to plan but one thing I have a hard time figuring out. So far, the ball is following the lines through its origin. What I would like to do is replace the ball where it collides on its edge.

Does anyone have an idea?

Offline

#2 2013-07-26 08:27:59

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Fluid and dynamic collisions

Sorry, I dont download from sites that require me to install a downloader. I just spent 2 hours last night trying to remove a toolbar (that kept coming back) and fix the damages to my browser settings that kept getting overwritten by spyware.

Offline

#3 2013-07-26 08:29:48

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Fluid and dynamic collisions

Weird. I didn't have to download anything special nor did I even notice an option to.

The way this is typically handled is dead-simple. All you need to do is push your collision line outward along its normal by the radius of the ball.

Event: obj_ball.Step

Expand    if ( scr_line_intersect( 
        other.x, 
        other.y, 
        other.x + other.hspd,
        other.y + other.vspd, 
        xstart + other.radius * segmentNormalX, 
        ystart + other.radius * segmentNormalY, 
        x + other.radius * segmentNormalX, 
        y + other.radius * segmentNormalY ) ) {

I've obviously added "radius" to the obj_ball.Create event and draw the ball with it instead of the literal "5".

If you need the ball to collide with the back-side of a line, you need to test which side the ball is on and reverse the normal if it is on the back-side.

Nice work, so far. The simple physics work surprisingly well.


Abusing forum power since 1986.

Offline

#4 2013-07-26 09:27:05

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Re: Fluid and dynamic collisions

The site shouldn't require you to download an installer but I'll check it out to make sure.

As for the solution, it is dead-simple indeed. Funny, I tried doing just that but I was only replacing the ball instead of checking the collision on the edge of itself...

Thanks for the heads up though.

I just added a grapnel system to the engine and it is working very well, I will post it later tonight.

EDIT : icuurd12b42, you probably clicked the wrong download button. I must say, it is not very clear though. Any uploading site suggestions?

Last edited by Marchal_Mig12 (2013-07-26 09:30:54)

Offline

#5 2013-07-26 10:38:07

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Fluid and dynamic collisions

After I posted that it occurred to me that it will not work well for segments that meet at an angle greater than 180 degrees. I spent a good while working on different solutions with varying degrees of success before asking myself, why am I doing this when GM:Studio has a perfectly robust physics system built into it?


Abusing forum power since 1986.

Offline

#6 2013-07-26 10:56:14

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Re: Fluid and dynamic collisions

That is so true! Never thought about that! I'll give it a try smile

Offline

#7 2013-07-27 08:30:36

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Fluid and dynamic collisions

I used point_line_distance with segment true. and the normal is point_direction(x0,y0,x,y). setting on the line is siply x,y = x0,y0+lenghtdir_x/y(ball_radius,normal) if point_distance(x0,y0,x,y) is smaller than radius of the ball

this will handle both sides of the line and the tips. and will work for convex shapes as the collision usually resolves itself naturally in a single pass when the ball is in contact with 2 lines during you iteration process (a process which you can delegate to GM's collision event system) for concave shapes, you need to iterate through the shape lines multiple times until their is no more conflict, AKA a line pushes the ball in another line, that other line pushe back the ball inside the first line... For this case the multiple pass probably need to move the ball gradually outside collision so each line can push the ball outside in concert.
x0,y0+lenghtdir_x/y(1/*ball_radius*/,normal)

Last edited by icuurd12b42 (2013-07-27 08:32:01)

Offline

#8 2013-07-27 10:34:32

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Fluid and dynamic collisions

I was working on a similar point_line_distance() solution but soon ran into problems with multiple collisions. I started working of a time rewinding collision resolver when I remembered the existence of Box2D and stopped the madness.


Abusing forum power since 1986.

Offline

#9 2013-07-27 15:15:43

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Re: Fluid and dynamic collisions

point_line_distance is a good way to handle that, true.

Offline

#10 2013-07-28 22:31:06

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Fluid and dynamic collisions

If you have studio, use the physics engine and the madness will stop. I have a bunch of methods I used to do this sort of thing and you'll eventually have to start to do progressive scans especially when the ball is traveling faster than 1/4 if it's radius... and at that point you are better off learning the physics engine and be done with it. There is a flag for fast moving object in studio's engine if I remember.

Offline

#11 2013-07-29 10:21:26

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Re: Fluid and dynamic collisions

Well I've done it before with Zuwaka and it is not TOO hard. I tried the built-in physics from studio and found myself stuck where it could not offer a few things I need. Also, it seems very unstable. Sometimes, the physics collision mask would reset (don't know why yet) or the gravity would change for no reason...

Offline

#12 2013-07-29 20:08:33

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Fluid and dynamic collisions

I was making this a while back

http://host-a.net/u/icuurd12b42/PiinballTutorial.05.gmk . the flippers are a little nasty but the rest is pretty stable

Offline

#13 2013-07-29 21:27:28

Marchal_Mig12
Member
Registered: 2009-05-21
Posts: 75

Re: Fluid and dynamic collisions

I have a few problems with my gm8, seems like I don't have pro anymore... and can't up it sleep...

Offline

#14 2013-07-30 08:09:03

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Fluid and dynamic collisions

It's probably importable in studio.

Offline

Board footer

Powered by FluxBB