calculating 3D object's roll angle relative to jaw angle
by Slavisa Gotovnik · in Torque Game Builder · 05/28/2013 (5:26 pm) · 2 replies
HI!
Im relatively noob to 3d objects in Torque 2D and i've been smashing my brains allover the keyboard most of the night with my planes model roll function. Since ive been lazy in school with math im not so good at it.
Im using %this.setShapeRotation(180, 0, %curentAngle); to jaw my plane model whenever i press left or right button on my keyboard. That all works fine now i want my model to roll about 5 degrees to each side repectively.
Lets say i press left button.
So i wonder what about angles in between 0, 90, 180, 270. Is there any neat function in t2d that can calculate
angles on the fly or i must program my own function with loads of if else statements. I really would like to avoid that.
It seems i was lazy with english too so i cant type really good :p
I hope someone understands me what im trying to acheve here if not tough luck for me.
REGARDS!
Im relatively noob to 3d objects in Torque 2D and i've been smashing my brains allover the keyboard most of the night with my planes model roll function. Since ive been lazy in school with math im not so good at it.
Im using %this.setShapeRotation(180, 0, %curentAngle); to jaw my plane model whenever i press left or right button on my keyboard. That all works fine now i want my model to roll about 5 degrees to each side repectively.
Lets say i press left button.
If planes z axis angle is 0 i change y axis --> setShapeRotation(180, -5, %curentAngle). If planes z axis angle is 90 i change x axis --> setShapeRotation(185, 0, %curentAngle). If planes z axis angle is 180 i change y axis --> setShapeRotation(180, 5, %curentAngle). If planes z axis angle is 270 i change x axis --> setShapeRotation(175, 0, %curentAngle).
So i wonder what about angles in between 0, 90, 180, 270. Is there any neat function in t2d that can calculate
angles on the fly or i must program my own function with loads of if else statements. I really would like to avoid that.
It seems i was lazy with english too so i cant type really good :p
I hope someone understands me what im trying to acheve here if not tough luck for me.
REGARDS!
#2
Note that i didnot update the code for right direction.
This actualy work in my project but looks gimpy when plane is facing any angle between 0, 90, 180, 270. I hope you understand this little more. Any math genius in the house?
THANKS!
05/29/2013 (1:36 pm)
Hi and ty for your reply. I just came from work and scripted this code for you to understand my math dilema (i was so sleepy last night i wrote jibberish things i can hardly understand it myself and i wrote the damn post xD). Essentialy i just need math formula to calculate angles.Note that i didnot update the code for right direction.
function playerClass::rotating(%this, %curentAngle, %direction, %rotateSpeed) {
// 0 for left
if (%direction == 0) {
// if we hold left key
if (%this.moveLeft) {
if (isEventPending(%this.playerRotatingSchedule)) {
cancel(%this.playerRotatingSchedule);
}
// +1 degree to curent z angle (z = planes jaw)
%curentAngle++;
// update planes z angle (jaw) and Y angle (roll)
// this all goes in one line but you will easy understand how
// i want to get it to work
%valX = getWord(%this.rollAngleCalculation(%curentAngle), 0);
%valY = getWord(%this.rollAngleCalculation(%curentAngle), 1);
%curentAngle = getWord(%this.rollAngleCalculation(%curentAngle), 2);
echo("X " @ %valX @ " Y " @ %valY @ " Z " @ %curentAngle);
%this.setShapeRotation(%valX, %valY, %curentAngle);
// if plane is moving forward update polar velocity so it will move to planes facing
if ($playerCurentSpeed != 0) {
%curentAngle = radToDeg(getWord(%this.getShapeRotation(), 1));
%this.setLinearVelocityPolar(-%curentAngle, $playerCurentSpeed);
}
%this.playerRotatingSchedule = %this.schedule(%rotateSpeed, "rotating", %curentAngle, %direction, %rotateSpeed);
}
else {
%this.setShapeRotation(180, 0, %curentAngle);
cancel(%this.playerRotatingSchedule);
}
}
// 1 for right everything else is the same as above
if (%direction == 1) {
if (%this.moveright) {
if (isEventPending(%this.playerRotatingSchedule)) {
cancel(%this.playerRotatingSchedule);
}
%curentAngle--;
%this.setShapeRotation(180, 0, %curentAngle);
if ($playerCurentSpeed != 0) {
%curentAngle = radToDeg(getWord(%this.getShapeRotation(), 1));
%this.setLinearVelocityPolar(-%curentAngle, $playerCurentSpeed);
}
%this.playerRotatingSchedule = %this.schedule(%rotateSpeed, "rotating", %curentAngle, %direction, %rotateSpeed);
}
else {
echo("angle : " @ %curentAngle);
cancel(%this.playerRotatingSchedule);
}
}
}
// function to calculate roll angle (mainly Y axis)
// since the X, Y, Z axis dont rotate with 3d object
// when we use %this.setShapeRotation(180, 0, %curentAngle)
// we need to calculate (virtual Y axis if you will)
// so the plane will roll to the left or right for 5 degrees no
// mather wich direction is facing. We need to update X and Y axis for every schedule.
function playerClass::rollAngleCalculation(%this, %curentZAngle) {
%retValX = 180; // needs to be 180 if not plane will fly upside down :p
%retValY = 0;
// we can do loads of if else statements like
if (%curentZAngle >= 0 && %curentZAngle < 90) {
%retValX = 180;
%retValY = -5;
}
if (%curentZAngle >= 90 && %curentZAngle < 180) {
%retValX = 185;
%retValY = 0;
}
if (%curentZAngle >= 180 && %curentZAngle < 270) {
%retValX = 180;
%retValY = 5;
}
if (%curentZAngle >= 270) {
%retValX = 175;
%retValY = 0;
if (%curentZAngle == 360) {
// reset Y to zero or it will just keep adding to the value to infinity
// in schedule
%curentZAngle = 0;
}
}
// thats all fine if my plane can fly only up, down, left and right
// (north, south, east and west).
// But plane is able to fly in all directions. I can code
// for 45(north west), 135(south west) , 225 (sout east) and
// 315 (north east) the same way i did above. I just devide 5
// by 2 and add or substract 2.5 to X and Y
return %retValX SPC %retValY SPC %curentZAngle;
}This actualy work in my project but looks gimpy when plane is facing any angle between 0, 90, 180, 270. I hope you understand this little more. Any math genius in the house?
THANKS!
Associate Simon Love
Are you looking for something along the lines of
?
Moved to Torque Game Builder forums.