(cocos2d-iphone) Problem-Sprite rotates 180 degree to and fro - iphone

hi i have a problem with sprite during startup
The problem is that the sprite rotates 180 degree to and fro once before setting to its position.
It happens very fast.
I know it is some bug but couldnt find the problem.
Can anyone please help me.
The code are as follows
CCSprite *arrowSprite = [[CCSprite spriteWithFile:#"Launcher2.png"] retain];
CGSize size = [arrowSprite contentSize];
arrowSprite.anchorPoint = ccp(25/size.width,20/size.height);
NSLog(#"Value of anchorPoint x,y %f, %f",arrowSprite.anchorPoint.x,arrowSprite.anchorPoint.y);
arrowSprite.position = ccp(65, 80);
[self addChild:arrowSprite z:1];

I don't see anything in your code that would cause any rotation of the sprite. You can confirm this by reducing the posted code down to the bare bones by commenting out the line that sets the anchorPoint. If you're still getting the rotation, it's happening somewhere else in your application.

Related

Rotating UIImageView Moves the Image Off Screen

I have a simple rotation gesture implemented in my code, but the problem is when I rotate the image it goes off the screen/out of the view always to the right.
The image view that is being rotated center X gets off or increases (hence it going right off the screen out of the view).
I would like it to rotate around the current center, but it's changing for some reason. Any ideas what is causing this?
Code Below:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *l = [self.viewCase layer];
[l setMasksToBounds:YES];
[l setCornerRadius:30.0];
self.imgUserPhoto.userInteractionEnabled = YES;
[self.imgUserPhoto setClipsToBounds:NO];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
rotationRecognizer.delegate = self;
}
- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer
{
CGFloat angle = rotationRecognizer.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle);
rotationRecognizer.rotation = 0.0;
}
You want to rotate the image around it's center, but that's not what it is actually happening. Rotation transforms take place around the origin. So what you have to do is to apply a translate transform first to map the origin to the center of the image, and then apply the rotation transform, like so:
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);
Please note that after rotating you'll probably have to undo the translate transform in order to correctly draw the image.
Hope this helps
Edit:
To quickly answer your question, what you have to do to undo the Translate Transform is to subtract the same difference you add to it in the first place, for example:
// The next line will add a translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 10, 10);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, radians);
// The next line will undo the translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -10, -10);
However, after creating this quick project I realized that when you apply a rotation transform using UIKit (like the way you're apparently doing it) the rotation actually takes place around the center. It is only when using CoreGraphics that the rotation happens around the origin. So now I'm not sure why your image goes off the screen. Anyway, take a look at the project and see if any code there helps you.
Let me know if you have any more questions.
The 'Firefox' image is drawn using UIKit. The blue rect is drawn using CoreGraphics
You aren't rotating the image around its centre. You'll need correct this manually by translating it back to the correct position

How to make two object collide in cocos 2d?

if (CGRectIntersectsRect(projectileRect, targetRects))
{
NSLog(#"ha ha Collision detected");
}
CGRect projectileRect = [player boundingBox];
CGRect targetRects = [anEnemy boundingBox];
This is the code i am using for collision.The problem is many time the collision get not checked.If it get checked the position of collision is bottom left or right.
PLease advise what to do or any sample of how to do collision.ALso i am not using box or chimpunk
Make sure to check if the object has passed through the object as well. Ray has some great tuts on all of this here

Changing the Bounding box for UIImageview When Rotating

I have an image that i rotate to face the touchLocation of the user, but because the bounding box of the UIImageView gets larger. This is ruining some collision detection.
My only plan is to code a new bounding box system to get each of the 4 points in a bounding box and rotate that myself, then write check collide code for that.
But before i do that, is there an easy way to do this?
My rotate code:
- (void)ObjectPointAtTouch{
//Get the angle
objectAngle = [self findAngleToPoint:Object :touchLocation];
//Convert to radian, +90 for image alignment
double radian = (90 + objectAngle)/(180 / M_PI);
//Transform by radian
Object.transform = CGAffineTransformMakeRotation(radian);
}
I figured it out, as seen in other tutorials they also resize the scale off the bouncing box to fix this problem. CGAffineTransform scale I think it is.

iphone image not rotating properly aacording to location upon camera view

i am developing application which point towards the particular location, i have calculate angle between current coordinate to specific coordinate and my image is rotating by that difference but image rotation is fixed means when i change my direction the image is not moved it remain fix to specific postion, i want that the image should be moved aacording to the actual position where it is located(where we want to reach) thanks for the help in advance below is my code..
-(float) angleToRadians:(float) a {
return ((a/180)*M_PI);
}
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = [self angleToRadians:fromLoc.latitude];
float fLng = [self angleToRadians:fromLoc.longitude];
float tLat = [self angleToRadians:toLoc.latitude];
float tLng = [self angleToRadians:toLoc.longitude];
float result= atan2f(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
return RadiansToDegrees(result);
}
- (void) scanButtonTouchUpInside {
UIImage *overlayGraphic = [UIImage imageNamed:#"GFI.jpg"];
overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(50, 50, 50, 80);
[self addSubview:overlayGraphicView];
float angle =[self getHeadingForDirectionFromCoordinate:currentlocation toCoordinate:pointlocation];
if(angle < 0.0)
angle += 2*M_PI;
overlayGraphicView.transform=CGAffineTransformMakeRotation(angle);
}
Judging by the name of the method that is rotating the image: scanButtonTouchUpInside, I'm guessing that it is called only when a user hits a button. If it is only called once, I would expect the image to only rotate once, then remain fixed. In order to keep the image rotating, you need to put that code somewhere that is called repeatedly. A (cheap hack) way to test this would be to add this line to the end of your - (void)scanButtonTouchUpInside method:
[self performSelector:#selector(scanButtonTouchUpInside) withObject:nil afterDelay:0.25f];
This will make the method call fire repeatedly. One thing to note though: It seems that your calculation of the angle of rotation depends on a variable called currentLocation. This solution will only work if you are updating currentLocation appropriately.
EDIT:
Just a thought, you're currently calculating the angle of rotation based on the location of the device and the location of the destination. You're not taking into account compass bearing at all. Is this intentional? With this approach, your angle will never change unless the device actually moves. Any change in orientation will be ignored.

Problem with smoke bubble in cocos2d

I am having a problem. When the smokeMoveBy action starts a small smoke bubble is spotted on the screen at other place then the moving path of the smoke.
This is only happening when I am using scaleX and scaleY.
The method smokeLoop is being called at each 1 seconds in the scheduler.
Here self is a layer.
Any solutions?
My code follows,
CGPoint dummyPosition=ccp(600, 600);
ParticleSystem *smoke = [ParticleSmoke node];
ccColor4F startColor;
startColor.r = 1.f;
startColor.g = 1.f;
startColor.b = 1.f;
startColor.a = 1.f;
[smoke setStartColor:startColor];
ccColor4F endColor;
endColor.r = 0.8f;
endColor.g = 0.8f;
endColor.b = 0.8f;
endColor.a = 1.0f;
[smoke setEndColor:endColor];
[smoke setLife:0.1f];
[smoke setScaleX:0.1f];
[smoke setScaleY:0.2f];
[smoke setStartSize:30.f];
[self addChild:smoke z:2];
[smoke setPosition:dummyPosition];
-(void)smokeLoop{
id smokeMoveBy = [MoveBy actionWithDuration:durTime position:ccp(0.f, (-1.f*480))]];
id smokeSeq=[Sequence actions:[Place actionWithPosition:smokeInitPosition], smokeMoveBy, nil];
[smoke runAction:smokeSeq];
}
Not sure if this is your issue, but I had an issue with Cocos2D, scaling and moving, which I solved with moving the anchorPoint.
What I wanted to do is zoom (scale) and move a layer. Zooming would move fine if position was {0,0} and transform point was {0.5,0.5}. But then if I'd move it, it would still transform around {0.5,0.5}, which might by then be out of screen, so it would scale really weird.
Solution was to move the transform point to the middle of the screen, every time I moved the layer's position. This was not an easy formula for me to fix, as when I moved the transform point, the scale operation would have a new center point.
The formula I ended up using was the following:
layer = self.foreground;
ccpAdd(
ccpDivide(
ccpNeg(layer.position),
(CGPoint){layer.contentSize.width, layer.contentSize.height}),
(CGPoint){0.5f,0.5f}
);
Basically: Divide the inverse of the layers position (meaning, {300,200} would become {-300,-200}) by the size of the layer {480,320}, and then add {0.5,0.5} (as I want my anchor to be always center + offset)
Anyway, you may need to work out a completely different formula, but this worked for me. I had to apply this to my anchor point every time I moved the layer.
Good luck, hope this helps!