CCRotateTo and CCRotateBy differnce - iphone

//CCRotateBy
id action=[CCRotateBy actionWithDuration:1.0 angle:45];
[player runAction:action];
//CCRotateTo
id action=[CCRotateTo actionWithDuration:1.0 angle:45];
[player runAction:action];
the above two code produce same results...I need to know the difference between using rotateTo and rotateBy...
please advise...

CCRotateTo rotates the object to the specified angle, while CCRotateBy rotates the object to its current angle + the specified angle. They would be equivalent if your object's initial rotation is 0. However, if its initial angle was 90, CCRotateTo would rotate it towards angle 45, while CCRotateBy would rotate it towards angle 135.

CCRotateBy is also very handy when it comes to ever rotating sprites:
CCSprite *halo = [CCSprite spriteWithFile:#"halo.png"];
[halo setOpacity:160];
CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.5 angle:40.0];
[halo runAction:[CCRepeatForever actionWithAction:rotRight]];
with the CCRotateBy action you never need to think about any angles...

Related

How to move the PhysicsSprite in box2d

I am having a physicsSprite of kinematics body type and I want to move the body and sprite bit down and back to its position. I tried it like this Inside update method:
for (NSValue *bodyValue in [self getGoalPostBodiesList])
{
b2Body *gPBody = (b2Body *)[bodyValue pointerValue];
PhysicsSprite *pSprite =(PhysicsSprite *) gPBody->GetUserData();
NSLog(#"\n tag value of Sprite = %d",pSprite.tag);
if(pSprite == goal1)
{
pSprite.position = CGPointMake((gPBody->GetPosition().x)*32.0,(gPBody->GetPosition().y)*32.0);
float angle = gPBody->GetAngle();
pSprite.rotation = -(CC_RADIANS_TO_DEGREES(angle));
id moveDownAction = [CCMoveTo actionWithDuration:0.5 position:CGPointMake(pSprite.position.x,(pSprite.position.y )- 40)];
id moveUpAction = [CCMoveTo actionWithDuration:0.5 position:CGPointMake(pSprite.position.x,(pSprite.position.y )+ 40)];
CCSequence *seqAction = [CCSequence actions:moveDownAction,moveUpAction, nil];
[pSprite runAction:seqAction];
b2Vec2 pos = b2Vec2(pSprite.position.x/32.0, pSprite.position.y/32.0);
gPBody->SetTransform(pos, CC_DEGREES_TO_RADIANS(pSprite.rotation));
gPBody->SetLinearVelocity(b2Vec2(0.0f, 0.0f));
gPBody->SetAngularVelocity(0.0f);
}
}
Still the sprite is not changing its position.
Anyone's help will be deeply appreciated.
Thanks all,
MONISH
To summarize your code, you update the position of your sprite to reflect that of the body, start an animation, and then update the position of the body to correspond to the position of the sprite. So naturally, nothing should move here, since your CCMoveTo actions have not exerted any effect on your sprite yet.
Second, your update method may be called very often, like dozens of times per second, so the animation gets reset continously and will not make any visible progress.
To follow a consistent pattern, how about you set the velocity of your kinematic bodies. Also, update the position of your sprites to correspond to these bodies as you would do for dynamic bodies, but don't set the transformation of your bodies to correspond to their sprites.

Rotate or flip the square image in cocos2d

Assume that one square image is for showing road. Now In my game I have to flip this square image on Y axis so that it looks like road and person can walk on that road. I want this type of rotation.
But I want the same rotation in cocos2d then how I have to do it?
Thanks for help
I don't completely understand what you want to do, but I'll do my best here :)
You could use the flipX or flipY properties of a CCSprite
E.X.
CCSprite *sprite = [CCSprite spriteWithFile:#"file.png"];
sprite.position = position;
sprite.flipX = YES;
[self addChild:sprite];
or you could use a rotate action as Anish said.
Hope that helped.
Rotating Turrets in Cocos2D
Try like this...
CCRotateBy *rot = [CCRotateBy actionWithDuration: 2 angle: 720];
[sprite runAction:rot];
CCSprite *sprite = [CCSprite spriteWithFile:#"file.png"];
sprite.rotation = 45;
I Flip CCSprite at Y axis like that in android...
CCSprite sprite=CCSprite.sprite("icon.png");
CCIntervalAction a = (CCIntervalAction)CCOrbitCamera.action(2, 1, 0, 0, 360, 0, 0);
addChild(sprite,1);
sprite.runAction(CCRepeatForever.action(a));

bounding a sprite in cocos2d

am making a canon to fire objects. back of the canon the plunger is attached. plunger acts for set speed and angle. canon rotates 0-90 degree and plunger moves front and back for adjust speed. when am rotates the canon by touches moved its working fine. when plunger is pull back by touches moved and it rotates means the plunger is bounds outside of the canon.
how to control this:-
my code for plunger and canon rotation on touches moved. ( para3 is the canon , para6 is my plunger):-
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
if (CGRectContainsPoint(CGRectMake(para6.position.x-para6.contentSize.width/2, para6.position.y-para6.contentSize.height/2, para6.contentSize.width, para6.contentSize.height), touchLocation) && (touchLocation.y-oldTouchLocation.y == 0))
{
CGPoint diff = ccpSub(touchLocation, oldTouchLocation);
CGPoint currentpos = [para6 position];
NSLog(#"%d",currentpos);
CGPoint destination = ccpAdd(currentpos, diff);
if (destination.x < 90 && destination.x >70)
{
[para6 setPosition:destination];
speed = (70 + (90-destination.x))*3.5 ;
}
}
if(CGRectIntersectsRect((CGRectMake(para6.position.x-para6.contentSize.width/8, (para6.position.y+30)-para6.contentSize.height/10, para6.contentSize.width, para6.contentSize.height/10)),(CGRectMake(para3.position.x-para3.contentSize.width/2, para3.position.y-para3.contentSize.height/2, para3.contentSize.width, para3.contentSize.height))))
{
[para3 runAction:[CCSequence actions:
[CCRotateTo actionWithDuration:rotateDuration angle:rotateDiff],
nil]];
CGFloat plungrot = (rotateDiff);
CCRotateTo *rot = [CCRotateTo actionWithDuration:rotateDuration angle:plungrot];
[para6 runAction:rot];
}
}
how about u do this that you use the [CCMoveTo actionWithDuration: position:] method??
Through this method you can easily control the speed by the "actionWithDuration" argument which takes integer values of time in seconds, while direction can be adjusted through "position" argument which takes ccp(x,y) as the values to the point you want your plunger to move to.
You can use it like this....
CCSprite *plunger = [[CCSprite alloc] initWithFile:#"plunger.png"];
plunger.position = ccp(240,240);
[self addChild:plunger z:10];
[plunger release];
id = [CCMoveTo actionWithDuration:3 position:ccp(300,240)];
The values given are of my choice. You may use them to your accordance.
Hope it helps you....
I hope i understood the question correctly:
if the problem is, that the cannon and plunger both rotate around their own center points, but you want them to rotate together, then the solution should be to make the plunger a child sprite of the cannon (this also makes the plugers position relative to the cannon) i.e.
[para3 addChild:para6]
then you only need to rotate the cannon and the plunger will be rotatet with it.
if i got your question totally wrong, maybe you could post a screenshot :-)

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.

CCSprite following a sequence of CGPoints

I'm developing a line drawing game, similar to Flight Control, Harbor Master, and others in the appstore, using Cocos2D.
For this game, I need a CCSprite to follow a line that the user has drawn. I'm storing a sequence of CGPoint structs in an NSArray, based on the points I get in the touchesBegin and touchesMoved messages. I now have the problem of how to make my sprite follow them.
I have a tick method, that is called at the framerate speed. In that tick method, based on the speed and current position of the sprite, I need to calculate its next position. Is there any standard way to achieve this?
My current approach is calculate the line between the last "reference point" and the next one, and calculate the next point in that line. The problem I have is when the sprite "turns" (moves from one segment of the line to another).
Any hint will be greatly appreciated.
Why do you code your own tick method? Why don't you just use the built-in CCMoveTo method?
(void) gotoNextWayPoint {
// You would need to code these functions:
CGPoint point1 = [self popCurrentWayPoint];
CGPoint point2 = [self getCurrentWayPoint];
// Calculate distance from last way point to next way point
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
float distance = sqrt( dx*dx + dy*dy );
// Calculate angle of segment
float angle = atan2(dy, dx);
// Rotate sprite to angle of next segment
// You could also do this as part of the sequence (or CCSpawn actually) below
// gradually as it approaches the next way point, but you would need the
// angle of the line between the next and next next way point
[mySprite setRotation: angle];
CCTime segmentDuration = distance/speed;
// Animate this segment, and afterward, call this function again
CCAction *myAction = [CCSequence actions:
[CCMoveTo actionWithDuration: segmentDuration position: nextWayPoint],
[CCCallFunc actionWithTarget: self selector: #selector(gotoNextWayPoint)],
nil];
[mySprite runAction: myAction];
}