Follow the mouse in 3d space
Moderator: Moderators
Follow the mouse in 3d space
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.
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.
Re: Follow the mouse in 3d space
Hi jinxtengu,
K
Here you gojinxtengu 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?
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>
Re: Follow the mouse in 3d space
That is a nice example Kjell.
Re: Follow the mouse in 3d space
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?
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?
Re: Follow the mouse in 3d space
Hi jinxtengu,
K
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 ).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?
K
Re: Follow the mouse in 3d space
With the example i tested it on, the camera rotation is only on the Y axis.
Re: Follow the mouse in 3d space
Hi jinxtengu,
K
Should have asked before posting the example I've updated the example to only take the Y-axis rotation of the camera in consideration.jinxtengu wrote:With the example i tested it on, the camera rotation is only on the Y axis.
K
Re: Follow the mouse in 3d space
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?
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?
Re: Follow the mouse in 3d space
Are you using the steeringcontroller? It only operates in 2d on X/Y axis, as far as I recall.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?
Re: Follow the mouse in 3d space
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.
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.