Page 1 of 2
How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 12:59 am
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 2:16 am
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 2:28 am
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 5:53 am
by iphonemobdev
Hello please give me point that what should i give for body->w and how can i rotate this body?.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 12:14 pm
by slembcke
You could do something like this every frame:
Code: Select all
body->w = 0.1f;
cpBodySetAngle(body, body->a + body->w*dt);
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Thu Aug 06, 2009 11:37 pm
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Fri Aug 07, 2009 9:36 am
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Sat Aug 08, 2009 12:40 am
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Sat Aug 08, 2009 4:31 pm
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.
Re: How to rotate cpSegmentShapeNew type of body @60 degree when
Posted: Sun Aug 09, 2009 11:29 pm
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.