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.
Related
I have a program displaying planes of cubes, like levels in a house, I have the planes displayed so that the display angle is consistent to the viewport projection plane. I would like to be able to allow the user to select them.
First I draw them relative to each other with the first square drawn at {0,0,0}
then I translate and rotate them, each plane has it's own rotate and translate.
Thanks to this this page I have code that can cast a ray using the user's last touch. If you notice in the picture above, there is a green square and blue square, this is debug graphic displaying the ray intersecting the near and far planes in the projection matrix after clicking in the centre (with z of zero in order to display them), so it appears to be working.
I can get a bounding box of the cube, but it's coordinates will think they are still up in the left corner.
My question is how do I use my ray to check intersections with the objects after they have been rotated and translated? I'm very confused as I once had this working when I was translating and rotating the whole grid as one, now each plane is being moved separately I can't work out how to do it.
So the picture below is from one of the slides of my computer graphics class. Can anyone please explain to me what the last 4 lines of code do ?
Those 4 lines of code are specific to the application you're writing in your class (or that your teacher is showing you or whatever). They appear to be setting up some scene coordinates so that the center of the scene is always aligned with the center of the window. cx is the x coordinate of the center of the scene (in world coordinates, I imagine). dy is the height of the scene (also in world coordinates). In the last 2 lines, the left and right edges of the scene are adjusted based on the aspect ratio of the window. The new left edge is the center minus half the height times the aspect ratio of the window.
Also, you should understand that OpenGL does not have a Reshape function. That is part of glut, which is not part of OpenGL. glut is a separate library to aid in using OpenGL, but is not part of OpenGL itself. I mention that mainly because glut is deprecated on some platforms.
I have a program in which I have a simple rectangle drawn over the screen. When I put the rectangle inside a camera such as a ofEasyCam, it translates the position of the rectangle to the centre of the screen. Also, it flips the figure vertically and gives me an inverted drawing of the rectangle.
I have lots of objects on the screen and all of them appear inverted. How do I prevent the camera from rotating the Y-axis so that my object appears as is?
Found this link which suggests there might be a bug, the solution suggested is to do the following:
cam.setScale(1, -1, 1);
It also mentioned that this occurred when they tried an "ofEasyCam - inside an ofFbo"
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.
At the moment, I have a solar system in openGL with multiple coordinate systems. I.e., the sun has its own orbit coordinate system for planets that orbit around it and its own coordinate system for the circle that represents the sun. Every planet has its own orbitCS and CS for its shape.
I use GL_TRIANGLE_FAN to make spheres for the planets.
Then, when I receive mouse coordinates, I use glUnProject to translate them from window to world coordinates. Then, I attempt to translate these world coordinates to local coordinates to determine if a mouse click is within a planet shape or not. If so, it outputs the name of the planet - the hard part is getting the planet click recognition to work.
I can not seem to figure out how to do this properly. The formula I am using true if ((pow(target_LCS[0],2) + pow(target_LCS[1],2)) < pow(radius,2)) does not seem to work correctly. Instead of determining whether or not the click is in the shape, it is never true. I also do not know how to properly determine the radius with the TRIANGLE_FAN.
OpenGL knows nothing about objects. When you create a sphere with GL_TRIANGLE_FAN you must have code that tells opengl where to draw the sphere and what the radius is. If you want to do intersection testing, you will need to keep track of the planet radii and the planet positions yourself. So just use the positions and radii specified from the drawing code.