A question about model.y velocity

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

A question about model.y velocity

Post by jinxtengu »

Hi,
I'm having a bit of difficulty with something,

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.

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 :oops: but i'm sure I'll get the hang of it eventually.
Anyway, can anyone offer some suggestions on how to sole this problem? :)
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: A question about model.y velocity

Post by Kjell »

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 :wink:
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 :oops:
No worries, it's not as straight-forward as you might expect.

K
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: A question about model.y velocity

Post by VilleK »

Nice example Kjell.
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: A question about model.y velocity

Post by jinxtengu »

Thanks for the code example Kjell.
I tried to follow it, unfortunately I was unable to get it to work in the game.
Here's the zgeproj file of what I was doing so you can take a look. Clicking the mouse spawns a bullet.
http://www.revengeofthesunfish.com/example1.zgeproj
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: A question about model.y velocity

Post by Kjell »

Hi jinxtengu,

You made a couple of mistakes ...

- Always use local variables when possible .. and otherwise never give components names that collide with properties ( in any scope ). So instead of putting 3 Variable components named "x", "y" and "t" in bulletmodel.Definitions, use local variables as i did in my example.
- Use centerMouse after you've processed the mouse input, not before .. otherwise the mouse input will always be zero :wink: So call centerMouse after assigning Facing_z and frontView.
- If you're not using a fixed framerate, always incorporate deltaTime into any variables that are time-based. So instead of incrementing bulletlife by 1 each frame, add App.DeltaTime instead.

Here's your project with just those 3 things changed ( i didn't touch anything else ).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0.21 4 1" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="Floormodel2" Position="0 -4 0"/>
    <SpawnModel Model="Playermodel"/>
  </OnLoaded>
  <Content>
    <Group Name="materials">
      <Children>
        <Material Name="bulletmaterial" Color="0.9098 0.1765 0.1765 1"/>
        <Material Name="floormaterial" Color="0.2863 0.7961 0.3765 1">
          <Textures>
            <MaterialTexture Texture="floorbmp" TextureScale="222 222 1" TexCoords="1"/>
          </Textures>
        </Material>
        <Bitmap Name="floorbmp" Width="128" Height="128">
          <Producers>
            <BitmapFromFile Comment="Imported from skin8.bmp">
              <BitmapFile>
