Not Understanding Why my VEX code is Working

   1826   0   0
User Avatar
Member
2036 posts
Joined: Sept. 2015
Offline
Hello,

Ok, so instead of my code not working, it is; But I don't understand why.

The code below that I am working on was an excercise for some other code I am working on and I needed to do what I have below to get a better understanding of how to structure my code for that project using VEX.

The gif I've attached shows a square of 4 points rotating and a line drawn from one corner to another point.

In my vex I take the point of that corner(which is the first point of the line) and make a “copy” of it.

I then rotate that copy in the opposite direction in an angle amount equivalent to what the originating object was before it was rotated.

I do this so that my second point gets created with an offset of x and y always related to how the first corner point of the “square” sits when the object is at 0 degrees.

I then rotate both the copied point and the new offset point by the amount of same degrees as what the original object made and in the same direction.

The gif gives the impression that the one corner ( upper right at beggining of clip ) is only one point. But it is actually one point on top of another ( the copied ) one.

The thing is that in my code when I first wrote it I was using a “counter” angle to get it back to original position, created the second point from that position, and then rotated it back ( opposite the “counter” angle by same amount) - And found it did not move.

I only got it too work when I doubled the last rotation ( I tried it on a whim just to see what would happen ).

I don't understand why it works “perfectly” now.

It's like, as an example, my object rotated counterclockwise 45 degrees.

I then rotated by 45 degrees in the clockwise direction and did my work on the points.

I then for the final result rotated counterclockwise 90 degrees. ( I doubled my angle variable amount ).

I would expect that I would only of had to rotate it back in the counterclockwise direction 45 degrees.

I suspect I am going to feel silly and it's something that I should have noticed that is staring me in the face, but I can't see why it's working as is below.

The line of code that is making me scratch my head has a commented out line just above and below it.

Anyone care to “Set me Straight”?

Thanks.



vector Pt_0_Vec;
vector Pt_1_Vec;

vector Pt_0_Vec_Initial;

int Line_Prim;

int Pt_0_Import;
int Pt_1_Import;

int Pt_0_Int;
int Pt_1_Int;

int Vertex_0;
int Vertex_1;

matrix3 m;
vector axis;

float imported_angle;

float input_original_angle;
float input_counter_angle;

float Angle_Original;
float Angle_Counter;

imported_angle = ch("/obj/Sources/transform1/ry");

input_original_angle = imported_angle;
input_counter_angle = imported_angle * (-1);

printf("angle is %g\n", input_original_angle);
printf("angle is %g\n", input_counter_angle);


Line_Prim = addprim(0, "polyline" );

m = ident();
axis = {0,1,0};
Angle_Original = radians(input_original_angle);
Angle_Counter = radians(input_counter_angle);

Pt_0_Vec_Initial = point( @OpInput1, "P", 1);
Pt_0_Vec = Pt_0_Vec_Initial;

rotate(m, Angle_Counter, axis);
Pt_0_Vec *= m;

Pt_1_Vec.x = Pt_0_Vec.x + 1.5;
Pt_1_Vec.y = Pt_0_Vec.y;
Pt_1_Vec.z = Pt_0_Vec.z - 1.5;

//****** Why I need to multiply "Angle_Original" by 2 instead of just using it's value determined earlier

rotate(m, (Angle_Original*2), axis);

//*******************************************************************************************************

Pt_0_Vec *= m;
Pt_1_Vec *= m;

Pt_0_Int = addpoint(0, Pt_0_Vec);
Pt_1_Int = addpoint(0, Pt_1_Vec);

Vertex_0 = addvertex( 0, Line_Prim, Pt_0_Int );
Vertex_1 = addvertex( 0, Line_Prim, Pt_1_Int );
Edited by BabaJ - July 30, 2016 16:36:17

Attachments:
Vex Rotation.gif (405.1 KB)

  • Quick Links