Rotating Sprite with Touch - Cocos2d - iphone

Does anyone have a better way to rotate a sprite with one finger? My problem is that I can not get the sprite to stop rotating after it has been fully rotated twice and I flip my screen 180 degree periodcally (self.rotation = 180;) then flip it back (self.rotation = 0). But, when I flip it to 180 degrees the sprite will not rotate properly.
Any one have any better ideas than this?
CGFloat gRotation;
- (void)update:(ccTime)delta
{
g.rotation = gRotation;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (CGRectContainsPoint(g.boundingBox, location))
{
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, g.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, g.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
gRotation += currentTouch - previousTouch;
}
}
Thanks
EDIT:
I went in to GameConfig.h and changed #define GAME_AUTOROTATION kGameAutorotationUIViewController to #define GAME_AUTOROTATION kGameAutorotationNone
Then, went in to AppDelegate.m and changed #if GAME_AUTOROTATION == kGameAutorotationUIViewController to #if GAME_AUTOROTATION == kGameAutorotationNone
That fixed the sprite's rotation when I flipped the screen, but I am still having problems stopping the sprite's rotation after two full rotations.

add a new line at the end:
gRotation += currentTouch - previousTouch;
gRotation = fmod(gRotation,360.0); // <<< fix the angle
this maybe fix your problem, because the angle will stay in the 360 range
Other way to rotate with one finger is the UIPanGestureRecognizer (but you still need to keep the angle in the 360 range):
UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:#selector(handlePanFrom:)] autorelease];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer];
...
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
...
}
Take a look at this tutorial for more details (its on drag, but show how to do the pan gesture):
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d

Related

Image rotation is slow on touches moved

I have written a program that will rotate my imageView as i want (CW or ~CW).
And it is working fine.
but it is responding very slowly, I want it to respond quickly...
can you please help me out of it?
Thanks in advance...!
Following is my code------------------------
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [[event allTouches]anyObject];
CGPoint prevLoc = [touch previousLocationInView:self];
CGPoint loc = [touch locationInView:self];
if (isRotating) {
float prevAngle = atan2(prevLoc.y-self.center.y,
prevLoc.x-self.center.x);
float newAngle = atan2( loc.y-self.center.y,
loc.x-self.center.x);
float finalAngle =angle+(newAngle-prevAngle);
//Apply new transform
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(Angle);
self.transform=rotateTransform;
angle = finalAngle;
}
}

box2d impulse on release

i wanted to apply impulse when i release my finger but however i couldn't get it to work. the object body will not re-act to my input. so here's my code for release.
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint != nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
float distanceX = location.x-170;
float distanceY =location.y-270;
float distance = sqrtf(distanceX*distanceX+distanceY*distanceY);
CGFloat angle = atan2f(distanceY,distanceX);
// Apply an impulse to the body, using the angle
tailBody->ApplyLinearImpulse(b2Vec2(-distance*cosf(angle)/4+100,-distance*sinf(angle)/4+100), tailBody->GetPosition());
if (mouseJoint) {
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
is there something missing from the code?

Rotating a sprite with Touch - Cocos2d

I know this question has been asked several times, trust me I have searched. I have found one answer to rotating a sprite with touch, but there has to be a simpler way.
All I need is for my sprite to rotate with my touch. Max rotation 0 Minimum rotation 0.
I know that I'll need a few checks.
int maxRot = 0;
int minRot = 0;
if (arrowRotation > maxRot)
{
//do or don't do something
} else if (arrowRotation < minRot)
{
//do or don't do something
}
Can someone lead me in the right direction to rotating a sprite with touch, with a minimun and maximum rotation?
Here is the code that I think is to complicated or can be accomplished in a simpler way.
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
}
Thanks in advance!
I dont think you can simplify the rotation into anything simpler. I honestly didnt get how you want to both the minimum and the maximum rotations to be zero. They cant be same value. If you want to limit the rotation to a maximum and minimum add the following to the end of your ccTouchesMoved: method:
if (arrowRotation >= maxRot) {
arrowRotation = maxRot;
}
else if (arrowRotation <= minRot) {
arrowRotation = minRot;
}
use KTOneFingerRotationGestureRecognizer...
KTOneFingerRotationGestureRecognizer *rotation;
rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotating:)];
[[self Wheel] addGestureRecognizer:rotation];
[rotation release];
Method
- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer {
view = [recognizer view];
if(isRotating == FALSE) {
[view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])];
}
}

Apply Linear Impulse to Rotation of Sprite- Cocos2d/Box2D

I have an arrow and a ball. The anchorPoint for the arrow is on the bottom. All I need to do is have the sprite be able to rotate with touch, then a linear impulse is applied to the ball and shot in the direction the arrow is pointing to.
E.G.
Say you rotate the arrow 70 degrees then press the fire button. The ball is then shot in a 70 degree angle.
Currently, this is what I am using but the ball does not shoot in the direction the arrow is pointing too.
Rotates arrow with touch.
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
}
Fire Button to shoot the ball in the direction the arrow is pointing too.
-(void) fireBall: (id)sender
{
[self unscheduleUpdate];
direction = arrowRotation;
b2Vec2 force = b2Vec2(direction, 24.8);
_ballBody->ApplyLinearImpulse(force, _ballBody->GetWorldCenter());
}
Any help is greatly appreciated!
Thanks in advance!
The impulse you are applying to the ball is (direction, 24.8). Should it not be: (direction.x * 24.8, direction.y * 24.8). This may not be the only problem, but it's one that jumped out at me.

Moving CCLayer with "ccTouchesMoved" works but it needs some tweaks I can't figure it out

my code:
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint prevLocation = [touch previousLocationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
CGPoint diff = ccpSub(touchLocation,prevLocation);
[self setPosition: ccpAdd(self.position, diff)];
}
}
This code lets me move the layer with my fingers. That works fine. But now I want to let the user only move the layer within a predefined CGRect. How to do that?
For example:
CGRect rect = CGRectMake(0,0,600,320);
Now the player should be only allowed to move the layer within this rect. In that example he could only move it (on an ipod touch) to the left and right. (till 600px).
What do I need to change to achieve that?
Thank you for any help.
Have a nice day :)
Keep a variable to check.. eg..
distmoved = distmoved + touchlocation.x - prevlocation.x;
if(distmoved<600)
//move code
note that distmoved is a float..
You should make your own check. It's not hard in your case:
[self setPosition: ccpAdd(self.position, diff)];
if (self.position.x < minX)
{
//correct x position
}
if (...)
// ...
// ans so on