Page 1 of 2

Line Segments + Rotating a Circle at Constant Speed

Posted: Tue May 05, 2009 3:56 pm
by zacaj
Ive got a few(hopefully simple questions).
1. How do I find the moment of inertia of a line segment?
2. Why dont line segments collide with each other?
3. How would I get a circle to roll at a constant speed(No accelleration)?

also:
with cpSpaceResizeActiveHash and cpSpaceResizeStaticHash
according to the moon demo, the first argument should be the average size of your objects, is this diameter, or radius? According to the moon demo, the second number is related to the number of objects you are putting, is this max or average number. In the docs, it says this should be around 10x the size, which isnt related to the number at all, so which one is right?

Thanks

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Tue May 05, 2009 5:09 pm
by maximile
1. I'd be interested to know this too.
2. It's a legacy from when Chipmunk didn't support line segments with thickness. I usually just add two circles and a rectangle to simulate it (and approximate the moment of inertia using a poly shape).
3. It will roll at a constant speed along a flat surface. Down a hill, you could just set the velocity every frame.
also: Diameter. According to the docs, the second argument should be about 10x the *number* of objects, not the size.

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Tue May 05, 2009 7:40 pm
by zacaj
3. How would you get it to roll? I tried setting the torque, but then it accelerated..

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 12:16 am
by Android_X
3. If you want it to rotate at a constant speed you could try: setting the angular velocity directly (body->w) and set the inertia to INFINITY so that collisions don't effect the rotation, eg cpBodySetMoment(body, INFINITY);

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 1:44 am
by slembcke
Undocumented, but exists in the trunk code. Copy paste away into your own project to use with 4.1.x

Code: Select all

cpFloat
cpMomentForSegment(cpFloat m, cpVect a, cpVect b)
{
        cpFloat length = cpvlength(cpvsub(b, a));
        cpVect offset = cpvmult(cpvadd(a, b), 1.0f/2.0f);
        
        return m*length*length/12.0f + m*cpvdot(offset, offset);
}
To get your constant rolling effect I'd go with what Android_X said. The infinite moment of inertia makes whatever angular velocity you set permanent. Because it's a circle, you don't have to worry about the infinite amount of torque it could produce because the only way the torque can escape is through friction.

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 3:43 pm
by zacaj
The rolling works great, thanks. But the line segment acts really wierd...

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 4:24 pm
by slembcke
Are you trying the same rolling behavior with a line segment? That might not work so well. It effectively has an infinite amount of torque driving it which might make it look stiff and unrealistic. It could also cause problems if something gets in the way of its turning as it would apply a nearly infinite amount of torque to push it out of the way.

It works well with circles because they only transfer that torque out by friction. The friction force is limited to the force pushing the two surfaces together multiplied by the frictional coefficient of the two surfaces so it can't be infinite.

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 7:17 pm
by zacaj
No, the line segments just a regular body, falling under gravity

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Wed May 06, 2009 11:43 pm
by slembcke
There is no collision detection function for checking line segments against other line segments if that is what you are asking.

Re: Line Segments + Rotating a Circle at Constant Speed

Posted: Thu May 07, 2009 8:24 am
by zacaj
No, its falling on a box