All topics about ZGameEditor goes here.
Moderator: Moderators
Ats
Posts: 764 Joined: Fri Sep 28, 2012 10:05 am
Contact:
Post
by Ats » Mon Jun 20, 2022 12:13 pm
I'm trying to "aim" from one vec3 to another, using a rotation vector (yaw, pitch and roll). But I don't get good results...
Where do I mess up?
Original rotation of the aim?
Calculation of the angles?
Problem with degree/radian/cycles?
Code: Select all
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="-23.4758 4 18.6785" CameraRotation="0 41.143 0" FileVersion="2">
<OnUpdate>
<ZExpression>
<Expression>
<![CDATA[vec3 cubepos = vector3(10,0,5);
vec3 linepos = vector3(2,0,1);
transformCube.Translate = cubepos;
transformLine.Translate = linepos;
// Transform.Rotate / Rotation = Unit of rotation is cycles. one cycle = 360 degrees = 2PI
float yaw = atan2(cubepos.X - linepos.X, cubepos.Z - linepos.Z) / 2*PI;
float pitch = atan2(cubepos.Z - linepos.Z, cubepos.Y - linepos.Y) / 2*PI;
float roll = atan2(cubepos.Y - linepos.Y, cubepos.X - linepos.X) / 2*PI;
vec3 rot = vector3(yaw, pitch, roll);
transformLine.Rotate = rot;
// rotate camera
float t = App.DeltaTime;
t = App.Time / 20;
App.CameraPosition.Y = 4;
App.CameraPosition.X = cos(t * PI*2) * 30;
App.CameraPosition.Z = sin(t * PI*2) * 30;
App.CameraRotation.Y = t - 0.25;]]>
</Expression>
</ZExpression>
</OnUpdate>
<OnRender>
<RenderTransformGroup Name="transformCube" Translate="10 0 5">
<Children>
<RenderSetColor Color="1 1 0 1"/>
<RenderMesh Mesh="CubeMesh"/>
</Children>
</RenderTransformGroup>
<RenderTransformGroup Name="transformLine" Translate="2 0 1" Rotate="0 2.4674 0">
<Children>
<RenderSetColor Color="0 1 1 1"/>
<RenderMesh Mesh="LineMesh"/>
</Children>
</RenderTransformGroup>
<RenderSetColor Color="1 0 0.502 1"/>
<RenderMesh Mesh="AxesMesh"/>
</OnRender>
<Content>
<Mesh Name="CubeMesh">
<Producers>
<MeshBox Scale="0.2 0.2 0.2"/>
</Producers>
</Mesh>
<Mesh Name="LineMesh">
<Producers>
<MeshBox Scale="0.05 0.05 100"/>
<MeshTransform Position="0 0 99.5"/>
<MeshBox Scale="0.05 0.4 0.05"/>
<MeshCombine/>
<MeshBox Scale="0.4 0.05 0.05"/>
<MeshCombine/>
</Producers>
</Mesh>
<Mesh Name="AxesMesh">
<Producers>
<MeshBox Scale="0.02 0.02 100"/>
<MeshBox Scale="0.02 100 0.02"/>
<MeshCombine/>
<MeshBox Scale="100 0.02 0.02"/>
<MeshCombine/>
</Producers>
</Mesh>
</Content>
</ZApplication>
Kjell
Posts: 1915 Joined: Sat Feb 23, 2008 11:15 pm
Post
by Kjell » Mon Jun 20, 2022 1:09 pm
Hi Ats,
Ats wrote: ↑ Mon Jun 20, 2022 12:13 pm Where do I mess up?
It depends a little on your specific situation and the type of result you're after ... but let's say you want a floor-mounted turret to track a target then you only need pitch & yaw. Here's a example:
Code: Select all
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="0 2 16" LightPosition="0.2 1 0.5" FileVersion="2">
<OnUpdate>
<ZExpression>
<Expression>
<![CDATA[// Cache time
float t = App.Time;
// Animate camera
App.CameraRotation.Y = t/32;
float a = App.CameraRotation.Y*PI*2;
App.CameraPosition.X = -sin(a)*16;
App.CameraPosition.Z = cos(a)*16;
// Animate box position
Box.Translate.X = cos(t)*4;
Box.Translate.Y = cos(t)*2+2;
Box.Translate.Z = sin(t)*4;
// Line look-at box
float dx, dy, dz;
dx = Box.Translate.X - Line.Translate.X;
dy = Box.Translate.Y - Line.Translate.Y;
dz = Box.Translate.Z - Line.Translate.Z;
Line.Rotate.X = -atan2(dy, sqrt(dx*dx+dz*dz))/(PI*2);
Line.Rotate.Y = atan2(dx, dz)/(PI*2);]]>
</Expression>
</ZExpression>
</OnUpdate>
<OnRender>
<UseMaterial Material="GridMaterial"/>
<RenderMesh Mesh="GridMesh"/>
<RenderTransformGroup Name="Box">
<Children>
<UseMaterial Material="BoxMaterial"/>
<RenderMesh Mesh="BoxMesh"/>
</Children>
</RenderTransformGroup>
<RenderTransformGroup Name="Line" Scale="1 1 4">
<Children>
<UseMaterial Material="LineMaterial"/>
<RenderMesh Mesh="LineMesh"/>
</Children>
</RenderTransformGroup>
</OnRender>
<Content>
<Mesh Name="BoxMesh">
<Producers>
<MeshBox Scale="0.25 0.25 0.25"/>
</Producers>
</Mesh>
<Material Name="BoxMaterial" Shading="1"/>
<Mesh Name="LineMesh">
<Producers>
<MeshBox Scale="0 1 1" Grid2DOnly="255"/>
<MeshTransform Position="0 0 1" Rotation="0.75 0 0"/>
</Producers>
</Mesh>
<Material Name="LineMaterial" Shading="2" Light="0" DrawBackFace="255"/>
<Mesh Name="GridMesh">
<Producers>
<MeshBox Scale="8 8 1" XCount="7" YCount="7" Grid2DOnly="255"/>
<MeshTransform Rotation="0.75 0 0"/>
</Producers>
</Mesh>
<Material Name="GridMaterial" Shading="3" Light="0"/>
</Content>
</ZApplication>
K
Ats
Posts: 764 Joined: Fri Sep 28, 2012 10:05 am
Contact:
Post
by Ats » Mon Jun 20, 2022 2:29 pm
Thanks a lot Kjell!
I get why there is no roll, but I don't see where the negative atan2 yaw is coming from
Code: Select all
float yaw = -atan2(dy, sqrt(dx*dx+dz*dz))/(PI*2);
I'll think about that after a little nap
Edit:
I had to adapt it a bit to my game, but now the enemy locked down is working perfectly
Code: Select all
float yaw = atan2(dy, sqrt(dx*dx+dz*dz))/(PI*2);
float pitch = 0.5 + atan2(dx, dz)/(PI*2);
Kjell
Posts: 1915 Joined: Sat Feb 23, 2008 11:15 pm
Post
by Kjell » Mon Jun 20, 2022 7:34 pm
Hi Ats,
Ats wrote: ↑ Mon Jun 20, 2022 2:29 pm I get why there is no roll, but I don't see where the negative atan2 yaw is coming from
It depends on which axis of your line / model you want to point at the target. The calculations are just simple pythagorean theorem in 2D .. how to apply the results in 3D ( rotation ) is down to your setup.
Ats wrote: ↑ Mon Jun 20, 2022 2:29 pm I had to adapt it a bit to my game, but now the enemy locked down is working perfectly
In the example you posted in this thread earlier your line is poiting towards the positive z-axis. Judging from the changes you made it looks like in your game ( for this particular "thing" ) you needed the negative z-axis
K