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"
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.
I plan on making a game, and I want to create some background animations for said game. One of these animations is a rotating rectangle. I've looked all over, and I cannot find any form of math or logic that allows me to rotate a rectangle (SDL_Rect to be specific, but you might have already known that).
I can't figure out the math for myself, I really don't have any working code for this, so I can't show anything.
Essentially I'm looking for some type of logic that I can apply the rectangle's coordinates so that whenever the main game loop loops, it will rotate the rectangle some amount of degrees.
You can't rotate an SDL_Rect. If you look at its definition, it's made of coordinates for the top-left corner, the width and the height. There's no way to represent a rectangle with sides that aren't parallel to the coordinate system's axes.
SDL_RenderCopyEx supports drawing rotated textures, though.
I have trouble in opengl. I want to rotate my vehicle while moving forward/backward. Here's a picture which shows exactly my problem. Effects of current code are in blue - after moving the car rotates over the starting location and not the current one. I want to have situation in red - in which my vehicle will rotate over current position and later move forward/backward correctly.
My current code:
lxr=sin(angle);
lzr=cos(angle);
xr+=speed*lxr;
zr+=speed*lzr;
totalangle+=angle
glRotatef(totalangle,0.0,1.0,0.0);
glTranslatef(0.0,0.0,xr);
drawVehicle();
You can try to call translate before rotate. glRotatef rotate view matrix and it affects on current view and also matrix glTranslatef.
From the image, I thought you are translating and then rotating, but looking at the code, I see it is not true.
So, it is obvious that you are in the drawVehicle(); function not rendering your object in the center (0,0). You need to render it in the center, rotate and then translate.
Also, your translation is bogus. You are just translating in z direction, not in y :
glTranslatef(0.0,0.0,xr);
You need to do something like this :
glRotatef(totalangle,0.0,1.0,0.0);
glTranslatef(0.0,yOffset,0.0);
drawVehicle(); // render around [0,0]
you have to move the origin of the coordinate system too, in order to rotate your car as you wish.
I have a sprite, a square, just for orthogonal projection. Now I want to project it in a very basic, simple isometric way. (I know this might not be pretty, but I just want to figure this out)
Given my square, I rotate it 45 degrees. Now if I understand correctly, I should still divide my height by 2. This has been impossible for me in SFML. There is a scale function but if I scale with a factor 0.5 in the y-axis direction, my cube just gets stretched, instead of a diamond shape. It looks as though SFML transforms the sprite according to it's own relative axes (that were rotated before..).
Since you cannot access the height of a sprite, I was wondering if this was even possible?
Can I convert a square sprite to a diamond shape in SFML?
Using a sf::RenderTexture is an option (see other answer). Another option is to fiddle with the sf::View. Double the view's height, and adjust coordinates. It would go something like this:
my_sprite.setRotation(45.f);
//adjust the position for new screen coordinates (once)
my_sprite.setPosition(my_sprite.getPosition().x, my_sprite.getPosition().y * 2);
//...
//when drawing:
sf::View v = my_render_window.getDefaultView();
v.setSize(v.getSize().x, v.getSize().y * 2);
v.setCenter(v.getSize() *.5f);
my_render_window.setView(v);
my_render_window.draw(my_sprite);
my_render_window.setView(my_render_window.getDefaultView());
Rotate your sprite as you are doing now. Render it to an sf::RenderTexture. Use the member function getTexture, and make a new sprite from it, or reuse the old sprite. Scale the sprite along the y-axis. Draw it to the render window.
Some math on your part may be required in order to set the RenderTexture to the right size and to draw the original sprite in the correct location on it.
original_sprite.setRotation(45);
sf::RenderTexture rt;
rt.create(FigureOutWidth(),FigureOutHeight());
original_sprite.setPosition(MoreMathHere());
rt.draw(original_sprite);
sf::Sprite new_sprite(rt.getTexture());
new_sprite.setScale(1.0,0.5);
It should go without saying, but do this once in initialization, not every frame.
In OpenGL I have to rotate a cube (and translate it) so that it looks like in these two images.
Without any transformations only the front facing red face is visible. I just don't understand how I can rotate it (so that the top and right sides are visible like in the images) and keep the red face perfectly square.
I've thought about translating it to the bottom left, but that only moves the red square around, it doesn't make the other faces visible.
I'm using glFrustum(-20, 20, -20, 20, -1, -10);
If you are using a perspective projection (which you are) and the front face of your cube is parallel to the x-y plane, then you will only see the other two faces if the cube is entirely in one quadrant of the eye space; that is, if there were horizontal and vertical lines cutting the window in half, the cube would have to lie entirely within one of the four resulting rectangles.
Other options for making the other two faces show are
use an isometric projection
rotate the cube to bring the other faces into view.
To aid in visualising this, try playing Minecraft (say) and moving around in different ways to see how different sides of different blocks come into view.
That is not a rotation.
The second picture looks like an orthographic projection (glOrtho), but that may be a coincidence.
In either case, you can only get an image like that if the cube is translated away from the origin toward the bottom left, as you suggest.