Computer Science Canada

Rigid Body and Cloth Physics

Author:  zero-impact [ Fri May 22, 2009 6:46 pm ]
Post subject:  Rigid Body and Cloth Physics

This is a system based off of verlet integration.
Simulates cloth and rigid bodys without collision detection so far.
Based off of http://www.teknikus.dk/tj/gdc2001.htm.

Run clothsystem.t to start.
Tell me what you think Very Happy

P.S. I'm not sure how I should go about collision detection. I've heard separating axis theorem http://en.wikipedia.org/wiki/Separating_hyperplane_theorem, any other ideas?

Author:  Geniis [ Fri May 22, 2009 10:02 pm ]
Post subject:  Re: Rigid Body and Cloth Physics

Thats pretty sweet. The cloth is pretty realistic. Good job.

Author:  A.J [ Sat May 23, 2009 10:51 am ]
Post subject:  RE:Rigid Body and Cloth Physics

Very Impressive. I like the cloth. You used verlet integration very effectively.

Author:  zero-impact [ Sat May 23, 2009 4:08 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Thanks A.J.

Author:  corriep [ Sat May 23, 2009 9:02 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Your gonna have to teach me rigid body dynamics some time, I can't find a decent guide on the internet Sad

PS. Can't wait to see how it works w/ mouse and collisions!

Author:  zero-impact [ Sat May 23, 2009 9:05 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Corriep, my reference is included in my first post.

Author:  zero-impact [ Sat May 23, 2009 10:58 pm ]
Post subject:  Re: Rigid Body and Cloth Physics

[Update]

I have added constraints that allow you to connect vertices to arbitrary points. This of course allows you to interact with the shapes via the mouse.

Here is a demo with the solids on hinges and the cloth connected to your mouse position.
If you look at the comments on the main loop you can see how to try it out on different objects.

Tell me what you think Very Happy

Author:  saltpro15 [ Sun May 24, 2009 7:57 am ]
Post subject:  RE:Rigid Body and Cloth Physics

that is so friggin cool Very Happy

time to start reading that reference!

Author:  zero-impact [ Sun May 24, 2009 8:52 am ]
Post subject:  RE:Rigid Body and Cloth Physics

I have a few problems and I'm wondering if anyone has any idea's.
Right now my scale is effectively 1pixel == 1m and each particle has a mass of 1g. How could I change it so that the scale is more like 500pixel == 1m?

This brings me to my second problem, since the scale is so large, I have to use a relativly large timestep for it too look right (0.3s) when in reality the time between frames is 0.03s. Using such a large timestep seems to "break" cloth and especially softbodies. However if I set my timestep to 0.03 or lower they work perfectly fine and look really neat, if you want to watch it in ultra slow-mo that is.

So yea, basically if I fix the scale that will allow me to fix the timestep thing.

Sorry if that doesn't make sense at all.

Author:  CodeMonkey2000 [ Sun May 24, 2009 11:57 am ]
Post subject:  Re: Rigid Body and Cloth Physics

You have memory leaks all over the place.

Turing:

fcn add (vec2 : ^Vector2) : ^Vector2
        var temp : ^Vector2
        new Vector2, temp
        temp -> initialize (x + vec2 -> getx, y + vec2 -> gety)
        result temp
end add


Everytime functions like add are called you allocate memory for new pointers, but old useless memory is never freed, so your program keeps using up memory. If you kept the program running for a few minutes, it will crash (or at least windows will force it to stop execution). You should go back and fix all the functions with pointers.

Author:  andrew. [ Sun May 24, 2009 1:29 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Pretty damn cool. Never really expected something that advanced to be done with Turing.

Author:  zero-impact [ Sun May 24, 2009 2:45 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Thanks a bunch CodeMonkey. I had no idea about that.
I'm not sure what you mean by fixing it with pointers though?

Author:  CodeMonkey2000 [ Sun May 24, 2009 5:18 pm ]
Post subject:  Re: Rigid Body and Cloth Physics

I meant to say fix all the functions that return a pointer, my bad.

Let's closely analyze what's going on.

Turing:

fcn add (vec2 : ^Vector2) : ^Vector2
        var temp : ^Vector2
        new Vector2, temp
        temp -> initialize (x + vec2 -> getx, y + vec2 -> gety)
        result temp
end add


So if I do

Turing:

     foo := foo->add(bar)


What's really going on here? You initialized memory for foo. Then when you called add, add allocates more memory, and foo now points to what add allocated. The previous memory location that foo was pointing to is still there, you never freed it. To fix this you need to somehow free the previous memory foo was pointing to. This can easily be fixed if turing could do operator overloading and had , and just overloading the "=" operator. There are a few ways of fixing this.

One is to change your functions (that return a pointer) directly like so:
Turing:

     proc add (vec2 : ^Vector2)
        x=vec2->getx
        y=vec2-gety
    end add


So you instead do:
Turing:

     foo->add(bar)


Or if you don't want to change the functions:

Turing:

     var holder:^Vector2
     holder=foo->add(bar)
     free Vector2,foo
     foo=holder


I have to go now, so if there is anything you want me to clear up, let me know.

Author:  zero-impact [ Sun May 24, 2009 6:36 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Changing the functions wont work very well because of things like this
code:
newPos := (pos -> multiply (2) -> subtract (pos_) ->
            add (accel -> multiply (timestep * timestep)))

Author:  CodeMonkey2000 [ Sun May 24, 2009 6:39 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Hmm, then my last example is your only choice, but it will be a pain.

Author:  zero-impact [ Sun May 24, 2009 7:09 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Yes it is, I have already tried. After monitoring the memory usage for 5 minutes I see that it climbs to about 2Gb before crashing Wine.
I think I will recode it in c++ using SDL and use it as an opprotunity to learn OOP in c++.

Thanks for the help.
You can consider that my final version.

Author:  CodeMonkey2000 [ Sun May 24, 2009 7:42 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

In that case, look up the bresenham's line blitting algorithm and the flood fill algorithm. You will find that very useful.

Author:  zero-impact [ Sun May 24, 2009 7:57 pm ]
Post subject:  RE:Rigid Body and Cloth Physics

Thanks, however I think I might just use the primitives in SDL_gfx for simplicity's sake for the time being. Thanks for the tip though Smile

Author:  DanTheMan [ Tue May 26, 2009 8:29 am ]
Post subject:  RE:Rigid Body and Cloth Physics

I like it a lot! I am a big fan of rigid cloth physics ;P

Buut... The cloth isn't interacting with the shapes... is it supposed to be like that?

Author:  saltpro15 [ Wed May 27, 2009 7:54 am ]
Post subject:  RE:Rigid Body and Cloth Physics

nice work lawson, it crashed a school computer in <30 seconds :p

edit : you've sparked my interest in learning how to use verlet integration. Now if only these school computer's had a C++ compiler and SDL Sad

edit edit : so now I'm coding in Wordpad, and secretly downloading a compiler to a flash drive :p
sdl can wait for now

Author:  zero-impact [ Thu May 28, 2009 4:28 pm ]
Post subject:  Re: RE:Rigid Body and Cloth Physics

DanTheMan @ Tue May 26, 2009 8:29 am wrote:
I like it a lot! I am a big fan of rigid cloth physics ;P

Buut... The cloth isn't interacting with the shapes... is it supposed to be like that?


Yes, I do not have any "inter-shape" collision detection at the moment. I don't think I'm going to either because I am recoding this in c++. I will post a link when I ahve made any progres. However with school that could mean a month from now.


: