CGRectMake parameters ?

Forums Programming cocos2d support (graphics engine) CGRectMake parameters ?

This topic contains 14 replies, has 9 voices, and was last updated by  ShaneT 1 year, 5 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
Author Posts
Author Posts
August 26, 2010 at 7:57 pm #224304

EasyDev
Participant
@easydev

Hey developers !

I am really enjoying cocos2d now, and my game is almost finished ! I hope you’ll be able to download it from the appstore in a few weeks :) I hope you’ll really appreciate it ! But I still get problems programming, and I have just one question ; I have to make CGRects to be able to do two CCSprites interact (for example when one hit the other, both disappear), so, here is the code I found in the documentation :

persoRect = CGRectMake(perso.position.x - (perso.contentSize.width/2),
perso.position.y - (perso.contentSize.height/2),
perso.contentSize.width,
perso.contentSize.height);

gateauRect = CGRectMake(gateau.position.x - (gateau.contentSize.width/2),
gateau.position.y - (gateau.contentSize.height/2),
gateau.contentSize.width,
gateau.contentSize.height);

I am a bit confused because there is 4 parameters, could you please tell me what does each parameter correspond to ? Completing this :

rect = CGRectMake(firstParameterIs…, secondParameterIs…, thirdParameterIs…, fourthParameterIs…);

Thanks in advance for your help ! :)

August 26, 2010 at 8:31 pm #296034

abitofcode
Moderator
@abitofcode

Hold down the command key then double click the word CGRectMake.

If you’ve installed the documentation (see the READ THIS FIRST POST at the top of the forum) you can hold the OPTION key (ALT) and double click the word for the documentation.

SHIFT + COMMAND + F is also very useful for finding stuff across the whole project. Especially useful if you have the cocos2d source download with the tests.

While we’re at it COMMAND + OPTION + UP ARROW will toggle between your .m and you .h files

I find Command + SHIFT + E useful for toggling the editor into a full pane.

And the final one In XCODE>Preferences>Debugging set Auto clear debug console to save a lot of scrolling.

edit: CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

August 26, 2010 at 8:45 pm #296035

xStoryTeller
@xstoryteller

Startpoint.x, Startpoint.y, Width, Height.

CGRectMake(10,10,8,3);

x - - - - - - - - x (18,13)
| |
| |
| |
x - - - - - - - - x
(10,10)

Hope this helps ;)

August 26, 2010 at 9:01 pm #296036

EasyDev
Participant
@easydev

Sorry for my stupidity but I didnt understood and I’m not on my computer so I can’t take a look at thé documentation. Thanks for your help :)

August 26, 2010 at 9:18 pm #296037

chrisco
Participant
@chrisco

@abitofcode – great tips! Had no idea about the last three and the auto clear will fix one of the things that drives me nuts constantly :) I’m also a big fan of ctrl-clicking parts of my code – there’s a bunch of useful options there and great if you can’t remember all of those key commands.

August 26, 2010 at 10:21 pm #296038

David994A
Participant
@david994a

All Cocoa API documentation is on the developer.apple.com website

August 27, 2010 at 8:12 am #296039

EasyDev
Participant
@easydev

Thanks a lot for the help, so, I am now on my computer and I took a look about the documentation, and I think this code is correct:

persoRect = CGRectMake(perso.position.x - (perso.contentSize.width/2),
perso.position.y - (perso.contentSize.height/2),
perso.contentSize.width,
perso.contentSize.height);

gateauRect = CGRectMake(gateau.position.x - (gateau.contentSize.width/2),
gateau.position.y - (gateau.contentSize.height/2),
gateau.contentSize.width,
gateau.contentSize.height);

But I don’t know why this code doesn’t work :

if (CGRectIntersectsRect(persoRect, gateauRect)) {
NSLog(@"touched");
}

Any ideas ?

August 27, 2010 at 8:44 am #296040

