Calculate angle between a player and an object?
by Squall · in Torque 3D Beginner · 08/25/2010 (8:07 am) · 3 replies
Hi everyone,
I have a player and an object, with the "getTransform" function (used on player), i can get the "pos X, pos Y, pos Z, rot X, rot Y, rotZ , ANGLE".And i know the position of the object.
May i ask is there any function in Torque to calculate the angle (called K) between the player with current facing direction and the object so that I can rotate the player (using yaw, pitch...) by K and moveforward to hit the object?.
Thank you :)
I have a player and an object, with the "getTransform" function (used on player), i can get the "pos X, pos Y, pos Z, rot X, rot Y, rotZ , ANGLE".And i know the position of the object.
May i ask is there any function in Torque to calculate the angle (called K) between the player with current facing direction and the object so that I can rotate the player (using yaw, pitch...) by K and moveforward to hit the object?.
Thank you :)
Associate Michael Hall
Distracted...
$Pi = 3.14159; $TwoPi = 6.28319; // This lil function generates the rotation required for an object at PosOne to // point at PosTwo (z rot only) function pointToXYPos(%posOne, %posTwo) { %vec = VectorSub(%posOne, %posTwo); //get the angle %rotAngleZ = mATan( firstWord(%vec), getWord(%vec, 1) ); //add pi to the angle %rotAngleZ += $Pi; //make this rotation a proper torque game value, anything more than 240 degrees is negative if(%rotAngleZ > 4.18879) { //the rotation scale is seldom negative, instead make the axis value negative %modifier = -1; //subtract 2pi from the value, then make sure its positive %rotAngleZ = mAbs(%rotAngleZ - $TwoPi); //sigh, if only this were all true } else %modifier = 1; //assemble the rotation and send it back return "0 0" SPC %modifier SPC %rotAngleZ; } // And this lil function generates the rotation required for an object at posOne to // point at PosTwo for X, Y And Z axis. function pointToPos(%posOne, %posTwo) { //sub the two positions so we get a vector pointing from the origin in the direction we want our object to face %vec = VectorSub(%posTwo, %posOne); // pull the values out of the vector %x = firstWord(%vec); %y = getWord(%vec, 1); %z = getWord(%vec, 2); //this finds the distance from origin to our point %len = vectorLen(%vec); //---------X----------------- //given the rise and length of our vector this will give us the angle in radians %rotAngleX = mATan( %z, %len ); //---------Z----------------- //get the angle for the z axis %rotAngleZ = mATan( %x, %y ); //create 2 matrices, one for the z rotation, the other for the x rotation %matrix = MatrixCreateFromEuler("0 0" SPC %rotAngleZ * -1); %matrix2 = MatrixCreateFromEuler(%rotAngleX SPC "0 0"); //now multiply them together so we end up with the rotation we want %finalMat = MatrixMultiply(%matrix, %matrix2); //we're done, send the proper numbers back return getWords(%finalMat, 3, 6); }