I have a vector between (posX, posY) and (mouseX, mouseY), and I get the mouse position as a positive integer with allegro's event library. From this vector using an arc tangent I get the radian of (deltaX, deltaY). I then plug that radian into an al_draw_rotated_bitmap function. I expect the bitmap to point towards where the mouse cursor is, but the issue I have is that the radian or vector is causing it to be rotated perpendicular to the cursor.
Here is the relevant code:
void setRotation(int dx, int dy)
{
float deltax = posX - mouseX;
float deltay = posY - mouseY;
rotation = atan2(deltay, deltax);
}
void Player::draw()
{
al_draw_rotated_bitmap(player, al_get_bitmap_width(player) / 2, al_get_bitmap_height(player) / 2, posX, posY, rotation, 0);
}
int main()
{
while(true)
{
player.setRotation(mouseX, mouseY);
player.draw();
al_flip_display();
}
}
Imagine (deltax, deltay) = (0, 100), that is the mouse is 100 pixels above the object, and so the picture (I believe) shouldn't be rotated at all. But atan2(deltay, deltax) = atan2(100, 0) = π/2, that's why your picture is rotated perpendicularly.
To fix this you should change it to atan2(deltax, deltay), possibly adding - before arguments depending on the direction of X and Y axes in Allegro, which I don't know.
In other words, atan2 measures the angle relative to X axis, but in your case the angle should be measured relative to Y axis (because alignment on Y axis means no rotation), so you should swap its arguments.
Related
The device I am using generates vectors like this;
How do I translate polar (angle and magnitude) from a left handed cordinate to a cartesian line, drawn on a screen where the origin point is the middle of a screen?
I am displaying the line on a wt32-sc01 screen using c++. There is a tft.drawline function but its references are normal pixel locations. In which case 0,0 is the upper left corner of the screen.
This is what I have so far (abbreviated)
....
int screen_height = tft.height();
int screen_width = tft.width();
// Device can read to 12m and reports in mm
float zoom_factor = (screen_width / 2.0) / 12000.0;
int originY = (int)(screen_height / 2);
int originX = (int)(screen_width / 2);
// Offset is for screen scrolling. No screen offset to start
int offsetX = 0;
int offsetY = 0;
...
// ld06 holds the reported angles and distances.
Coord coord = polarToCartesian(ld06.angles[i], ld06.distances[i]);
drawVector(coord, WHITE);
Coord polarToCartesian(float theta, float r) {
// cos() and sin() take radians
float rad = theta * 0.017453292519;
Coord converted = {
(int)(r * cos(rad)),
(int)(r * sin(rad))
};
return converted;
}
void drawVector(Coord coord, int color) {
// Cartesian relative the center of the screen factoring zoom and pan
int destX = (int)(zoom_factor * coord.x) + originX + offsetX;
int destY = originY - (int)(zoom_factor * coord.y) + offsetY;
// From the middle of the screen (origin X, origin Y) to destination x,y
tft.drawLine( originX, originY, destX, destY, color);
}
I have something drawing on the screen, but now I have to translate between a left handed coordinate system and the whole plane is rotated 90 degrees. How do I do that?
If I understood correctly, your coordinate system is with x pointing to the right and the y to the bottom and you used the formula for the standard math coordinate system where y is pointing up so multiplying your sin by -1 should do the trick (if it doesn't, try multiplying random things by -1, it often works for this kind of problems).
I assuming (from your image) your coordinate system has x going right y going up angle going from y axis clockwise and (0,0) is also center of your polar coordinates and your goniometrics accept radians then:
#include <math.h>
float x,y,ang,r;
const float deg = M_PI/180.0;
// ang = <0,360> // your angle
// r >= 0 // your radius (magnitude)
x = r*sin(ang*deg);
y = r*cos(ang*deg);
I have a 2D Rectangle defined with 4 points ordered in counter clockwise fashion - e.g. point 0(x0,y0), point 1(x1, y1), etc.I would like to know how to rotate each of these points in 3D space (even though the Rectangle is 2D).
I would like to randomly choose the axis (x, y or z) to rotate around. Something along the lines of the following C++ code for each point in the Rectangle:
struct Point { float x, y; };
// Rotate around X-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateXaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
float ry = p.y*cos(rads) + rz*sin(rads);
p.y = ry;
}
// Rotate around Y-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateXaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
float rx = rz*sin(rads) + p.x*cos(rads);
p.x = rx;
}
// Rotate around Z-Axis
// pt is current point in Rectangle
// rz is randomly chosen z-coordinate value between [-1,1]
void rotateZaxis(Point &p, angle, float rz) {
float rads = PI * angle / 180.0;
rx = p.x*math.cos(rads) - p.y*math.sin(rads);
ry = p.x*math.sin(rads) + p.y*math.cos(rads);
p.x = rx;
p.y = ry;
}
Is the above code correct for what I would like to do?
Thank you in advanced for any help.
Your implementation doesn't look correct to me. I were you I would just write one function for rotation around any axis through the origin of the coordinate system pointing in a general direction. Then you choose the specific directions as you please. Here is code in python (it is more concise and presents the idea more clearly), you can implement it in C++.
import numpy as np
import math
def rotation(axis, angle, Vector):
'''
axis should be a unit vector!
'''
axis_X_Vector = np.cross(axis, V)
rotated_Vector = Vector
rotated_Vector = rotated_Vector + math.sin(angle)*axis_X_Vector
rotated_Vector = rotated_Vector + (1 - math.cos(angle))*np.cross(axis, axis_X_Vector)
return rotated_Vector
In OpenGL, I'm building a football game that allows you to shoot a ball by first moving height indicators left and right, before shooting based on the indicators when a button is pressed. Here's what it looks like:
Football Game Visual
When these indicators are moved, my ball needs to travel at the height of the vertical indicator (y), and left or right direction if the vertical one (x).
Firstly, here's the code that moves my indicators (which are just textures being drawn in my RenderScene() function)
void SpecialKeys(int key, int x, int y){
if (key == GLUT_KEY_RIGHT) { // moves the bottom indicator RIGHT
horizontalBarX += 5.0f;
}
if (key == GLUT_KEY_LEFT) {
horizontalBarX -= 5.0f; // moves the bottom indicator LEFT
}
if (key == GLUT_KEY_UP) { // moves the top indicator UP
verticalBarY += 5.0f;
verticalBarX += 1.0f;
}
if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN
verticalBarY -= 5.0f;
verticalBarX -= 1.0f;
}
}
Calculations for my football to move
Now to get my football to move after the indicators have be moved, I need to apply the following calculations to the x, y and z axis of the ball:
x = sin(theta) * cos (phi) y = cos(theta) * sin(phi) z = cos(theta)
where theta = angle in z-x, and phi = angle in z-y
So with this, I have attempted to get the values of both theta and phi angles first, by simply incrementing them depending on what height indicators you have pressed in the SpecialKeys() function:
void SpecialKeys(int key, int x, int y){
if (key == GLUT_KEY_RIGHT) { // moves the bottom indicator RIGHT
horizontalBarX += 5.0f;
theta += 5; // Increase theta angle by 5
}
if (key == GLUT_KEY_LEFT) {
horizontalBarX -= 5.0f; // moves the bottom indicator LEFT
theta -= 5; // Decrease theta angle by 5
}
if (key == GLUT_KEY_UP) { // moves the top indicator UP
verticalBarY += 5.0f;
verticalBarX += 1.0f;
phi += 5; // Increase theta angle by 5
}
if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN
verticalBarY -= 5.0f;
verticalBarX -= 1.0f;
phi -= 5; // Decrease phi angle by 5
}
}
Now that I have the angles, I want to plug in the calculated values into the drawFootball() parameters, which by the way is initially called in my RenderScene function as...
drawFootBall(0, 40, 500, 50); // x,.y, z, r
...and here's how I'm attempting to launch the ball with the calculations above:
void SpecialKeys(int key, int x, int y){
// indicator if statements just above this line
if (key == GLUT_KEY_F1) {
drawFootBall(sin(theta)*cos(phi), cos(theta)*sin(phi), cos(theta), 50);
}
}
But when I go to click the launch button F1, nothing happens at all. Where have I messed up?
EDIT:
If it helps, here's my drawFootball() function:
void drawFootBall(GLfloat x, GLfloat y, GLfloat z, GLfloat r)
{
glPushMatrix();
glFrontFace(GL_CCW);
glTranslatef(x,y,z);
//create ball texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_BALL]);
//glDisable(GL_LIGHTING);
glColor3f(0.5,0.5,0.5);
quadricFootball = gluNewQuadric();
gluQuadricDrawStyle(quadricFootball, GLU_FILL);
gluQuadricNormals(quadricFootball, GLU_SMOOTH);
gluQuadricOrientation(quadricFootball, GLU_OUTSIDE);
gluQuadricTexture(quadricFootball, GL_TRUE);
gluSphere(quadricFootball, r, 85, 50);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
Firstly make sure your theta and phi are in the right units i.e. Radians. If in degrees convert them to radians by using sin(theta * PI/180.0f) and so on, assuming PI is defined.
I believe what you are computing there is a direction vector for the Ball. The d(x,y,z) is the direction in which the ball should travel, (assuming there is no gravity or other forces). Its probably the direction in which the ball is kicked.
I think if you simply wanted to move your ball, you need to multiply this direction with a length. Since your ball has a radius of 50 units, try translating to 2 times this radius.
glTranslatef(2.0f*r*x,2.0f*r*y,2.0f*r*z);
This will move the ball to 2 times its radius in your desired direction.
However you probably want to have some physics to have a more realistic movement.
I'm trying to implement a picking ray via instructions from this website.
Right now I basically only want to be able to click on the ground to order my little figure to walk towards this point.
Since my ground plane is flat , non-rotated and non-translated I'd have to find the x and z coordinate of my picking ray when y hits 0.
So far so good, this is what I've come up with:
//some constants
float HEIGHT = 768.f;
float LENGTH = 1024.f;
float fovy = 45.f;
float nearClip = 0.1f;
//mouse position on screen
float x = MouseX;
float y = HEIGHT - MouseY;
//GetView() returns the viewing direction, not the lookAt point.
glm::vec3 view = cam->GetView();
glm::normalize(view);
glm::vec3 h = glm::cross(view, glm::vec3(0,1,0) ); //cameraUp
glm::normalize(h);
glm::vec3 v = glm::cross(h, view);
glm::normalize(v);
// convert fovy to radians
float rad = fovy * 3.14 / 180.f;
float vLength = tan(rad/2) * nearClip; //nearClippingPlaneDistance
float hLength = vLength * (LENGTH/HEIGHT);
v *= vLength;
h *= hLength;
// translate mouse coordinates so that the origin lies in the center
// of the view port
x -= LENGTH / 2.f;
y -= HEIGHT / 2.f;
// scale mouse coordinates so that half the view port width and height
// becomes 1
x /= (LENGTH/2.f);
y /= (HEIGHT/2.f);
glm::vec3 cameraPos = cam->GetPosition();
// linear combination to compute intersection of picking ray with
// view port plane
glm::vec3 pos = cameraPos + (view*nearClip) + (h*x) + (v*y);
// compute direction of picking ray by subtracting intersection point
// with camera position
glm::vec3 dir = pos - cameraPos;
//Get intersection between ray and the ground plane
pos -= (dir * (pos.y/dir.y));
At this point I'd expect pos to be the point where my picking ray hits my ground plane.
When I try it, however, I get something like this:
(The mouse cursor wasn't recorded)
It's hard to see since the ground has no texture, but the camera is tilted, like in most RTS games.
My pitiful attempt to model a remotely human looking being in Blender marks the point where the intersection happened according to my calculation.
So it seems that the transformation between view and dir somewhere messed up and my ray ended up pointing in the wrong direction.
The gap between the calculated position and the actual position increases the farther I mouse my move away from the center of the screen.
I've found out that:
HEIGHT and LENGTH aren't acurate. Since Windows cuts away a few pixels for borders it'd be more accurate to use 1006,728 as window resolution. I guess that could make for small discrepancies.
If I increase fovy from 45 to about 78 I get a fairly accurate ray. So maybe there's something wrong with what I use as fovy. I'm explicitely calling glm::perspective(45.f, 1.38f, 0.1f, 500.f) (fovy, aspect ratio, fNear, fFar respectively).
So here's where I am lost. What do I have to do in order to get an accurate ray?
PS: I know that there are functions and libraries that have this implemented, but I try to stay away from these things for learning purposes.
Here's working code that does cursor to 3D conversion using depth buffer info:
glGetIntegerv(GL_VIEWPORT, #fViewport);
glGetDoublev(GL_PROJECTION_MATRIX, #fProjection);
glGetDoublev(GL_MODELVIEW_MATRIX, #fModelview);
//fViewport already contains viewport offsets
PosX := X;
PosY := ScreenY - Y; //In OpenGL Y axis is inverted and starts from bottom
glReadPixels(PosX, PosY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, #vz);
gluUnProject(PosX, PosY, vz, fModelview, fProjection, fViewport, #wx, #wy, #wz);
XYZ.X := wx;
XYZ.Y := wy;
XYZ.Z := wz;
If you do test only ray/plane intersection this is the second part without DepthBuffer:
gluUnProject(PosX, PosY, 0, fModelview, fProjection, fViewport, #x1, #y1, #z1); //Near
gluUnProject(PosX, PosY, 1, fModelview, fProjection, fViewport, #x2, #y2, #z2); //Far
//No intersection
Result := False;
XYZ.X := 0;
XYZ.Y := 0;
XYZ.Z := aZ;
if z2 < z1 then
SwapFloat(z1, z2);
if (z1 <> z2) and InRange(aZ, z1, z2) then
begin
D := 1 - (aZ - z1) / (z2 - z1);
XYZ.X := Lerp(x1, x2, D);
XYZ.Y := Lerp(y1, y2, D);
Result := True;
end;
I find it rather different from what you are doing, but maybe that will make more sense.
I was just wondering how would I go about clipping a circle in a rectangular boundary box? I am currently using the Cohen–Sutherland algorithm for line clipping in my program and so far I've managed to get rectangles and polygons to clip. However, for circle clipping, I have no idea how I would accomplish this. I'm using the following to construct my circle:
glBegin(GL_POLYGON);
double radius = 50;
for(int angle = 0; angle <= 360; angle++ ){
float const curve = 2 * PI * (float)angle / (float)360;
glVertex2f(point.x + sin(curve) * radius, point.y + cos(curve) * radius);
}
glEnd();
My clipping algorithm is the same as the one here: http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm. However, it returns 2 points representing a new line to later be used to draw the clipped shape. So basically I've tried to do this:
line Lines[360] // an array with size 360 with data type line, which is a struct holding two points (x1, y1, x2, y2) of the new line returned by my clipping function.
double radius = 50;
for(int angle = 0; angle < 360; angle++){
float const currentCurve = 2 * PI * (float)angle / (float)360;
float const nextCurve = 2 * PI * (float)(angle+1) / (float)360;
int x1 = (int)(point[i].x + sin(currentCurve) * radius); // point is another struct holding only a single point.
y1 = (int)(point[i].y + cos(currentCurve) * radius);
x2 = (int)(point[i+1].x+ sin(nextCurve) * radius);
y2 = (int)(point[i+1].y + cos(nextCurve) * radius);=
// Clip the points with the clipping algorithm:
Lines[i] = Clipper(x1, y1, x2, y2);
}
// Once all lines have been clipped or not, draw:
glBegin(GL_POLYGON);
for(int i = 0; i < 360; i++){
glVertex2f(Lines[i].x1, Lines[i].y1);
glVertex2f(Lines[i].x2, Lines[i].y2);
}
glEnd();
Note that, I've drawn a circle on the screen with a mouse and and stored each 360 points into a struct array called point, which is apart of a linked list. So I have like 1 node representing one circle on the screen.
Anyway, with the above, my circle is not drawing clipped (or drawing at all for that matter) and my application crashes after a few mouse clicks.
Use the scissor test - read up on glScissor(): http://www.opengl.org/sdk/docs/man/xhtml/glScissor.xml