Offset on spawned model.

All topics about ZGameEditor goes here.

Moderator: Moderators

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

Offset on spawned model.

Post by jinxtengu »

Hi, i was wondering how would one go about spawning a new model so that it's positioned say, relative to the right side of the model that spawns it.

Now I understand that you could set the offset to be like x+0.54 to place it to the right, however if it’s a 3d model and your facing it from a different angle then the offset would need to be different in order for the spawned model to be again right side spawned, so I guess the spawning needs to take into account the camera view.
does this make sense what I’m saying?
any ideas? :)
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Offset on spawned model.

Post by Kjell »

Hi jinxtengu,

You should get the view matrix of the camera, reset the translation components and multiply it with your desired offset vector. Here's a ( simple ) example .. press left-mouse-button to spawn a model.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0.25 1 0.5" RenderOrder="1" FileVersion="2">
  <OnUpdate>
    <KeyPress Keys="{" RepeatDelay="0.25">
      <OnPressed>
        <ZExpression Comment="Check this!">
          <Expression>
<![CDATA[// Get the current view matrix ( of the camera )

mat4 viewMatrix;
getMatrix(0, viewMatrix);

// We only want rotation, so set translation components of matrix to 0

viewMatrix[3,0] = 0;
viewMatrix[3,1] = 0;
viewMatrix[3,2] = 0;

// Create a vector pointing to the right and multiply with rotation matrix

vec3 rightVector = vector3(1, 0, 0);
vec3 offsetVector = transformPoint(viewMatrix, rightVector);

// Spawn new sprite using calculated offset

Sprite.Position.X =   offsetVector.x;
Sprite.Position.Z = 0-offsetVector.z;

createModel(Sprite);]]>
          </Expression>
        </ZExpression>
      </OnPressed>
    </KeyPress>
    <ZExpression>
      <Expression>
<![CDATA[//

App.CameraRotation.Y = 0-App.Time/4;

//

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

App.CameraPosition.X = 0-sin(y)*4;
App.CameraPosition.Z =   cos(y)*4;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <OnRender>
    <UseMaterial Material="LineMaterial"/>
    <RenderNet XCount="3" YCount="3">
      <RenderVertexExpression>
<![CDATA[//

Vertex.X *= 4;
Vertex.Z = 0-Vertex.Y*4;
Vertex.Y = -0.5;]]>
      </RenderVertexExpression>
    </RenderNet>
    <UseMaterial Material="BoxMaterial"/>
    <RenderMesh Mesh="BoxMesh"/>
  </OnRender>
  <OnClose>
    <ZExpression>
      <Expression>
<![CDATA[//

Sprite.Position = 0;]]>
      </Expression>
    </ZExpression>
  </OnClose>
  <Content>
    <Model Name="Sprite" Scale="0.25 0.25 0.25">
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[//

Sprite.Rotation.Y = 0-App.CameraRotation.Y;]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="SpriteMaterial"/>
        <RenderMesh Mesh="SpriteMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="SpriteMesh">
      <Producers>
        <MeshBox/>
        <MeshExpression Scale="1 1 0" AutoNormals="0" VertexColors="255">
          <Expression>
<![CDATA[//

C.R = V.X;
C.G = 1-V.X;
C.B = V.Y;]]>
          </Expression>
        </MeshExpression>
      </Producers>
    </Mesh>
    <Material Name="SpriteMaterial" Light="0" DrawBackFace="255"/>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox Scale="0.25 0.25 0.25"/>
      </Producers>
    </Mesh>
    <Material Name="BoxMaterial"/>
    <Material Name="LineMaterial" Shading="2" Light="0"/>
  </Content>
</ZApplication>
Make sure to check out the ZExpression in the KeyPress component in App.OnUpdate.

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

Re: Offset on spawned model.

Post by jinxtengu »

Thanks Kjell, I'll try that out and see how it goes.
Post Reply