I am developing a cocos2d game and i am a little confused with the coordinate system. I have another application which i use to build levels with it but it has different coordinates. The coordinate (0,0) is in the top left corner and not in bottom left corner like in cocos2d. What is the easiest way to translate the coordinates to cocos2d space? Can i just substract the Y coordinate from the level height? Or is there any simpler way? Is the cocos2d coordinate system left handed or right handed?
Thanks!
I'm pretty sure UIKit has it's origin (0, 0) in the top left corner as well, so you should be able to call:
[[CCDirector sharedDirector] convertToGL:yourpoint];
And that will convert it to the same coordinate system as cocos2d.
Related
I'm using Box2D and cocos2d-x but I can't figure the coordinate system out.
I know cocos2d-x uses P(0,0) as the bottom left part of the screen.
And Box2D uses P(0, 0) as the center of it's world.
I'm using debug draw for Box2D.
When creating a body with a polygon shape attached to it at P(0,0) in Box2D coordinates it shows up on the bottom left part of the screen.
Why doesn't it show up in the center?
When positioning any b2Body, use Sprite::getBoundingBox().getMidX() for the horizontal center, and for the vertical center use Sprite::getBoundingBox().getMidY().
You might want to wrap those calls in a Node::convertToWorldSpace(midX, midY) to make sure that your center coordinates are expanded into the cocos2d-x world coordinate system and hence the Box2D world coordinate space as it has to be consistent.
I have heard that rectangles, coordinates of the mouse and other things involving drawing all use device coordinates. Is this true? Are there any ways that can tell me if I have logical or device coordinates?
I could look at the documentation of functions that give me the coordinates, but sometimes they don't explicitly say if these are logical or device coordinates. For example, the documentation for GetCursorPos function says it "retrieves the position of the mouse cursor, in screen coordinates."
I am assuming screen coordinates are the same as device coordinates? Does this mean I have to convert the screen coordinates I get from the function into client coordinates?
You know what coordinate (0,0) is on the top-left corner of screen. But on paper when we draw a graph, (0,0) may be on bottom left, or on the centre of the graph plotting paper.
By default, the logical and coordinates and physical/screen coordinates are same, and (0,0) points to top-left. But what if you want to draw a line from bottom left to somewhere in middle of screen, that matches the math/trigonometry you've learnt or are practising? Well, the you move to changing the logical coordinate system to something of your liking.
You'd use SetMapMode to change the logical coordinate system. You may later use LPtoDP, DPtoLP, ClientToScreen, ScreenToClient etc for mapping and for physical monitor to your window coordinates mapping.
About Coordinate Spaces and Transformations
I am making a game in Cocos2d. I have a ball that will be shot at a flat surface (the top of the screen) how can I make it so the ball will travel, hit the surface, then reflect the angle and travel that direction? Does that make sense? Please tell me if it doesn't, and I will clarify. Thanks!
EDIT:
Here's an illustration of what I want
Here
You could build the game using box2d (in cocos2d). Then you will have that "effect" for free.
Once you launch a ball at an angle, say 50 degrees, add (cos(50)*speed) to his X position, and (sin(50)*speed) to his Y position.
When you detect the ball's y position has reached the surface's y position, just change the angle to -50.
But you must know that it only works if you want a reflection angle on a top surface, where it hits the top surface and bounces down.
I'm not really sure if it makes sense but I need to do and there is a chance there won't be any obstacle in front.
Is's not clear what you mean by tracing but :
1 You may be looking for gluUnProject to go from screen coordinates to space coordinates. With the help of the Z buffer for distance from camera, you can get the coordinates of 3D point which is seen at the specified pixel.
2 you want to draw a 3D line from camera origin to some 3D point at the mouse cursor. Well it's just a point at the mouse cursor.
I have an OpenGL scene with a top left coordinate system. When I glScale it zooms in from (0,0) the top left. I want it to zoom in from the mouse's coordinate (relative to the OGL frame). How is this done?
Thanks
I believe this can be done in four steps:
Find the mouse's x and y coordinates using whatever function your windowing system (i.e. GLUT or SDL) has for that, and use gluUnProject to get the object coordinates that correspond to those window coordinates
Translate by (x,y,0) to put the origin at those coordinates
Scale by your desired vector (i,j,k)
Translate by (-x,-y,0) to put the origin back at the top left
I did a smooth zoom in using glortho . The skeleton of my solution is
glortho(initial viewport x,y & size)
glcalllist(my display list)
render
.
.
loop to gradually go to final viewrport coordinates/size . Implement your timing and FPS requirements
.
.
glortho(final viewport x,y & size)
glcalllist(my display list)
render
I hope you get the general idea. There are few other methods to acheive this, but I find glortho the method the easiest to comprehend.