How to prevent a motor from moving?

Official forum for the Chipmunk2D Physics Library.
Post Reply
theforestfloor
Posts: 7
Joined: Tue Jun 01, 2010 10:49 am
Contact:

How to prevent a motor from moving?

Post by theforestfloor »

I'm trying to attach a simple motor to a box shape, but the motor moves about due to the movement. I've been trying to come up with ways to make the motor's position static, while allowing it to freely rotate as a motor but have been unsuccessful.

I have a circle with a simple motor contraint attached to the staticBody.
I then attached a pin joint off-center of the circle to a box

The affect will be like a wheel of a train. I did a quick google search for an image example and found this:
Image

I'm doing something wrong, but can't put my finger on it. Any advice?
Thanks in advance! :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to prevent a motor from moving?

Post by slembcke »

cpSimpleMotor applies torque but doess not control positions. If you want an object controlled by a motor to stay still you have to attach a pivot joint as well.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
theforestfloor
Posts: 7
Joined: Tue Jun 01, 2010 10:49 am
Contact:

Re: How to prevent a motor from moving?

Post by theforestfloor »

Au, thank you! I've connected the motor via a pivot joint to the static body at the center of the motor. This has had the desired affect of keeping the location of the motor static.

It appears that the pin joint which connects the motor to the box isn't constraining the length of the distance between the two bodies of the pin joint, so over time the box drifts away, or around. How would I address this?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to prevent a motor from moving?

Post by slembcke »

I think I would need to see a video or some example code and screenshots to understand what the problem is. I'm guessing that the pin joint is working as intended, but may be hooked up wrong or need a second joint to do what you want.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
theforestfloor
Posts: 7
Joined: Tue Jun 01, 2010 10:49 am
Contact:

Re: How to prevent a motor from moving?

Post by theforestfloor »

Sure, lets paste in a bit of code. :) There's some additional code for the motor and the wave box if you are interested. It is in those classes that the body and shape are constructed for each object. I can also get a series of screenshots showing the behavior of the movement over time.

Code: Select all

			page = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"768x1024_simple_background.png"]];
			page.center = parentView.center;
			page.hidden = YES;
			[page setUserInteractionEnabled:YES];
			
			actors = [[NSMutableSet alloc] init];
			landscape = [[NSMutableSet alloc] init];
			actorSpace = [[ChipmunkSpace alloc] init];
			landscapeSpace = [[ChipmunkSpace alloc] init];
			staticBody = [ChipmunkBody staticBody];
			
			// build waves
			NSLog(@"Building waves");
			newStormWavesView = [[StormWavesView alloc] initWithSpriteImage:@"wave1.png"];
			newStormWavesView.shape.group = @"waves";
			[landscape addObject:newStormWavesView];
			[landscapeSpace add:newStormWavesView];
			[page addSubview:newStormWavesView];
			
			MotorView *newMotorView = nil;
			newMotorView = [[MotorView alloc]
								initWithSpriteImage:
									@"58x58 face.png" 
									Location:cpv(576, 100)
							];
			newMotorView.shape.group = @"waves";
			[landscape addObject:newMotorView];
			[landscapeSpace add:newMotorView];
			[page addSubview:newMotorView];
			
			// turn our motor into a motor ..
			[landscapeSpace add:[ChipmunkSimpleMotor simpleMotorWithBodyA:newMotorView.body bodyB:staticBody rate:2]];
			
			// keep the motor in place
			[landscapeSpace add:[ChipmunkPivotJoint pivotJointWithBodyA:newMotorView.body bodyB:staticBody pivot:cpv(576, 100)]];

			// now create a pin joint
			[landscapeSpace addConstraint:[ChipmunkPinJoint pinJointWithBodyA:newMotorView.body bodyB:newStormWavesView.body anchr1:cpv(29, 0) anchr2:cpv(576, 120)]];
			
Thanks again for all the assistance!
theforestfloor
Posts: 7
Joined: Tue Jun 01, 2010 10:49 am
Contact:

Re: How to prevent a motor from moving?

Post by theforestfloor »

I took a few screenshots over a short period of time of the current behavior. I've stuck them into a gif. :)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to prevent a motor from moving?

Post by slembcke »

Where are you connecting the joint to the water part? It looks like the right side of it is probably staying the same distance. Is the problem that you don't want any rotation or other movement? You'll need more joints if that's what you want. If you don't want the shape to ever rotate, you can set it's moment of inertia to infinity when you create the body. If you want it's position to stay more or less the same, you can connect it to a slide joint so it can only move up and down.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
theforestfloor
Posts: 7
Joined: Tue Jun 01, 2010 10:49 am
Contact:

Re: How to prevent a motor from moving?

Post by theforestfloor »

It looks like a few different things were required to get the desired affect. Thank you for the help!

I did indeed need to set the moment of inertia for the box to INFINITY. This prevented the box from rotating about the motor, but caused it to slowly see-saw off in the direction of the turning motor. So I created a second motor which was pinned to the box as the first was, but the pin on the motor was placed on the opposite side to the first motor. This appears to have had the effect of preventing the box from slowly drifting off in the direction of the spinning motor (canceling out this slow movement perhaps?). I also pinned both motors to the center of the box rather than at the same x,y position on the screen as the pins on the motors.

The wave nows appears to rock back-and-forth, but stays in place. I don't know if this is the best way to approach this problem, but would welcome any additional insight anyone can provide.

Cheers!
Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests