What does 'ccp' signify in Cocos2d/Objective-C? - objective-c

I keep seeing a phrase like this:
//Example one
CGPoint backgroundScrollVel = ccp(-1000, 0);
//Another Example
// 3) Determine relative movement speeds for space dust and background
CGPoint dustSpeed = ccp(0.1, 0.1);
CGPoint bgSpeed = ccp(0.05, 0.05);
So what does ccp signify? Is it a property of CCParallax?

Like Stephen said, it's just a macro for CGPointMake(x, y), but if you particularly mean what does "ccp" stand for it's most likely c o c os2d p oint

Are you using Cocos2D? If so, ccp is just a C macro to create a point. As in:
#define ccp(__X__,__Y__) CGPointMake(__X__,__Y__)

It's just a convenience constructor for the CGPoint type.
Pretty sure it's just a macro to CGPointMake, but don't quote me on that.

It's a shorthand macro that maps to CGPointMake(x, y).
Basically it's a way to create CGPoints with less typing.

Nope no difference except CGPointMake is harder to type:
#define ccp(__X__, __Y__) CGPointMake(__X__,__Y__)
Found here:
http://www.cocos2d-iphone.org/api-ref/0.99.3/_c_g_point_extension_8h_source.html

Related

How to make translation in cocos2D

and merry Christmas !
My question is about cocos2D, and how to do a translation in cocos2D. In "classic" objective C, I would have done :
myAnimation = [CABasicAnimation animationWithKeyPath:#"transform.translation.x"];
myAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
myAnimation.toValue = [NSNumber numberWithFloat:200.0f];
myAnimation.duration = t;
myAnimation.repeatCount = 1;
[myUIImageView.layer addAnimation:myAnimation forKey:#"myAnimation"];
But i don't find the equivalent when I tried to make an action :
id action = [CC… ?];
Thanks !
I think you may be looking for members of the CCAction class.
For example, CCMoveTo will move a CCNode from its current position to a new position over a period of time. CCMoveBy will move a CCNode by a certain amount (relative). etc.
See this reference (and search google for "Cocos2d CCAction", there are lots of references).
Was this helpful?
use a CCMoveTo (for displacement to an absolute x,y) or CCMoveBy for a relative movement from current position at the moment when the animation starts.

Using CGAffineTransformMakeRotation to rotate a point

If I want to rotate one CGPoint about another, I'm currently doing this (which works fine):
CGFloat rx = cos(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.x-rotationPoint.x) - sin(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.y-rotationPoint.y) + rotationPoint.x;
CGFloat ry = sin(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.x-rotationPoint.x) + cos(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.y-rotationPoint.y) + rotationPoint.y;
It strikes me that I should be able to do this with a CGAffineTransform, but I'm a bit stuck as to how it would work:
CGAffineTransform affine CGAffineTransformMakeRotation(M_PI/4);
CGPointApplyAffineTransform(positionToRotate, affine);
That does nothing as I'm (hopefully) missing something obvious :)
So how do you rotate a CGPoint about another without doing the matrix math myself?
Cheers,
Ian
CGPointApplyAffineTransform returns the transformed point. It doesn't mutate the CGPoint you pass in.
CGPoint transformedPoint = CGPointApplyAffineTransform(positionToRotate, affine);

CGRectMake alternative

I have seen this alternative to using CGRectMake() in order to initialise a CGRect variable:
CGRect frame = (CGRect){0,0,10,10};
My question is, how does CGRect frame = (CGRect){0,0,10,10}; work? What's going on behind the scenes? It looks like a c-style array is being initialised ({x,y,w,h}) which is then being cast as a CGRect struct - is this correct? If so, how is it possible to cast a c style array as a struct?
N.B. I am not asking if it is appropriate to use the above alternative to CGRectMake(), I only wish to understand why/how it works.
It's a so-called compound literal. You can read more about them in this article by Mike Ash: Friday Q&A 2011-02-18: Compound Literals.
U can use like this:
CGRect rect = CGRectFromString(#"{{0, 0}, {612, 892}}"); // it contents { CGPoint origin;CGSize size;};
NSLog(#"rect : %#",NSStringFromCGRect(rect));
Check this:
CGPoint origin = {10, 20};
CGSize size = {100, 200};
CGRect rect = {origin, size};

How to recognize if CGPoint is included in shape?

I'm a beginner in quartz.
I'm wonderring a way that recognize if CGPoint is included in shape.
Please give me a help of Expert.
Follow is concept diagram.
In above three case,
Result I want is YES, Because three RED CGPoint is included in shape.
It is possible like follow way?
CGPoint RedPoint1 = {200,100};
CGPoint RedPoint2 = {200,200};
CGPoint RedPoint3 = {350,300};
BOOL includeRect;
includeRect = CGRectContainsPoint(RectCase, RedPoint1);
BOOL includeCircle;
includeCircle = CG ? ContainsPoint(CircleCase, RedPoint2)
BOOL includeBoldLine;
includeBoldLine = CG ? ContainsPoint(BoldLineCase, RedPoint3);
The ease of this all depends on how your shapes are defined.
If you have these as CGPathRefs or NSBezierPaths there is a containsPoint: method you could use.
If these are CGRects that have a transform applied to them, you can use the CGAffineTransformPoint methods to move the point into the same coordinate space and then use CGRectContainsPoint

setting and using obj-c CGPoint, CGRect, and others

I have three questions surrounding what I think is the topic of structs in obj-c
1) Why is it that I often (or always) cannot take a member var that is a CGPoint or a CGRect and set the values one by one? I find I have to do:
CGPoint point;
point.x = someValue;
point.y = someOtherValue;
obj.myPoint = point;
instead of simply obj.myPoint.x = someValue etc.
2) Is this behavior that is consistent across all structs in obj-c?
3) Is there an easy way to add two CGPoints? It seems like there should already be, but I couldn't find one. I thought it'd be cumbersome if I'd have to use a temporary CGPoint to accumulate values between two CGPoints before setting the dest var to the temp var (because of not being able to just do pointA.x += pointB.x (same for y).
1) From #sb in an answer to Cocoa Objective-c Property C structure assign fails
That won't accomplish anything, because [t member] returns a struct, which is an "r-value", ie. a value that's only valid for use on the right-hand side of an assignment. It has a value, but it's meaningless to try to change that value.
Basically you just have to live with the fact that you can't set the fields of struct directly when returned from a function.
2) Yes
3) Unfortunately I don't think there is a built-in convenience method for adding two CGPoint. If you find your self doing this frequently you can make your own:
CGPoint CGPointAdd(CGPoint p1, CGPoint p2)
{
return CGPointMake(p1.x + p2.x, p1.y + p2.y);
}
and then use it like:
obj.pointA = CGPointAdd(obj.pointA, pointB);
not as elegant as obj.pointA.X += ... but sometimes life isn't fair.
This is perfectly normal. The 'obj' owns that var and has getters and setters, you can not modify parts of that variable.
The best thing to do is copy the struct and modify whatever you need.
Also note you can use the CGPointMake(x, y) function (and the same for all CG structs), which is much easier.
To update this is easiest:
CGPoint point = obj.myPoint;
point.x += 10.0f;
obj.myPoint = point;
Obj-C 2.0 hides the getters and setters which looks like this:
CGPoint point = [obj myPoint];
point.x += 10.0f;
[obj setMyPoint:point];