Local transform?

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Local transform?

Post 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.
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Post by darkhog »

Car will use Y/X axis. Y for normal turns and X for slopes if I figure out how to make them.
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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 :wink:

K
Last edited by Kjell on Mon Sep 10, 2012 11:17 pm, edited 1 time in total.
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Post 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?
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
Attachments
Direction.zgeproj
(2.55 KiB) Downloaded 407 times
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Post 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.
Attachments
car.zgeproj
(14.45 KiB) Downloaded 397 times
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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 :wink:

K
darkhog
Posts: 58
Joined: Sun Sep 09, 2012 7:59 pm

Post by darkhog »

but car needs to speed up, it can't have constant velocity like rocket in your demo. How do I solve that?
User avatar
Kjell
Posts: 1883
Joined: Sat Feb 23, 2008 11:15 pm

Post 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
Attachments
Direction.zgeproj
(3.22 KiB) Downloaded 414 times
Post Reply