• 13
• 18
• 19
• 27
• 10

# 2D Rotation of Two Parallel Lines For Shooting Game

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

## Recommended Posts

Hi All, I am programming a shooting game having a gun base at the bottom. It moves upto 165 degrees to the left and 15 degrees to the right maximum. Figure 1 shows what i exactly need it. Figure 2 shows the two lines involved in the making of the gun. I am having trouble in moving both the parallel lines rotated at the centre of rotation, which is shown as black circle (between the parallel lines). i know how to move at various angles had it been only one single line. I have the following formula: x1 = x * cos(angle) - y * sin (angle); y1 = x * sin(angle) + y * cos (angle); Please show me how both the lines can be rotated using the above code so that it work like figure 1. Here is the link of Figures: http://img2.freeimagehosting.net/image.php?fd86dbece5.jpg Regards, Pramod Kumar New Delhi, India

##### Share on other sites
You'll likely want to do manipulate the vertices with a matrix. In figure 2, we would call these point coordinates relative to the black dot "model space." To compute the shape in figure 1 (world space), we multiply each vertex in model space by a matrix which will transform us into the desired rotated space.

Pseudo code:
Matrix m;        //transformation matrixVertex verts[4]; //original points, figure 2                 //relative to black dotVertex outVs[4]; //output points, draw thesem.SetRotation( angle );m.SetPosition( gunPosition );//transform each point by "m"for(int i = 0; i < 4; ++i){  outVs = m * verts;}

If you're using opengl this is known as the modelview matrix. Using the model view matrix will automatically transform every vertex you pass to it, so the loop is unnecessary. Right before you draw the gun, you set the modelview matrix to equal "m" then you draw your gun and it will be transformed by "m".

##### Share on other sites
Hi bzroom,

i am actually programming in Turbo C for DOS. I am actually comfortable with it. All the required graphics routines are written to draw directly to the video memory hence i does not use opengl or any window based graphic libraries. I just program for fun.

Now instead of matrix can the same be done using the given formula itself? Is there any way? Because i am not comfortable with it!.
x1 = x * cos(angle) - y * sin (angle);
y1 = x * sin(angle) + y * cos (angle);

If it can only be done through matrix then please show me how to calculate these 4 vertices using matrix in C.

Regards,

Pramod Kumar
New Delhi, India

##### Share on other sites
To do what you're trying, you need to rotate the points (using your formula) around the center point (the center of the black dot). To do that, you just need to change the endpoints so that they're all relative to the black dot.

Basically, if you want to rotate line1 (which is x1,y1 -> x2,y2), you need to change the endpoints like so (assuming xC,yC is the center of the circle):

x1' = x1 - xC
y1' = y1 - yC
x2' = x2 - xC
y2' = y2 - yC

Then rotate (x1',y1') and (x2',y2') using the sin/cos math. If you need to get things back into absolute coordinates again, then undo the first step, by re-adding (xC,yC).

Put another way... The rotation equation that you gave only works for things rotating directly around the origin (0,0). So slide the line until the center of rotation is the origin, do the rotation, and then slide it back.

##### Share on other sites
Hi Osmanb,

Nice explanation.. Just going ahead with it. Lets have an example if:

//Centre Of Rotation
XC = 150;
YC = 100;

x1 = 100;
y1 = 150;

x2 = 100;
y2 = 50;

Therefore;
x1' = 100 - 150;
y1' = 150 - 100;
x2' = 100 - 150;
y2' = 50 - 100

Hence;
x1' = -50;
y1' = 50;
x2' = -50;
y2' = -50;

so (-50,50,-50,-50) works out as the NEW ORIGIN for rotation. But how this is relative to the black dot at (150,100)? Is this origin valid!!!! Sorry i am weak in math.

##### Share on other sites
If you imagine that the black dot at (150,100) was the origin, then those new numbers are the actual coordinates for the end of your line. Now, if you want to rotate the line by some amount, you do the sin/cos math on the numbers you've calculated. Let's say theta is 90 degrees, so sin = 1 and cos = 0. (I'm lazy).

x1' = -50;
y1' = 50;
x2' = -50;
y2' = -50;

Rotating, we get:

x1'' = (-50 * 0) - (50 * 1) = -50
y1'' = (-50 * 1) + (50 * 0) = -50
x2'' = (-50 * 0) - (-50 * 1) = 50
y2'' = (-50 * 1) + (-50 * 0) = -50

So now our line is (-50,-50) to (50,-50). But that's still relative to the "fake" origin -- the black dot at (150,100). So we can convert the coordinates back into the full screen coordinates by re-adding the dot's position:

(-50,-50) + (150,100) = (100,50)
( 50,-50) + (150,100) = (200,50)

The final coordinates for the rotated line are (100,50) and (200,50). This process should work for any rotation, and if you rotate multiple parallel lines, they should remain parallel.