9 Replies - 1127 Views - Last Post: 26 January 2013 - 05:39 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

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  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

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?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,638
  • 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
Was This Post Helpful? 1
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

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)
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,638
  • 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( θ );





Radian


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


Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

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.
Was This Post Helpful? 0
  • +
  • -

#9 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

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.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,638
  • 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 -@- 90
       |
      180
   
              ^
              |
              y
.         <---x



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

.
       90
       |
   0  -@- 180
       |
      270
   
              ^
              |
              y
.         <---x



So I reckon if we add 90 degrees to your angle we will get your position.

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 RADIUS   5.0
#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;
  return RADIUS * cos( DEG_RAD(theta) );
}

int calculate_by(double theta)
{
  theta += 90;
  return RADIUS * sin( DEG_RAD(theta) );
}

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);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1