Follow the mouse in 3d space

All topics about ZGameEditor goes here.

Moderator: Moderators

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

Follow the mouse in 3d space

Post by jinxtengu »

Hello,
I made a little demo where a cursor model is set at the mouse co-ordinates. This seems to work, however when the camera angle is rotated around 180 degrees the movement is inverted. Also moving around on the x axis, leaves the cursor model where it was originally spawned. What sort of code would I need to write to keep the cursor movement relative to the cameras current position and current rotational value?

Hope someone can offer some suggestions. :)
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Follow the mouse in 3d space

Post by Kjell »

Hi jinxtengu,
jinxtengu wrote:What sort of code would I need to write to keep the cursor movement relative to the cameras current position and current rotational value?
Here you go :)

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" LightPosition="0 1 0.25" MouseVisible="255" FileVersion="2">
  <OnUpdate>
    <ZExpression Comment="Demo">
      <Expression>
<![CDATA[// Drive the position & rotation of the camera for demonstration purposes

float t = App.Time;

App.CameraPosition.X = sin(t*0.9)*4;
App.CameraRotation.Y = sin(t*0.8)/8;
App.CameraPosition.Z = cos(t*0.7)*4+8;]]>
      </Expression>
    </ZExpression>
    <ZExpression>
      <Expression>
<![CDATA[//

float a, s, c, r, x, y;

a = App.CameraRotation.Y*PI*2;
s = sin(a);
c = cos(a);

r = tan(App.FOV/360*PI);  // If you're using a constant FOV, you can swap out this calculation with the resulting value.

x = App.MousePosition.X*r*App.ViewportWidth/App.ViewportHeight; // If you're using a constant aspectRatio, you can swap out "App.ViewportWidth/App.ViewportHeight" with a specific value.
y = App.MousePosition.Y*r;

Box.Translate.X = App.CameraPosition.X+x*c+s;
Box.Translate.Y = App.CameraPosition.Y+y;
Box.Translate.Z = App.CameraPosition.Z+x*s-c;

// If you want the box to mimic the orientation of the camera, un-comment the following lines

//Box.Rotate.X = 0-App.CameraRotation.X;
//Box.Rotate.Y = 0-App.CameraRotation.Y;]]>
      </Expression>
    </ZExpression>
  </OnUpdate>
  <OnRender>
    <UseMaterial Material="BoxMaterial"/>
    <RenderTransformGroup Name="Box">
      <Children>
        <RenderMesh Mesh="BoxMesh"/>
      </Children>
    </RenderTransformGroup>
    <UseMaterial Material="GridMaterial"/>
    <RenderMesh Mesh="GridMesh"/>
  </OnRender>
  <Content>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox Scale="0.0625 0.0625 0.0625"/>
      </Producers>
    </Mesh>
    <Material Name="BoxMaterial" Shading="1"/>
    <Mesh Name="GridMesh">
      <Producers>
        <MeshBox Scale="4 1 1" XCount="7" YCount="1" Grid2DOnly="255"/>
      </Producers>
    </Mesh>
    <Material Name="GridMaterial" Shading="2" Light="0"/>
  </Content>
</ZApplication>
K
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Follow the mouse in 3d space

Post by VilleK »

That is a nice example Kjell.
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: Follow the mouse in 3d space

Post by jinxtengu »

Thanks for the example Kjell.
I've yet to get it to work. I tried putting it as a z expression on a models on update, but it didn't seem to do anything.
I'll have another go at it today, i was a little tired at the time i tried it.
I do mean, mouse on X and Y axes, although if the view is rotated wouldn't that necessitate using the Z axis also?
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Follow the mouse in 3d space

Post by Kjell »

Hi jinxtengu,
jinxtengu wrote:I do mean, mouse on X and Y axes, although if the view is rotated wouldn't that necessitate using the Z axis also?
My question was / is which axes you're using to rotate the camera. I assumed you're using X and Y, but perhaps you're using Z as well .. or maybe only Y ( in which case the required math is a lot simpler ).

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

Re: Follow the mouse in 3d space

Post by jinxtengu »

With the example i tested it on, the camera rotation is only on the Y axis.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Follow the mouse in 3d space

Post by Kjell »

Hi jinxtengu,
jinxtengu wrote:With the example i tested it on, the camera rotation is only on the Y axis.
Should have asked before posting the example :roll: I've updated the example to only take the Y-axis rotation of the camera in consideration.

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

Re: Follow the mouse in 3d space

Post by jinxtengu »

The code works now.
The cursor stays relative with the view rotated on it's y axis.
Thanks Kjell!

A new problem has arisen however; When sending a boid in the direction of the model placed at the mouse co-ordinates, it no longer actively seeks out the cursor model it just goes towards the center of the screen.
Any ideas why this might be the case?
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Follow the mouse in 3d space

Post by VilleK »

jinxtengu wrote:A new problem has arisen however; When sending a boid in the direction of the model placed at the mouse co-ordinates, it no longer actively seeks out the cursor model it just goes towards the center of the screen.
Any ideas why this might be the case?
Are you using the steeringcontroller? It only operates in 2d on X/Y axis, as far as I recall.
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: Follow the mouse in 3d space

Post by jinxtengu »

The movement of the model is using the steering controller, but I tried without it, and the model (which is sent towards the cursor model) still moves towards the camera (towards the screen, rather than where the cursor model appears to be positioned).

I think it must have to do with the Z axis because changing this line:

cursor.position.Z = App.CameraPosition.Z+x*s-c;
to this
cursor.position.Z = App.CameraPosition.Z+x*s-c-55;

and the movement works fine, however then the cursor isn't positioned correctly when the camera is rotated.
If it could keep a relative offset of 55 it would probably work, but i haven't quite figured out the maths of how to do that yet.

Edit
---------------------------------------------------------
actually testing the code with the -55
"cursor.position.Z = App.CameraPosition.Z+x*s-c-55;"
doesn't seem to work. I must have made a mistake.
Post Reply