Page 1 of 1

Spring Query

Posted: Wed May 05, 2010 4:03 am
by xibit
Morning Chipmunkers

Firstly, I just wanted to say how impressed I am with Chipmunk. Eventhough I am new to physics engines I am found my introduction quite rewarding thus far.

I currently have a ball that I want to be pulled towards and held at another point. I currently have a collision callback that creates a damped spring between the ball and the sensor shape. The collision call back works well however the spring seems to push away from the sensor point rather than away. I assume this is due to the wrong parameters in my cpApplyDampedSpring call. Could someone show me an example of a spring call that would pull 2 points together?

Not sure what other info you guys would need if any to let me know and I will post it if I know it :-P

There is a good chance that I have completely miss understood the purpose on a Damped Spring so I am happy to receive suggestions of alternates.

Thanks in advance

Re: Spring Query

Posted: Wed May 05, 2010 7:38 am
by mallyone
Would you mind posted a snippet of your code?

One thing that comes to mind is your restLength in the spring might be set longer than you want, giving the illusion of push?

m...

Re: Spring Query

Posted: Wed May 05, 2010 8:47 am
by slembcke
If you want to pull two points together, the distance between the points has to be greater than the rest length. Presumably you just want to use a rest length of 0.

Re: Spring Query

Posted: Wed May 05, 2010 9:27 am
by xibit
I have my rest lenght set to 0, but it still seems to just push the ball to the edge of the screen. I am sure it is just something I don't understand with the other figures.

I will have to guess the code I have at the minute, I will post it from my source after work if I am wrong.

a and b are the shapes involved in the colision. a is the ball and b doesn't move. I have written the x and y values to NSLog and they are correct. (they weren't for ages but I fixed that :D )

Code: Select all

cpApplyDampedSpring(a->body, b->body, cpv(a->body->p.x, a->body.p.y), cpv(b->body->p.x, b->body.p.y), 0.0f, 100.0f, 0.5f, dt);
Does that help?

Re: Spring Query

Posted: Wed May 05, 2010 9:55 am
by slembcke
That looks fine. You said that you are only guessing about what the code is. I'm guessing that you have some parameters flipped or something.

Also, cpv(a->body->p.x, a->body.p.y) is equivalent to just a->body->p. You don't need to copy the vector like that.

Re: Spring Query

Posted: Wed May 05, 2010 5:08 pm
by xibit
Okay, this is my collision callback with the new improved anchor points thanks to Slembcke

Code: Select all

void pullToStatic(cpArbiter *arb, cpSpace *space, void *unused) {

	cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b); 

	int steps = 10;
	cpFloat dt = 1.0/60.0/(cpFloat)steps;
	
	cpApplyDampedSpring(a->body, b->body, a->body->p, b->body->p, 0.0f, 100.0f, 20.0f, dt);
	
	cpFloat dist = cpvdist(a->body->p, b->body->p);
	
	NSLog(@"%f x %f", a->body->p.x, a->body->p.y);
	NSLog(@"%f x %f", b->body->p.x, b->body->p.y);
	NSLog(@"Dist = %f", dist);
		
}
I added in a cpvdist to check the distance was greater than the test length and it is.

However this still pushed the ball away from the static body. I have messed about with the numbers but nothing seems to pull the ball to the position I want (anchr2)

Any thoughts?

Re: Spring Query

Posted: Wed May 05, 2010 6:23 pm
by slembcke
Oh! Crap, and I looked right at it to give you the advice about generating the anchor points. :p

The anchor points are relative, so you are using anchor points that are as far away from the bodies as the bodies are from (0, 0). You want to be using cpvzero for both anchor points.

Re: Spring Query

Posted: Thu May 06, 2010 1:35 am
by xibit
Cool, cpvzero gives me what I wanted. Thanks

Out of interest though I still got a strange effect when using cpApplyDampedSpring but when I used ....

Code: Select all

cpConstraint *spring = cpDampedSpringNew(a->body, b->body, cpvzero, cpvzero, 0.0f, 50.0f, 20.0f);
cpSpaceAddConstraint(space, spring);
.... instead it worked just fine

Now I am stuck on removing the spring when the ball is no longer touching, I has assumed I could get to the constraint using a->body->constraints but no such look :-(

Re: Spring Query

Posted: Thu May 06, 2010 10:27 am
by slembcke
Chipmunk doesn't really track one-to-many relationships. While it could, I usually find it's much better to organize that in the game code instead.