chrisco
Participant
@chrisco

Can you try this?

if (CGRectIntersectsRect([perso boundingBox], [gateau boundingBox])) {
NSLog(@"touched");
}

If that doesn’t work, try logging the values of the bounding boxes to the console. Maybe they’re not returning the value you expect them to be. If they are children of different CCNodes for example, and the parent nodes are at different positions, the sprites may look like they’re overlapping on screen, but the position values won’t line up.

August 27, 2010 at 8:59 am #296041

EasyDev
Participant
@easydev

Yes, they are overlapping on the screen. But the code you gave me doesn’t work :( And here is the 2 boundings values from the console : boundings = [perso boundingBox] = 195906619685470208.000000, [gateau boundingBox] = 2473901709568.000000

I don’t understand why this code doesn’t work and why those two boundings have different values…

August 27, 2010 at 10:14 am #296042

chrisco
Participant
@chrisco

How are you logging the values in the console? It’s printing a float, but you need something like the following:

NSLog(@"Gateau boundingBox = %@", NSStringFromCGRect([gateau boundingBox]);
NSLog(@"Perso boundingBox = %@", NSStringFromCGRect([perso boundingBox]);

And are they both children of the same node? That may not make a difference, but it’s also possible that if they’re children of different nodes, they’re in a different coordinate space since their position will be relative to their parent node.

August 27, 2010 at 11:48 am #296043

EasyDev
Participant
@easydev

Well, thanks for your help, that’s always you who fix my problems ! You forced me to search about CCNodes and I saw that I forgot to declare a tag for each of my nodes ! Thanks, now, it works perfectly !

April 28, 2012 at 7:03 am #296044

MisterX
Participant
@misterx

When used with Cocos2D does CGRectMake create a Rect with the origin at the lower left or upper left corner?

I see a diagram someone made above depicting the origin at the lower left. Can anyone else confirm this?

Apple’s Dev Center states the following about CGRect (that it can technically be either lower or upper left):

“In the default Quartz coordinate space, the origin is located in the lower-left corner of the rectangle and the rectangle extends towards the upper-right corner. If the context has a flipped-coordinate space—often the case on iOS—the origin is in the upper-left corner and the rectangle extends towards the lower-right corner.”

April 28, 2012 at 4:34 pm #296045

prj
@prj

A rectangle is a rectangle is a rectangle.

I suspect you’re confusing the origin of the coordinate system with the “origin” of the rectangle. The latter would perhaps be better described as its start position.

So if you have a CGRectMake(5, 5, 20, 20), in iOS coordinates, it would start 5 right from the top, 5 down, and be 20 wide to the right, and 20 deep down.

In Quartz it would start 5 to the right, 5 up, and be 20 wide to the right, and 20 high going up.

If I had MyAwfulGraphicSystem with an origin at the top right, with x positive to the left, and y positive down, it would start 5 to the left, and 5 down, 20 wide to the left, 20 deep.

But in all cases, it’s still defined as having an origin of 5, 5, and a size of 20, 20.

April 28, 2012 at 5:20 pm #296046

clarus
Moderator
@clarus

a CGRect is just a struct with an origin and a size. Like @prj says, different coordinate systems use different origins for laying things out.

These are the most important ones to know when working with cocos2d:

- UIKit uses a top-left origin.

- Cocos2d uses a bottom-left origin.

November 11, 2012 at 4:27 am #296047

ShaneT
Participant
@shanet

Hi, how can you change the rotation of a cgRect created using CGRectMake.

For example you have rectangle image that is rotated at .45, therefore when checking for collisions between two images using the CGRects created using CGRectMake, and checking for the collisions using CGRectIntersectsRect, then the CGRects also hgave to be rotated at the same rotation of the image for the collision to work?

If the CGRects and therefore their points are not rotated the same as the original images then the collisions will be inaccurate.

Am I missing something?

Thanks

Shane

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.