How to maintain a certain angle between to joints

Official forum for the Chipmunk2D Physics Library.
Post Reply
Norberg
Posts: 2
Joined: Wed Dec 25, 2013 10:22 am
Contact:

How to maintain a certain angle between to joints

Post by Norberg »

Hi,

Im trying to simulate a molecule with chipmunk. I currently do it by letting each atom have its own body + shape and connect the atoms with a SlideJoint to create a molecule where the lenght of the bonds can be increased/decreased depending on the forces in the simulation. This works fine but now i would like to take the simulation to the next level and also maintain the correct angle between bonds instead of letting them move as they whish but i cant figure out how to do that with chipmunk. I would also prefer the angle to be somewhat flexible like a spring and not completly fixed. I have tried to take a look at the constraints that works on angles(especially the DampedRotarySpring) but i cant figure out if any of those will do what i need.
I dont request code to do this, its enough if you can help me to figure out what constraint i should use.
I have also attached a screenshot of a simulated water molecule to better visualize what i try to do. Its the angle between the to Hydrogen molecule that i want to set to always be around 104 degree.
Attachments
h20.png
h20.png (5.01 KiB) Viewed 4267 times
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: How to maintain a certain angle between to joints

Post by viblo »

Its hard to say exactly which one is best, why not try each one (DampedRotarySpring, RotaryLimitJoint, GearJoint) and see which one gives you the best behavior? For the non spring constraints you can try to experiment with error_bias, max_bias and max_force. But I think its a good guess that the DampedRorarySpring will work for your case as long as you figure out some good spring values.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
Norberg
Posts: 2
Joined: Wed Dec 25, 2013 10:22 am
Contact:

Re: How to maintain a certain angle between to joints

Post by Norberg »

I have know tested all the constraints that works on angles without any luck. The good thing is that i now think i know why they don't work ;) If i have understood this correctly the angle limiting constraints like DampedRotarySpring only work on the rotation/angle of a body. And what i tried to do was not to limiting the rotation of a body but instead limiting the angle between two constraints. But not all is bad, when testing i also happend to test a GrooveJoint instead of the PinJoint i used before and it appears to solve my issue just fine. With the GrooveJoint i can specify a line relative to the oxygen atom where the hydrogen atoms are allowed to be, and by creating the lines with some trigonometry im able to maintain a certain angle between the bonds. I further plan to adjust the max_force and max_bias to get some spring like behavior of the angle.

I wrote a simple test program(python) to test the constraints if someone is interested: https://gist.github.com/Norberg/c0ab09b754f5825644ae
- Adjust the commented code depending on what to test, currently configured to test GrooveJoint
Also noticed a bug in in pymunk when a GroveJoint is rotated: the debug drawing is not taking the rotation into account, i will provide a fix on github.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests