How to rotate cpSegmentShapeNew type of body @60 degree when

Official forum for the Chipmunk2D Physics Library.
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Hi,

I am facing one problem. I have done some coding to rotate cpSegmentShapeNew but its not working . Have a look on the following code,

Code: Select all

//**creating shape
testBody = cpBodyNew(INFINITY, INFINITY);
cpShape* testShape = cpSegmentShapeNew(testBody, cpv(230, 82), cpv(193, 46), 0.0f);
testShape->e = 0.0;
testShape->u = 0.0;
testShape->data = flipper;
testShape->collision_type = 2;
cpSpaceAddStaticShape(space, testShape);

//Body moving when user touch
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	//event that starts when a finger touchs the screen
	UITouch *touch = [touches anyObject];
    CGPoint tmpLoc = [touch locationInView: [touch view]];
	CGPoint location = [[Director sharedDirector] convertCoordinate:tmpLoc];

	ball.position = location;
	ballBody->p = location;
	[flipper runAction:[RotateTo actionWithDuration:0.1f angle:60]];

	cpBodySetAngle(testBody, 60);

	cpvrotate(testBody->rot, cpv(100000,0));

	return kEventHandled;
}
Please anyone tell me that where i am wrong.

Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by slembcke »

What exactly do you expect this to do?

Code: Select all

cpBodySetAngle(testBody, 60);
cpvrotate(testBody->rot, cpv(100000,0));
First of all, Chipmunk always uses radians for angles to greatly simplify internal calculations. 60 radians is about 15 full revolutions, probably not what you wanted. After that you are setting the cached rotation vector so that it doesn't match the angle of the current body. Not sure what you were going for there. You only want to set the angle.

Also, don't expect the physics to work if you suddenly change the angle or position of a body like that. You have to also make sure that the velocity matches, and you'll want to do it slower over several frames.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Thanks slembcke.

Actually i am confused that what to do so i was trying to put some code.
Actually i want to set angle and rotate this body with sprite. When user touch, it rotate at 60 degree and when user cancel the touch, it go to the original position. so how can i set angle and match with velocity?. Actually i tried to do using body->a = setAngle... but it gives me error.

There is no any example also there i can see this body rotation.

Thanks for reply and seek another reply.

Thanks.
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Hello please give me point that what should i give for body->w and how can i rotate this body?.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by slembcke »

You could do something like this every frame:

Code: Select all

body->w = 0.1f;
cpBodySetAngle(body, body->a + body->w*dt);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Hi slembcke,

Thanks for reply.

I don't want to do every time. When user touch the screen, it will rotate for 60 degree and when user cancel the touch event, it will rotate and again set t original position. So i think that i can not use dt.

Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by slembcke »

Make it rotate in one direction when they are touching the screen and the other when they aren't. Stop it's rotation when it gets to the limits.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Hi Slembcke,

Thanks for your reply.
Yes your are right but following code is not working for me. First thing i have to give in touch begin method so i can not use dt (Timer), please give some more specific code, I tried to use following code, but it is not rotating,

Code: Select all

body->w = 0.1f;
cpBodySetAngle(body, body->a + body->w*dt);
Thanks,
iPhone Dev.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by slembcke »

You should seriously consider using a fixed size timestep anyway. It will produce a much more stable simulation, and you always know what to use as the timestep.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
iphonemobdev
Posts: 10
Joined: Tue Aug 04, 2009 12:38 am
Contact:

Re: How to rotate cpSegmentShapeNew type of body @60 degree when

Post by iphonemobdev »

Hi Slembcke,

Thanks for giving me reply again and again.

Yes you are right that i should use timestep. I tried get dt from timer method and put in touch method. But i faced one problem is body->w. If i put this body->w then another body sprite also get affected from this body->w.

I have segment shape with two point, (200, 80) (280, 120). So first point should rotate and second point should stay at same point and you told me do this "Make it rotate in one direction when they are touching the screen and the other when they aren't. Stop it's rotation when it gets to the limits." But how can i do by coding?. Please give some clues in coding.

Today i have to solve this issue. I am really stuck with this problem and there is no any sample code to do this.

Please give me reply so i can solve it today it self.

Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests