Page 1 of 1
Local transform?
Posted: Mon Sep 10, 2012 9:33 pm
by darkhog
How can I move object taking rotation into account (i.e. local transform)? Because now, when my car is rotated it drives sideways. My code for car movement:
Code: Select all
CurrentModel.Velocity.Z-=movspd*App.DeltaTime;
There is nothing like LocalVelocity, etc. here so I wonder how I can make it take rotation into account.
Posted: Mon Sep 10, 2012 9:48 pm
by Kjell
Yea, this is something i suggested / requested years ago and still think is a significant missing feature. Can you tell me how many rotation axes your car is using so I can give you the best performing solution for your needs?
K
Posted: Mon Sep 10, 2012 10:06 pm
by darkhog
Car will use Y/X axis. Y for normal turns and X for slopes if I figure out how to make them.
Posted: Mon Sep 10, 2012 10:34 pm
by Kjell
Hi darkhog,
To get the direction vector based on XY rotation use the following function.
Code: Select all
void GetDirFromEulerXY(float X, float Y, ref float DX, ref float DY, ref float DZ)
{
X *= PI * 2;
Y *= PI * 2;
float CX = cos(X);
DX = CX * sin(Y);
DY = 0 - sin(X);
DZ = CX * cos(Y);
}
And then call the function like this ( the result will be stored into the DX / DY / DZ variables ).
Code: Select all
float DX, DY, DZ;
GetDirFromEulerXY(Car.Rotation.X, Car.Rotation.Y, DX, DY, DZ);
By the way, if you're going to use for instance the Bullet Physics extension, you won't have to calculate these things yourself
K
Posted: Mon Sep 10, 2012 10:59 pm
by darkhog
But I don't know how! I really tried to understand demo project on Google Code page for it, but there are lot of weird code that I can't comprehend true form of (bonus points if you get the reference).
Anyway thanks for code.
//edit: How do I use it to set speed around, e.g. Local Z? As in Velocity?
Posted: Mon Sep 10, 2012 11:31 pm
by Kjell
Hi darkhog,
darkhog wrote:How do I use it to set speed around, e.g. Local Z? As in Velocity?
Attached is a simple example ( use the arrow keys to control )
K
Posted: Tue Sep 11, 2012 12:43 am
by darkhog
Tried, but for some reason it doesn't work with my car (spawned rotated car), but only work if car isn't rotated. Probably I am doing something wrong, so attaching project file.
Posted: Tue Sep 11, 2012 8:36 am
by Kjell
Hi darkhog,
You're adding to the velocity in your W/S KeyPress events instead of assigning the direction vector. A car only moves into the direction its wheels are facing ( drifting aside ), it's not a hot-air balloon
K
Posted: Tue Sep 11, 2012 9:15 am
by darkhog
but car needs to speed up, it can't have constant velocity like rocket in your demo. How do I solve that?
Posted: Tue Sep 11, 2012 9:49 am
by Kjell
Hi darkhog,
Use a variable to hold the speed and multiply that value with the direction vector. Attached is a updated version of my example. Same controls as before, but now you can control the speed with W / S.
K