Straight Joints With Particles

I've begun working out how to attach two particles together by a set distance, but I'm having trouble. I worked out this method by hand and it works in theory, but in practice the results aren't the best. One of my particles is zero mass so it's stationary. The other should just swing around the bottom of it. Instead, it swings to the bottom and stops. It's odd. I'm thinking I need to be applying forces to keep them together instead of setting the position, but I'm not sure how to calculate that. Here's the code I have right now. Any suggestions?

Yes, your problem is that you need to change the velocity, not the position. You can do that by applying forces or impulses. Impulses are simpler because they are instantaneous and don't rely on the time step. Unless you are going to do something really fancy, I'd go the impulse route instead of penalty forces.

To find the correct impulse, j, you would need to use this equation:
j = -(v2 - v1)/(1/m1 + 1/m2)
and apply this to your particles:
v1 += jâ€¢n/m1
v2 += jâ€¢n/m2
where â€¢ is the dot product, and n is the normalized direction from obj2 to obj1.
(NOTE: I probably have some signs flipped, proceed with caution.)

EDIT: Oh, forgot to mention, this will only solve the velocity constraint. You will still need to satisfy the position constraint like you are already doing or the objects will slowly drift apart due to the discrete time steps.

For constrained particle physics, Jakobsen physics is king. It's super simple, fast, and needs no explicit velocity representation. If you aren't interested in doing rigid body simulation, I'd go this route. (You can use this method for rigid body physics as well, but it doesn't work particularly well.)

What you really want is to change the position, not the velocity. Changing the velocity is only viable for collisions, not constraints. Really, read that Jakobsen article, it's good. And when you're done with that, look at "Constrained Rigid Body Dynamics" by Chris Hecker.

DoG, I'm pretty sure that that's not true. You need to do both. If you don't change the velocity, the object they will get pushed back together, but their velocities con continue to be divergent. This was the problem Nick was having, despite being attached to a solid particle, the second one was continuing to accelerate downward. You need to make their relative velocity in the direction of the constraint zero.

I'm back with another (hopefully easier) question. I'm trying to solve for my constraint using two different sized masses. I want it to behave realistically so that larger masses pull smaller masses in rather than the other way around. This (I assumed) was more related to the positioning of the masses rather than adjusting the velocities. Here is what I have right now:

The important part is in the first if statement where I use the opposite particle's mass to determine the percentage of the difference that particle has to move. This made logical sense, but I'm not sure if it's correct. As of now, my simulation doesn't seem to take mass into account very well (even very large masses get swung around like they were nothing and if I set all particles to a smaller mass on my chain, they move faster).

EDIT: To make things easier for people to help, I've uploaded the whole project for download. Xcode 2 required. I've included the two SDL frameworks I'm using as well. Download Here (2.4 MB ). The files of interest are easily identifiable, with Chain being the top level class. Chain is comprised of StickConstraint which is a subclass of Joint. Joint contains two Particles. You can look at main.cpp to see how I'm using it if it helps.

So my problem turned out to be that I was using a constant gravity force rather than constant gravity acceleration. I fixed that and now have yet another question. I've created a way that I can stick the static end of my rope to my mouse and drag it around, but it doesn't let me throw the rope around it like it should. I've tried setting the velocity manually, but I'm not sure I'm doing it right. Here's the code I'm using:

My first guess is that the problem stems from the fact that the mouse input is really coarse. You only receive a mouse event every few frames, so your end particle is moving really fast for one frame, and then comes to a dead halt for the next few frames.

I'll try and take a closer look later, but try that first.

EDIT:
Yeah, I think that pretty much hit it on the nose. Comment out the line where you set the particle velocity. It will have almost no effect on the simulation. Because the mouse velocity is so sporadic, it will have almost zero effect on the velocities of the particles in the string. The mouse position on the other hand will still have a lot of effect on the positions of the string particles. What you are seeing is the velocity getting out of sync with the positions. That's why if you whip the string around like crazy it will soon settle back to the same swinging motion it had before.

This is one of the reasons why Jakobsen style physics rocks for these kinds of simulations. There is no explicit velocity state, so it can't get out of sync with the positions. Smoothing the mouse velocities will help (probably alot), but you really want to consider implementing Jakobsen physics to do this sort of thing. The downsides of Jakobsen physics are that because there is no velocity state, it makes it difficult to implement friction, restitution, or interactions with objects that do use a velocity state.

Well, I had a nice long answer that I was tying up, but it seems that I accidentally quit the browser.

Anyway, the problem is with how you are applying the velocity to the attached particle. You only get a mouse motion event every few frames. This means that you are changing the velocity from 0 to a large value and then back to 0 for a few frames. The velocities of the other particles in the rope never have a chance to converge. Instead they will converge around to 0.

The position on the other hand isn't so jumpy. It moves somewhere and stays there for a while. This allows the positions of the attached particles to converge on their new intended positions over several frames.

The effect of this then is the same problem that you were having before when you were modifying the positions, but not the velocities. The swinging motion of the rope is mostly residual velocity from however the rope was swinging before you moved the mouse. If you whip the string around, it will very quickly settle into the same motion it had before. Although it's following the position of the mouse, it isn't picking up the mouse's velocity.

I've never implemented anything quite in the way you are, but I can think of a few suggestions to try out anyway:
1) Smooth out the mouse deltas over several frames. This will allow the velocities of the particles in the rope a chance to converge. (easy)
2) Use Jakobsen style physics. This would be very easy, fast, and would completely remove the position/velocity synchronization problem, but can make other advanced features more difficult.
3) When constraining the positions, use the the correction for the positions and also apply that to the velocities (scaled by the time step of course). You might even be able to remove the explicit velocity constraint this way. This seems like it would be similar to how the vertlet integration used in Jakobsen physics works, but I'm not really sure. (Maybe that's what DoG meant perhaps?)
4) Use a different time stepping algorithm. (update velocities, constrain velocities, update positions, constrain positions)