Page 1 of 1

# Getting a relative position from a rotation object?(Math)

Posted 22 January 2013 - 03:26 PM

Im only in Honors Algebra II right now and have no idea how to do this. Basically I have a character that rotates with the mouse. I need the position that is relatively to the left and a little up from the character.

I already have the rotation angle if it helps. Also, here is a bad visual reprensentation to further clarify.

C is the character, o is the position im trying to get. Each C is rotating 90 degrees clockwise. As you can see, the o spins with the C from a distance. I need a formula to get the position of the o.

```o         o
C     C       C       C
o   o

```

Is This A Good Question/Topic? 0

## Replies To: Getting a relative position from a rotation object?(Math)

### #2 Aphex19 Reputation: 619
• Posts: 1,873
• Joined: 02-August 09

## Re: Getting a relative position from a rotation object?(Math)

Posted 22 January 2013 - 04:07 PM

So, you represent the rotation as a Euler angle (0-360 degrees) in 2D space (i'm imagining your rotation as rotation around the Y axis from a birds-eye perspective)? If so, you could convert your Euler angle to the 'forward vector' and then find the vector perpendicular to that one, and average the two. That vector should then be pointing diagonally to C.

Another way would be to just find the forward vector of the angle + 45 degrees. That would probably save a lot of hassle.

Also, is this a C or C++ problem? How come you posted in this sub-forum?

This post has been edited by Aphex19: 22 January 2013 - 04:16 PM

## Re: Getting a relative position from a rotation object?(Math)

Posted 22 January 2013 - 04:12 PM

Uhh sorry i wasnt sure where to post it. Here is the code im using (c++).

```// Set Rotation
float desiredAngle = (180/3.1415927) * atan2f(-deltaX, deltaY);
Body->SetTransform( Body->GetPosition(), desiredAngle );
sprite.setRotation( Body->GetAngle());

sf::Vector2f D;
D.x = (float)b_p.x - sf::Mouse::getPosition(window).x;
D.y = (float)b_p.y - sf::Mouse::getPosition(window).y;

float DLen = sqrt( D.x*D.x + D.y*D.y);
D.x /= DLen;
D.y /= DLen;

// Determine position to shoot from( left and up from the player )
sf::Vector2f bPos = sprite.getPosition();
bPos.x += D.x;
bPos.y += D.y;

```

The code using sf::Vector2f D is not originally mine, so im not sure if that is the direction vector you are referring to? It probably isnt, so how can i convert the Eurler angle( desiredAngle) into the direction vector?

### #4 #define Reputation: 1862
• Posts: 6,710
• Joined: 19-February 09

## Re: Getting a relative position from a rotation object?(Math)

Posted 22 January 2013 - 04:21 PM

You can use sin and cos to get x and y (relative to C).

Converting between polar and Cartesian coordinates :-

Polar coordinate system

### #5 Aphex19 Reputation: 619
• Posts: 1,873
• Joined: 02-August 09

## Re: Getting a relative position from a rotation object?(Math)

Posted 22 January 2013 - 04:28 PM

Assuming your angle is in degrees and x is horizontal and y is up. I think that the direction vector would be...

```vx = sin(angle)
vy = cos(angle)
```

edit.
#define's post just got straight to the point.

This post has been edited by Aphex19: 22 January 2013 - 05:16 PM

## Re: Getting a relative position from a rotation object?(Math)

Posted 23 January 2013 - 03:33 PM

Im sorry but im still confused. Okay, so desiredAngle is the rotation of the sprite(the sprite rotates towards the mouse). Straight down is 0 and directly right is -90. Here is the code i tried:

```bPos.x = sin(desiredAngle);
bPos.y = cos(desiredAngle);

```

No bullets appear with this code though. I am guessing that the coordinate the bullet is starting from (bPos.x, bPos.y) is completely off the screen.

I dont know how to fix this because i dont really understand how sin and cos are getting a point. I though they were used to find the lengths of sides?(im in 10th grade math sorry)

### #7 #define Reputation: 1862
• Posts: 6,710
• Joined: 19-February 09

## Re: Getting a relative position from a rotation object?(Math)

Posted 23 January 2013 - 10:16 PM

The bullet is radius r from the character C. C is at position x and y, the bullet is dx and dy from C. The bullets position is therefore (x + dx) and (y + dy).

When the bullet is directly right of the character the angle theta is 0.
When the bullet is directly above the character the angle theta is 90 degrees (1.57 radians).
When the bullet is directly left of the character the angle theta is 180 (3.14 radians).
When the bullet is directly below the character the angle theta is 270 degrees ( 4.71 (or -1.57) radians).

The C functions sin and cos accept an angles in radians. sin

The x component is found using cos, y component sin.

Polar and Cartesian Coordinates

```    // to convert from Polar Coordinates (r,θ)
// to Cartesian Coordinates (x,y) :

dx = r * cos( θ );
dy = r * sin( θ );

```

```double radians( double degrees )
{
return ( degrees * PI / 180 );
}

```

## Re: Getting a relative position from a rotation object?(Math)

Posted 23 January 2013 - 11:24 PM

Please look into vectors and rotation matrices. All part of linear algebra.

## Re: Getting a relative position from a rotation object?(Math)

Posted 26 January 2013 - 12:14 PM

I still haven't figured this out />. I think i understand the concept but i cant figure out the details.

i THINK the code should be:
```// bulletPos.x = radius * sin( offset (operator?) desiredAngle) + playerX
bPos.x = 40 * sin( (40.0 * DEGTORAD) (something) dsiredAngle* DEGTORAD) + xPos;

```

As you can see i am missing something. I would really appreciate it if someone could explain how to do this. Am i even on the right track. I made a picture to make it easier to see what im trying to do. I am looking for bx and by. ### #10 #define Reputation: 1862
• Posts: 6,710
• Joined: 19-February 09

## Re: Getting a relative position from a rotation object?(Math)

Posted 26 January 2013 - 05:39 PM

The top left of the screen is 0,0 I take it.

So x and y increase like so.
```x--->
y
|
v
```

If we invert the screen
```    ^
|
y
<---x
```

If we add you angles, the angles go counter-clockwise and zero is at the top.
```.
0
|
270 [email protected] 90
|
180

^
|
y
.         <---x
```

If we rotate the angles clockwise, this fits with the axes, imagine looking from behind the screen.

```.
90
|
0  [email protected] 180
|
270

^
|
y
.         <---x
```

So I ran up some code just using the console to check.

```#include <iostream>
#include <cmath>

using namespace std;

#define ROWS     20
#define COLS     20
#define FIELDCH  '.'
#define TRACECH  ' '
#define BULLETCH 'o'

#define PI 3.14
#define DEG_RAD(degrees)  (degrees * PI / 180)

void init_array(char screen[ROWS][COLS])
{
for(int r=0; r<ROWS; r++) {
for(int c=0; c<COLS; c++) {
screen[r][c] = FIELDCH;
}
}
}

void display_array(char screen[ROWS][COLS])
{
for(int r=0; r<ROWS; r++) {
for(int c=0; c<COLS; c++) {
// add a character to square screen
cout << screen[r][c] << FIELDCH;
}
cout << endl;
}
}

// using double for theta here
int calculate_bx(double theta)
{
// adding 90 degrees to angle
theta += 90;
}

int calculate_by(double theta)
{
theta += 90;
}

bool display_bullet(char screen[ROWS][COLS], int x, int y, int theta)
{
int  by, bx;

if(theta == 360)
return false;

bx = calculate_bx(theta);
by = calculate_by(theta);
screen[y+by][x+bx] = BULLETCH;
display_array(screen);
screen[y+by][x+bx] = TRACECH;

return true;
}

int main()
{
char screen[ROWS][COLS];
int  y = ROWS/2, x = COLS/2;
int  theta;

init_array(screen);
screen[y][x] = '@';

do
{
cout << endl;
cout << "Enter angle (360 = exit) ";
cin  >> theta;

} while( display_bullet(screen, x, y, theta) );

// cout << "Press enter to continue. " << endl;
cin.ignore();
//cin.get();
return(0);
}

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }