Cocos2d-x - Understanding positioning sprites on screen - c++

Can anyone provide some basic pointers on placing CCSprites on screen?
Example:
CCSize s = CCDirector::sharedDirector()->getWinSize();
With s, say I wanted to position a sprite on the very bottom of the screen starting at 0, think something like grass.
if I am running at 1024 x 768, middle is:
setPosition( ccp(s.width/2, s.height/2) );
so starting all the way left and middle would be:
setPosition( ccp(0, s.height/2) );
So how do I get farther down?
setPosition( 0, s.height) );
This puts me starting at the top left and staying along the top of the screen.
Any help would be appreciated.

Position is relative to the sprite's parent, as well as its anchorPoint.
anchorPoint generally ranges from 0 to 1 for each coordinate, with a default of 0.5. I say "generally" because it can really be any value, but ranges outside of 0-1 place you outside of the bounds of the sprite.
For example, an anchorPoint of (0,0) makes positions relative to the bottom left. (1,0) is the bottom right, (0,1) is the top left and (1,1) is the top right. (0.5,0.5) is the very center of the sprite, which is the default.
Basically you just multiple the value by the width to get the relative position.
If you want to place a sprite at the very bottom of the screen (the bottom left corner aligned with the bottom left corner of the screen), you can do it multiple ways, based on the anchorPoint alone.
With the default anchorPoint of (0.5,0.5), the position would be (sprite.contentSize.width/2, sprite.contentSize.height/2).
If you set the anchorPoint to (0,0), the same position is obtained by simply (0,0).
If you wanted to move that sprite to the very center of the screen (the center of the sprite right in the middle), with an anchorpoint of (0.5, 0.5), the position would be (s.width/2, s.height/2).
This is all assuming you are adding a sprite to a parent the size of the screen, which is where the 2nd part of positioning comes in.
Position is also relative to the sprite's parent - which could be any other CCNode (CCLayer, another CCSprite, etc).
The way to think of that is not much different than adding a full screen node - except think in terms of the parent's size and position, not the screen.

Also Just to add something, all buttons start out in the middle of the screen then you can move them from there. if you wanted to button at (0,0):
CCLabelTTF *label1 = [CCLabelTTF labelWithString:#"Press Me!" fontName:#"Marker Felt" fontSize:20];
CCMenuItemLabel *button1 = [CCMenuItemLabel itemWithLabel:label1 block:^(id sender) { NSLog(#"button1 pressed"); }];
button1.position = ccp(-(s.width/2) , -(s.height/2)); // <---

Related

QGraphicsScene position problems

So i trying to do paint and i run into some problems. When i try to draw QGraphicsRectItem it doesnt get positioned where i want it.
QColor neki1(23, 145, 195);
QPen pen1(neki1, 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
QGraphicsRectItem* sel = new QGraphicsRectItem(0,0,100,100);
sel->setPen(pen1);
sel->setBrush(Qt::transparent);
addItem(sel);
this is the code and picture and you can see it get position like somewhere in the middle of the screen. On the begining of the program i set scene->setSceneRect(QRectF(0, 0, 1000, 1000)); but cordinates in top left corner of scene are not 0,0. Is there anyway to solve this?
Thank you for your help.
Oh, that's common mistake. You had set scene's bounding rect. But QGraphcsView can scroll about and zoom into that scene.
It "aims" at top left corner of scene, so 0,0 is somewhere near middle. You have to match view to the scene's bounding rect, e.g. with setAlignment, fitInView, etc.

c++ win32 Relative position to desktop

How will i get a application x y position relative to the client screen?
I tried but all unsucessful so can anyone help.
RECT pta;
GetWindowRect(hWnd,&pta);
POINT Rpt = { pta.left, pta.top };
ScreenToClient(hWnd, &Rpt);
But this doesn't work.
I want to set my cursor position to middle in the window of my app
If I understand right, you want to call the SetCursorPos() windows API call to center the mouse cursor to your window. That function takes screen coordinates.
GetWindowRect() returns the window top and left coordinates already in the screen coordinates, so no transform is necessary.
To get to your window's center coordinates, you just need to add half of your window's width and height to the top-left point's coordinates. Then you can call SetCursorPos().

setTransformOriginPoint not working as expected

I inherited from the QGraphicsObject and created a new class that has a pixmap and sets its transform origin point to:
setTransformOriginPoint(boundingRect().center());
But when I call setRotation() on the my class (which is added to a QGraphicsView using the scene), the rotation doesn't use the center as the rotation anchor. How can I set the center to be the anchor of the rotation ? Thanks !
More information: calling setRotation() outside of a sceneEvent function it works, but inside a sceneEvent, upon a pinch gesture, the origin point doesn't work.
Draw pixmap at QRect(0, 0, pixmap.width(), pixmap.height(). Use this rectangle for bounding rect also. Use setPos to move the item around the scene. Use setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2) to set the origin point. These coordinates are in the item coordinates, so they should point at the pixmap's center regardless of the item's position.

centering a sprite in the middle of the screen convertToWorldSpace / NodeSpace

i have passed many many days on this problem, i've not very clear in mind what convertToWorldSpace, convertToNodeSpace does...
i have 2 layer (CCNode) one that i can scale, one for panning the screen, so i scale always what is displayed makeing the center of scaling the middle of the screen:
scaleLayer=[CCNode node];
scaleLayer.position= ccp( size.width /2 , size.height/2 );
[self addChild:scaleLayer z:-2000];
panLayer=[CCNode node];
panLayer.position= ccp( -size.width /2 , -size.height/2 );
[scaleLayer addChild:panLayer z:-2000];
now i add sprites to panlayer:
[panLayer addChild:mysprite z:2];
i'd like to have a button that, when pressed, moves panlayer to make the sprite in the middle of the screen.
i have wrote this but works only if scaleLayer.scale=1
CGPoint p;
p=[panLayer convertToWorldSpace:mysprite.position];
p.x=(panLayer.position.x+(240-p.x));
p.y=(panLayer.position.y+(160-p.y));
[panLayer runAction:[CCMoveTo actionWithDuration:0.5 position:p]];
how can i make the sprite to be at center of the screen with different scales?
thanks
I'm not sure what your existing code does but you might find the property mysprite.boundingBox useful. It will give you a CGRect with the sprite's width and height and the position of its bottom left corner, and it will reflect whatever scale you have applied to the sprite.

Resize By Mouse

I have a rectangle class that has 2 points, the center axes point and the size of the rectangle. Lets say I want to drag the bottom of the rectangle with the mouse but keep the top of it in the same position. What is the algorithm to find the center position and the new rectangle size based on the mouse? Thanks in advance :)
Move the center in the same direction and half the distance (in either or both X and Y) as the bottom (right-hand corner) was dragged.
I would assume a graphics API is at hand here, what is it? I also assume that you have worked out how to detect that mouse clicking onto the edge of your box, have you decided exactly how that works though? do they just need to click near it, and they then drag the exact corner or what?
I can tell you that you are going to need to log the position of the mouse when they first click and get the differance to where they are now. Half that distance, and then add it to the original centre.
EDIT
oh, for the new size, its the difference of the mouse position added onto the original size. so if the mouse has moved down (increasing y) and left (decreasing x) say 10 units each way, you make the box ten units taller and ten narrower, the centre will be 5 down and 5 left.
It would be easier to tell the difference in the mouse movement, and create a new rectangle that size. Then use the rectangle class to check for the center point. Far easier than offsetting the old center.