<![CDATA[78DAED9DCBAD15310C86A5BB674113EC28817290A8E0B6C08E2E5850165DB083230D1A8D2689E3F895C4F34B5EA0A3CB3C3E278EE3FCC9BC7FFCFB0E83C160B041FBF9E98FF20F607B3914067B722F40078121BC6F645F3FFC3A0C2882EDF3DBB7137EE985177FB820187ED911E08260F8652F002EC27E7FFF6F4EFCE1822E79810BBAFCD105469BFD97B71F2F03FF5931E7E0CF7401F8EBF997BFF35DC0E7EF91855E9F7FA8D964E2CF1C825DE15F9F79232FE8F99F2E687961A880CFF751F7C9B7708109FFAB0B6EBEE0279FAFFF72F067BA80C8D3D2F017CCC54E1B2D010DF1A7F3E4EDF86BF21F66299243F57001FD4B32FEE5BBACF3F0448FC8C4BF1B4E356D9E3F9EB61AFF43F8AFD9D45BF1C73073C85D9D63AE4C1D43B63EFF4C0F5FEC826B4BAE72AE664DB2F9D74658C26A74B7F0623517F67E7126FCF397B0D1797482ECC47F8580A09F376982AD2CBD49C33F2601D0F317D77976CC4602F88BBD9C430BE1C7BF75E5F3C76E999748DA1F928D8BF95F2F4B10EEC2CF34D48A870013F8A397AAF24FE902F314C8EA6A65FC5984BFD3029CD51CA47529E5509E5B7C683805F6E00FE567649D01F06D5DF0902A3A1A49CCDE044088DC0602FE4BB5F6E317849A98AD4F84F4A8DA3B60AE41FECCC446E1A3BFE8B7E4746102B2F77E28A0A6853A62D1EC0D724B060CFE55F84AE97819C9690DF6EB1FE0DFAA2999A8F7391AF887E73F9C956BB10B88F883FC9FAFDB1C8D42D7F65CBA00F540BE88D16414302CC626F397077F8E6B10F6FDE20FD11D6EC0C19F3FFE9A543EB706EE276D35576EE4ABF95785287E2E98A561DEA8E507780116AF2CC5D2AD2045442B057FF087F949FD67F17FDD9733D08BC3BEED4EF398E43006FE7147F114987F6A845552A779DA4859EFE874834346D6FEAD76518D3EAD5ED6EBEA0B42F7DB7A29D9563E934E4D3FAD9FB23A9EBFF94BB9F2DF373301FF47F12F87E0F593138DA4641DFE26A9057185E9FC890309F96F744B2A16DF73B408FC6E0E33EA02CDE6FA60174C2FE1F25715036AD1612287D5F8074FD2E925E347E94CF8FCAD5C50DD24FE581704F3A7479907BA208CFFA876A2CB3F872828987FF7464CFE5799E2D68BA726F98F8924662FFEB68B1ADDFC7F35FEA70B32AD5ABA1EC86FCE3F938A23E06B081CFECAF7CA24D30D3B09D0AA51AD36226C2D4C95F1AF7E39023654FED2948004C720C3B6B6C76E894A937C2E65CCE88DA1D68F7FBEA176A918551293AD8060A8D54718220B426C892F7A884FF098DECDB7CB82E88A936CC28BD339BA0DF56465A2C32CB320B82058077B9B0580BF959E5C30FEAE7C40C76D5FF9DCD4E216855A8D7FF4A83D02BE7294F770417C6FBD9DE773C3EB77547575D752BC0B88662F3BDDD1699E6BFB18319F881D7ACDB2A91CED674A50AADED49B7F6414E21F70E444602824A6E15F7D0542A57CF29F353A3BDD7462FCB991ECAAF4E71E33E5CDDFEAB3147EBB84F4E9D09A7B2E56D099330F4511EFCC0DCE2B724CF65BFCABC73F32F5CCE0AFE7AFD193837F39A285F1F7805F9E2CB151B1973FF86AF8FB1D04573DD6C3CF057E05194E1AAC57503BE50CE54119DB85B86B0E46B7A545165E23BF4F3DCB05ADAEB732FFAD87F8EA4C64CD358B1CFCF92D7935B14702FE2DA4E2926F644F093BD343F6521A142DFEB22EE09DB3C5B77C7CE8C13BB385C1B057627D0121C22010013B78C2B6F0089C0883C19CEC1FE65B61C8]]>
              </BitmapFile>
            </BitmapFromFile>
          </Producers>
        </Bitmap>
      </Children>
    </Group> <!-- materials -->

    <Group Name="meshes">
      <Children>
        <Mesh Name="boxmesh">
          <Producers>
            <MeshBox/>
          </Producers>
        </Mesh>
        <Mesh Name="BasicconeMesh0" Comment="Cone">
          <Producers>
            <MeshImport>
              <MeshData>
<![CDATA[78DA854BC10D8020102B7C34BE5C82399C8C4058CC3DD8041ED61EC4F832DEA5BDF6D26E00768C395FE4E341A9400CA4FED974A94C001983E9D1CBA69549D3299BB5524EB462814753DFC1FC1737CEDB7F93C6173B3D6E98A2228E]]>
              </MeshData>
            </MeshImport>
          </Producers>
        </Mesh>
      </Children>
    </Group> <!-- meshes -->

    <Group Name="models">
      <Children>
        <Model Name="bulletmodel">
          <Definitions>
            <Variable Name="bulletlife"/>
          </Definitions>
          <OnSpawn>
            <ZExpression>
              <Expression>
<![CDATA[float t, x, y;

t = App.DeltaTime;

x = App.CameraRotation.X*PI*2;
y = App.CameraRotation.Y*PI*2;

bulletmodel.Rotation.X = 0-App.CameraRotation.X;
bulletmodel.Rotation.Y = 0-App.CameraRotation.Y;

bulletmodel.Velocity.X =   sin(y)*cos(x);
bulletmodel.Velocity.Y = 0-       sin(x);
bulletmodel.Velocity.Z = 0-cos(y)*cos(x);]]>
              </Expression>
            </ZExpression>
          </OnSpawn>
          <OnUpdate>
            <ZExpression Expression="bulletlife+=App.DeltaTime;"/>
            <Condition Expression=" return bulletlife>=43;">
              <OnTrue>
                <RemoveModel/>
              </OnTrue>
            </Condition>
          </OnUpdate>
          <OnRender>
            <UseMaterial Material="bulletmaterial"/>
            <RenderMesh Mesh="BasicconeMesh0"/>
          </OnRender>
        </Model>
        <Model Name="Playermodel">
          <Definitions>
            <Variable Name="Facing_z"/>
            <Variable Name="frontView"/>
            <Variable Name="PlayerViewAngle"/>
          </Definitions>
          <OnUpdate>
            <ZExpression>
              <Expression>
