• 15
• 15
• 11
• 9
• 10

# How to set position vector according to rotation:

This topic is 4023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys, I'm currently working on a "paratroopers 3d" game for my university. My cannon position is defined according to phi (horizontal) and theta (vertical) angles. when I trigger the cannon to fire a bullet, it first get the initial position of the cannon, and than make the necessary rotations according to phi and theta, so that the bullet location take into account the movements of the cannon. however, the rotations don't influence the 'pos' vector (its value remain the same as the cannon starting point) and the wrong position of the bullet negatively effect the collision detection with other object in the game. therefore, my question is how to extract the rotation matrix from the command glRotate so i can update 'pos' accordingly by multiply it with the matrix. thanks, Adam

##### Share on other sites
Quote:
 Original post by Adam81Hello guys, I'm currently working on a "paratroopers 3d" game for my university. My cannon position is defined according to phi (horizontal) and theta (vertical) angles. when I trigger the cannon to fire a bullet, it first get the initial position of the cannon, and than make the necessary rotations according to phi and theta, so that the bullet location take into account the movements of the cannon.however, the rotations don't influence the 'pos' vector (its value remain the same as the cannon starting point) and the wrong position of the bullet negatively effect the collision detection with other object in the game. therefore, my question is how to extract the rotation matrix from the command glRotate so i can update 'pos' accordingly by multiply it with the matrix. thanks,Adam
First of all, I would think that it would be the initial direction of the projectile rather than its position that you would want to transform (assuming for the sake of simplicity that the projectile's initial position is the same as that of the cannon).

In other words, setting the initial position of the projectile is a simple vector assignment - no transformation is required.

The direction vector on the other hand needs to be computed from phi and theta. There are a few ways to do this:

1. Perform a spherical-to-Cartesian coordinate conversion 'manually'.

2. Grab the matrix from OpenGL and apply it to the initial local-space direction vector (not as straightforward as it sounds).

3. Do your own matrix math and perform the necessary transformations outside of OpenGL.

There's also the question of whether your projectiles are points, or are (e.g.) mesh models that require a complete orientation as part of their description. If the latter, you'll have to compute an initial orientation for the projectile as well.

If you need further help, perhaps you can specify which of the options I mentioned sound most practical to you, and (if relevant) let us know how much experience you have with 3D math, and/or if you have a math library available to you.

##### Share on other sites
Hi jyk and thanks for your help.

i think that the third option would be just enough for me.
i've added the relevant part of my code to emphasis my problem.
right now all of the projectile calculations are within the game::init function and are part of the global matrix.

if I've understood you correctly, your suggestion is to determine the initialize position (in the c'tor) in the following way :
Pos = (0,10.0,5.0) * rotateMatOf(phi -90) * rotateMatOf(theta -10);
instead of the current way of :
pos = new Vector(0,10.0,5.0);

the question remains, how this can be done ? how can i define the rotation matrix ?

thanks again,

Bullet::Bullet(GLfloat zx, GLfloat zy)
{
direction = new Vector(0,0,0);
pos = new Vector(0,10.0,5.0);

ph = 2 * PI * (90 - zy) / 360;
th = 2 * PI * zx / 360;

direction->SetX(0);
direction->SetY(1);
direction->SetZ(sin(th));
}

void Bullet::Draw()
{
glPushMatrix();

glRotatef(((ph*360)/(2*3.14))-90, 0, 0, 1);
glRotatef(((th*360)/(2*3.14))-10, 1, 0, 0);

glTranslatef(pos->GetX(), pos->GetY(), pos->GetZ());
for (float angle = 0; angle < 360; angle += 10)
{
// draw a 10 degree triangle
glBegin(GL_TRIANGLES);
glColor4f(0.2, 0.6, 0.2, 0.2);
glVertex2f(0, 0);
glVertex2f(0.5, 0);
glVertex2f(0.4924, 0.1736);
glEnd();

// rotate
glRotatef(10, 0, 0, 1);
}
glEnd();
glPopMatrix();
}

void Bullet::updatePosition()
{
pos->SetX(pos->GetX() + direction->GetX());
pos->SetZ(pos->GetZ() + direction->GetZ());
pos->SetY(pos->GetY() + direction->GetY());
}

##### Share on other sites
I'll certainly try to help you with this further if I can, but first I have a couple of suggestions:

1. Edit your post and put your code in [ source ][ /source ] tags, which should make it easier to read.

2. Get rid of all the 'magic constants' and related expressions, and replace them with named functions such as DegToRad(). This will make your code much clearer and easier for us to read and follow (as well as making it easier for you to debug and maintain).

If you can clean up the code a bit, I'll be happy to take another look and see if I can make some suggestions (assuming someone else doesn't answer the question first).

##### Share on other sites
no problem, i hope that now it's more readable. i'm actually used to work in matlab and not C++ :-).

Bullet::Bullet(GLfloat zx, GLfloat zy)
{
ph = zx; th = zy; // these 2 parameters zx,zy comes from cannon class
direction = new Vector(0,0,0);
pos = new Vector(0,10.0,5.0); // set the bullet location according to the init viewpoint location (without any changes/rotations)

// setting the bullet direction/speed
direction->SetX(0);
direction->SetY(1);
}

void Bullet::Draw()
{
glPushMatrix();

glRotatef(ph-90, 0, 0, 1); // here i rotate to the viewpoint location
glRotatef(th-10, 1, 0, 0);

// progression of the bullet
glTranslatef(pos->GetX(), pos->GetY(), pos->GetZ());

// this loop is just for creation of the bullet out of 10 triangles with utual vertex.

for (float angle = 0; angle < 360; angle += 10)
{
// draw a 10 degree triangle
glBegin(GL_TRIANGLES);
glColor4f(0.2, 0.6, 0.2, 0.2);
glVertex2f(0, 0);
glVertex2f(0.5, 0);
glVertex2f(0.4924, 0.1736);
glEnd();

// rotate
glRotatef(10, 0, 0, 1);
}

glEnd();
glPopMatrix();
}

void Bullet::updatePosition()
{
pos->SetX(pos->GetX() + direction->GetX());
pos->SetZ(pos->GetZ() + direction->GetZ());
pos->SetY(pos->GetY() + direction->GetY());
}

thanks Alot,

##### Share on other sites
Quote:
 Original post by Adam81no problem, i hope that now it's more readable.
Sure, I'll try to take a look soon and see if I can spot anything.

Oh, and by the way, what I meant by 'source tags' was to enclose the code in the following tags:

[source]
[/source]

This will (in most cases at least) preserve the formatting of the code, rather than leaving it left-aligned as it is now. For me at least this makes it much easier to read code (without any indentation it's very difficult to follow the logical flow).

##### Share on other sites
I have a couple more questions for you:

1. Is it your intention that the initial position of the projectile be the same as the position of the cannon, or is it supposed to be offset from the cannon position?

2. Do you have a matrix class of any sort available? Or is the Vector class in your example all you have to work with?

3. What programming language are you using?

##### Share on other sites

Q1: Is it your intention that the initial position of the projectile be the same as the position of the cannon, or is it supposed to be offset from the cannon position?

A: yes, the initial bullet location is exactly as the cannon location which represented by the viewpoint (the viewpoint appear constantly at the middle of the screen)

Q2: Do you have a matrix class of any sort available? Or is the Vector class in your example all you have to work with?

A2: i have only Vector class which i implemented for my needs so far. it is basically capable of doing normalize, find_normal for 2 vectors using vector product and more... i can add additional methods if needed.

Q3: What programming language are you using?
A3: C++

##### Share on other sites
Quote:
 Original post by Adam81Q3: What programming language are you using?A3: C++
In that case, the following lines of code seem somewhat suspect:
direction = new Vector(0,0,0);pos = new Vector(0,10.0,5.0);
I assume 'direction' and 'pos' are of type Vector*, otherwise this code wouldn't compile. But why are you allocating simple vector objects dynamically? And are you deleting them at some point?

As for the particular problem in question, I'm not entirely clear on what your code is supposed to be doing. I'll mention one more time though that you should edit your post and place the code excerpt in [ source ][ /source ] tags. In this case it wouldn't be too difficult or time-consuming to dig through the code as is, or to make a cleaned-up copy of it, but taking the time to format your code correctly will likely be viewed as a 'good-faith effort', so to speak, and will make people more likely to help out.

If you're still not clear on how to add the tags, post back here and someone can probably guide you through it (it's very easy to do).