Hi jinxtengu,
jinxtengu wrote:what I'm trying to do is project a newly spawned model in the direction that the camera is facing, but I can't quite work out the correct formula to do this.
Here's a example for a FPS-esque setup ( mouse + WASD ) that uses camera rotation over the X and Y axes. The thing you want to take a look at in the source is the ZExpression in App.OnUpdate ( with comment "Check this out!" ).
Code: Select all
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="4 0.5 4" FileVersion="2">
<OnLoaded>
<ZExternalLibrary ModuleName="opengl32">
<Source>
<![CDATA[//
void glBegin(int mode){}
void glEnd(){}
void glVertex3i(int x, int y, int z){}]]>
</Source>
</ZExternalLibrary>
<ZExpression>
<Expression>
<![CDATA[//
centerMouse();]]>
</Expression>
</ZExpression>
</OnLoaded>
<OnUpdate>
<Group Comment="Input">
<Children>
<Group Comment="Axis">
<Children>
<Repeat Name="AxisRepeat" Count="2" WhileExp="//">
<OnIteration>
<ZExpression>
<Expression>
<![CDATA[//
int i = AxisRepeat.Iteration;
//
Axis[i] = 0;
//
AxisPositive.CharCode = AxisMap[i,0];
AxisNegative.CharCode = AxisMap[i,1];]]>
</Expression>
</ZExpression>
<KeyPress Name="AxisPositive" CharCode="87">
<OnPressed>
<ZExpression>
<Expression>
<![CDATA[//
Axis[AxisRepeat.Iteration]++;]]>
</Expression>
</ZExpression>
</OnPressed>
</KeyPress>
<KeyPress Name="AxisNegative" CharCode="83">
<OnPressed>
<ZExpression>
<Expression>
<![CDATA[//
Axis[AxisRepeat.Iteration]--;]]>
</Expression>
</ZExpression>
</OnPressed>
</KeyPress>
</OnIteration>
</Repeat>
</Children>
</Group>
<Group Comment="Key">
<Children>
<ZExpression>
<Expression>
<![CDATA[//
Key[1] = Key[0];
Key[0] = 0;]]>
</Expression>
</ZExpression>
<KeyPress Keys="{">
<OnPressed>
<ZExpression>
<Expression>
<![CDATA[//
Key[0]++;]]>
</Expression>
</ZExpression>
</OnPressed>
</KeyPress>
<ZExpression>
<Expression>
<![CDATA[//
Key[2] = Key[0] && !Key[1];]]>
</Expression>
</ZExpression>
</Children>
</Group>
</Children>
</Group>
<ZExpression Comment="Check this out!">
<Expression>
<![CDATA[// Control camera rotation using mouse
App.CameraRotation.X -= App.MousePosition.Y*0.25;
App.CameraRotation.Y += App.MousePosition.X*0.25;
App.CameraRotation.X = clamp(App.CameraRotation.X, -0.25, 0.25);
// Control camera position using WASD
float t, x, y;
t = App.DeltaTime;
x = App.CameraRotation.X*PI*2;
y = App.CameraRotation.Y*PI*2;
App.CameraPosition.X += (Axis[0]*cos(y)+Axis[1]*sin(y))*2*t;
App.CameraPosition.Z += (Axis[0]*sin(y)-Axis[1]*cos(y))*2*t;
// On left-mouse-button down, shoot bullet
if(Key[2])
{
// Set bullet position to camera
Bullet.Position.X = App.CameraPosition.X;
Bullet.Position.Z = App.CameraPosition.Z;
// Set bullet rotation to camera
Bullet.Rotation.X = 0-App.CameraRotation.X;
Bullet.Rotation.Y = 0-App.CameraRotation.Y;
// Set bullet velocity relative to camera rotation
Bullet.Velocity.X = sin(y)*cos(x);
Bullet.Velocity.Y = 0- sin(x);
Bullet.Velocity.Z = 0-cos(y)*cos(x);
createModel(Bullet);
}
//
centerMouse();]]>
</Expression>
</ZExpression>
</OnUpdate>
<OnRender>
<ZExpression>
<Expression>
<![CDATA[// Quick & dirty wireframe environment
glBegin(1);
for(int i=0; i<9; i++)
{
glVertex3i(i, 0, 0); glVertex3i(i, 0, 8);
glVertex3i(0, 0, i); glVertex3i(8, 0, i);
}
glEnd();
//
glBegin(2);
glVertex3i(0, 4, 0);
glVertex3i(8, 4, 0);
glVertex3i(8, 4, 8);
glVertex3i(0, 4, 8);
glEnd();
//
glBegin(1);
glVertex3i(0, 0, 0); glVertex3i(0, 4, 0);
glVertex3i(8, 0, 0); glVertex3i(8, 4, 0);
glVertex3i(8, 0, 8); glVertex3i(8, 4, 8);
glVertex3i(0, 0, 8); glVertex3i(0, 4, 8);
glEnd();]]>
</Expression>
</ZExpression>
</OnRender>
<Content>
<Group Comment="Input">
<Children>
<Group Comment="Axis">
<Children>
<Array Name="Axis" SizeDim1="2"/>
<Array Name="AxisMap" Type="4" Dimensions="1" SizeDim1="2" SizeDim2="2" Persistent="255">
<Values>
<![CDATA[78DA73710C0F060002D80130]]>
</Values>
</Array>
</Children>
</Group>
<Group Comment="Key">
<Children>
<Array Name="Key" Type="4" SizeDim1="3"/>
</Children>
</Group>
</Children>
</Group>
<Group Comment="Bullet">
<Children>
<Model Name="Bullet" Position="7.2203 0.5 6.7303" Rotation="0.0542 1.1424 0" Velocity="-0.7352 0.3338 -0.59">
<OnRender>
<RenderMesh Mesh="BulletMesh"/>
</OnRender>
</Model>
<Mesh Name="BulletMesh">
<Producers>
<MeshSphere Scale="0.125 0.125 0.125" ZSamples="3" RadialSamples="16"/>
</Producers>
</Mesh>
</Children>
</Group>
</Content>
</ZApplication>
If your situation is different, please explain and i'll try to provide another example
jinxtengu wrote:Apologies for asking such a basic question, thing is I still haven't gotten a real handle on calculating directions from velocity, and velocity to directions in 3d space, it's kind of embarrassing

No worries, it's not as straight-forward as you might expect.
K