<![CDATA[// camera
 //*
app.cameraPosition.x=currentModel.position.x;
app.cameraPosition.y=currentModel.position.y;
app.cameraPosition.z=currentModel.position.z;
app.cameraRotation.x=currentModel.rotation.x;
app.cameraRotation.y=currentModel.rotation.y;
app.cameraRotation.z=currentModel.rotation.z;
 //*
//mouse controls
Facing_z+=clamp(app.mousePosition.x*0.525,-0.01,0.01);
frontView-=clamp(app.mousePosition.y*.515,-0.01,0.01);
centerMouse();
frontView=clamp(frontView,-0.65,0.65); //look limit
app.cameraRotation.y=Facing_z;
app.cameraRotation.x=frontView;
//*/
// friction
currentModel.velocity.x*=.80;
currentModel.velocity.y*=.01;
currentModel.velocity.z*=.80;]]>
              </Expression>
            </ZExpression>
            <ZExpression>
              <Expression>
<![CDATA[App.CameraPosition.Z=CurrentModel.Position.Z;
App.CameraPosition.X=CurrentModel.Position.X;
App.CameraPosition.Y=CurrentModel.Position.Y;
PlayerViewAngle=Facing_z;

CurrentModel.Velocity.Z=0;
CurrentModel.Velocity.X=0;

//CurrentModel.Velocity.Y-=5.5*App.DeltaTime;
CurrentModel.Velocity.Y=clamp(CurrentModel.Velocity.Y,-6,5);]]>
              </Expression>
            </ZExpression>
            <KeyPress Keys="{">
              <OnPressed>
                <SpawnModel Model="bulletmodel" UseSpawnerPosition="255"/>
              </OnPressed>
            </KeyPress>
          </OnUpdate>
        </Model>
        <Model Name="Floormodel2" Scale="54 0.01 54">
          <OnRender>
            <UseMaterial Material="floormaterial"/>
            <RenderMesh Mesh="boxmesh"/>
          </OnRender>
        </Model>
      </Children>
    </Group> <!-- models -->

  </Content>
</ZApplication>
K
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: A question about model.y velocity

Post by jinxtengu »

Thanks Kjell. The example works after making those changes, also thank you for pointing out my mistakes. This is very helpful to me as I don't get feedback very often, so I never know if I'm doing things in a smart way or not.

One other thing, how can we adjust the rotational direction (adjust heading) so the bullet model is moving
in relation to it's x,y,z velocity, and also (separately) in relation to the direction the camera is facing?
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: A question about model.y velocity

Post by Kjell »

Hi jinxtengu,
jinxtengu wrote:One other thing, how can we adjust the rotational direction (adjust heading) so the bullet model is moving
in relation to it's x,y,z velocity, and also (separately) in relation to the direction the camera is facing?
It already does that .. but your cone mesh is pointing into the wrong* direction. So, you need to rotate it 90 degrees and export it again :wink:

Image

K
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: A question about model.y velocity

Post by jinxtengu »

Ahah, cool cool. I suspected that was the problem.
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: A question about model.y velocity

Post by jinxtengu »

Er, I'm still having a bit of an issue directing moving objects.
I find that this works when run on model update:
CurrentModel.Rotation.Y=Atan2(CurrentModel.velocity.x,CurrentModel.velocity.Z)/(PI*2);

But it only sets the direction for objects moving in a circle and not up and down.
How can we set CurrentModel.Rotation.X and Z, to behave in the same manner?
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: A question about model.y velocity

Post by Kjell »

Hi jinxtengu,

Usually you want to do it the other way around by calculating the velocity from the rotation values. Here's another quick demo ..

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0 1 0" 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[//

for(int i=0; i<16; i++)
{
  Plane.Position.X = random(4, 4);
  Plane.Position.Y = random(2, 2);
  Plane.Position.Z = random(4, 4);

  Plane.Rotation.Y = rnd();

  createModel(Plane);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[//

App.CameraRotation.X = 0-App.MousePosition.Y*0.125;
App.CameraRotation.Y = App.MousePosition.X*App.ViewportWidth*0.125/App.ViewportHeight;

//

float x, y;

x = App.CameraRotation.X*PI*2;
y = App.CameraRotation.Y*PI*2;

App.CameraPosition.X = 4-cos(x)*sin(y)*16;
App.CameraPosition.Y =   sin(x)       *16;
App.CameraPosition.Z = 4+cos(x)*cos(y)*16;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[//

glBegin(1);

for(int i=0; i<=8; i++)
{
  glVertex3i(i, 0, 0); glVertex3i(i, 0, 8);
  glVertex3i(0, 0, i); glVertex3i(8, 0, i);
}

glEnd();]]>
      </Expression>
    </ZExpression>
  </OnRender>
  <Content>
    <Model Name="Plane">
      <OnUpdate>
        <ZExpression Comment="Check this out!">
          <Expression>
<![CDATA[// Animate the plane over the x-axis

Plane.Rotation.X = cos(App.Time+Plane.Personality*PI*2)/16;

// Calculate the velocity from the rotation x & y axes

float x, y;

x = Plane.Rotation.X*PI*2;
y = Plane.Rotation.Y*PI*2;

Plane.Velocity.X = 0-cos(x)*sin(y);
Plane.Velocity.Y =          sin(x);
Plane.Velocity.Z = 0-cos(x)*cos(y);]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="PlaneMaterial"/>
        <RenderMesh Mesh="PlaneMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="PlaneMesh">
      <Producers>
        <MeshImport>
          <MeshData>
<![CDATA[78DA3D8AD111803008439FDA3F77B20EE1008EC2264EA2ED621E865A4D2E907B300389905D504F459BAC2CB0667697C08BA67951F3E87E346EB0DD2FEF9F8D7CD7E0834EE3EFC4D4FD0049822D1E]]>
          </MeshData>
        </MeshImport>
      </Producers>
    </Mesh>
    <Material Name="PlaneMaterial" Shading="1" DrawBackFace="255"/>
  </Content>
</ZApplication>
But in case you absolutely want to calculate the rotation from the velocity vector .. here you go :wink:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0 1 0" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[//

for(int i=0; i<64; i++)
{
  Plane.Velocity.X = random(0, 2);
  Plane.Velocity.Y = random(2, 2);
  Plane.Velocity.Z = random(0, 2);

  createModel(Plane);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[//

App.CameraRotation.Y = App.MousePosition.X*0.5;

//

float y = App.CameraRotation.Y*PI*2;

App.CameraPosition.X = 0-sin(y)*8;
App.CameraPosition.Z =   cos(y)*8;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <Content>
    <Model Name="Plane" Velocity="3.9249 1.1064 2.2806">
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[// Apply gravity to velocity.y

Plane.Velocity.Y -= App.DeltaTime;

// Calculate rotation from velocity

float s, x, y, z;

x = Plane.Velocity.X;
y = Plane.Velocity.Y;
z = Plane.Velocity.Z;

s = sqrt(x*x+y*y+z*z);

x /= s;
y /= s;
z /= s;

//

Plane.Rotation.X = asin(y)/PI/2;
Plane.Rotation.Y = atan2(0-x, 0-z)/PI/2;]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="PlaneMaterial"/>
        <RenderMesh Mesh="PlaneMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="PlaneMesh">
      <Producers>
        <MeshImport>
          <MeshData>
<![CDATA[78DA3D8AD111803008439FDA3F77B20EE1008EC2264EA2ED621E865A4D2E907B300389905D504F459BAC2CB0667697C08BA67951F3E87E346EB0DD2FEF9F8D7CD7E0834EE3EFC4D4FD0049822D1E]]>
          </MeshData>
        </MeshImport>
      </Producers>
    </Mesh>
    <Material Name="PlaneMaterial" DrawBackFace="255"/>
  </Content>
</ZApplication>
K
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: A question about model.y velocity

Post by jinxtengu »

Thank you for the code examples Kjell :)
Post Reply