How to move 3d objects using mouse in real time

Posted By: tex

How to move 3d objects using mouse in real time - 04/06/08 04:38

Hi, I don't think "how to move 3d objects using mouse in real time" has ever been answered in any detail. Lots of code snippets, but nothing comprehensive.

Any ideas?
Posted By: vlau

Re: How to move 3d objects using mouse in real time - 04/06/08 07:32

The question is "How do you want to move the 3D objects
in real time".
Posted By: tex

Re: How to move 3d objects using mouse in real time - 04/06/08 08:51

I am developing some psychology experiments. I need to be able to pick an object in the scene with the mouse pointer and move it around - such that it follows the pointer, whilst preserving the absolute distance from the camera/screen.
Posted By: tex

Re: How to move 3d objects using mouse in real time - 04/06/08 09:00

I found your modelNodePath demo. It almost does what I need. However, I can't seem to get the object to maintain the same distance from the camera if the camera is rotated dynamically. The oject always jumps to the camera coordinates. I can see this is not a problem in the demo as the camera is fixed. I need to be able to move the camera and the objects dynamically.
Posted By: vlau

Re: How to move 3d objects using mouse in real time - 04/06/08 10:18

Have send you the demo to the email address you gave me.
This is what I've done so far, I don't have too much time
dealing with it, sorry.

It may not exactly what you're after but I hope it will
get you started.

You may keep on asking here if you got any problem, maybe
other people can help.
Posted By: tex

Re: How to move 3d objects using mouse in real time - 04/07/08 09:17

Thank you for this.

However, it only moves about a plane. I need to be able to move objects around in any direction - up, down, etc. Your examples fix the z value and direction. Although I am able to adjust it to move about in drag_new.zip it seems to jump to the extent of the raycast until it finds a wall and then circumscribes the boundary?

I can't seem to get it to accept:

dist = vec_dist(my.x,camera.x);
vec_set(temp,mouse_dir3d);
//vec_normalize(temp,600);
vec_normalize(temp,dist);

Which should set it to a constant distance from the camera.

What am I doing wrong?
Posted By: FoxHound

Re: How to move 3d objects using mouse in real time - 04/07/08 10:04

You need to put the pan of the camera into your formula. Been a long time since I've used 3dgs and I don't have it installed on this computer so I can't try anything out for you. but that should get you going.
Posted By: vlau

Re: How to move 3d objects using mouse in real time - 04/07/08 10:33

@tex, yes it's not perfect. I'm still busy with my work here,
if I got some free time, I will rework for it again.

@Fox, nice to see you back again.


Posted By: tex

Re: How to move 3d objects using mouse in real tim - 04/07/08 10:39

Thanks Foxh and vlau,
I will keep working on it. I think ultimately this may be of use to some others too - so it is worth getting it to work properly.

It is late in Oz - so need to sleep...zzzzzzz

Cheers!:)
Posted By: tex

Re: How to move 3d objects using mouse in real tim - 05/07/08 06:38

I have done some more work on this problem.

I decided to use an orbit function. It is the opposite to the camera orbiting the avatar/object Instead I have a box orbiting the avatar/camera. However, the box should not follow the avatar/camera, or keep the same distance. But the distance between the object and the avatar/camera needs to be updated each time the camera/avatar moves so that when the box is moved it simply rotates about the new camer/avatar centroid coords.

The problem I have now is that it is not precise. When you move the camera/avatar around and then click/move the box it jumps a bit to a new cordinate.

What am I doing wrong?:


#include <acknex.h>
#include <default.c>

ENTITY* eBall;

var temp[3];

var rotspd=0.5; // cam movement speed

VECTOR vSpeed, vAngularSpeed, vForce, vMove;

float temp_x, temp_y, temp_z;
float temp_x2, temp_y2, temp_z2;
float tempx, tempy, tempz;

var cam_dist;

ANGLE cam_ang;

void main()
{
video_mode = 7;
shadow_stencil = 1;
// Load the level named "small.hmp"
level_load("small.hmp");
// Now create the "earth.mdl" model at x = 10, y = 20, z = 30 in our 3D world
eBall = ent_create("box.mdl",vector(0, 0, 0),NULL);
// Set an entity flag to cast a dynamic shadow
set(eBall,SHADOW);
// Use one of the default materials for giving it a shiny look
eBall.material = mat_metal;

//phent_settype(eBall,PH_RIGID,PH_SPHERE);
//phent_setmass(eBall,1,PH_SPHERE);
//phent_setfriction(eBall,90);
//phent_setelasticity(eBall,75,100);
//phent_setdamping(eBall,30,5);

// A ball game would be no fun without gravity.
//ph_setgravity(vector(0,0,-500));

while (1)
{


if(key_r)
{
if(mouse_left)
{

cam_ang.pan += rotspd*mouse_force.x;
cam_ang.tilt += rotspd*mouse_force.y;

eBall.x= (camera.x+cos(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt)));
eBall.y= -1*(camera.y+sin(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt)));
eBall.z= (camera.z+sin(cam_ang.tilt)*cam_dist);

vec_set(temp,eBall.x);
vec_sub(temp,camera.x);
vec_to_angle(eBall.pan,temp);

}

}

else {

temp_x2 = eBall.x - camera.x;
temp_y2 = eBall.y - camera.y;
temp_z2 = eBall.z - camera.z;
cam_dist = sqrt(temp_x2*temp_x2 + temp_y2*temp_y2 + temp_z2*temp_z2);

if(mouse_left)
{

vForce.x = -5*(mouse_force.x); // pan angle
vForce.y = 5*(mouse_force.y); // tilt angle
vForce.z = 0; // roll angle
vec_accelerate(vMove,vAngularSpeed,vForce,0.8);
vec_add(eBall.pan,vMove);

}
}

if(mouse_right)
{
vForce.x = -5*(key_force.x + mouse_force.x); // pan angle
vForce.y = 5*(key_force.y + mouse_force.y); // tilt angle
vForce.z = 0; // roll angle
vec_accelerate(vMove,vAngularSpeed,vForce,0.8);
vec_add(camera.pan,vMove);
}
vForce.x = 6 * (key_w - key_s); // forward
vForce.y = 6 * (key_a - key_d); // sideward
vForce.z = 6 * (key_home - key_end); // upward
vForce.z = 6 * (key_pgup - key_pgdn); // upward
vec_accelerate(vMove,vSpeed,vForce,0.5);
vec_rotate(vMove,camera.pan);
vec_add(camera.x,vMove);

wait(1);
}
}
Posted By: vlau

Re: How to move 3d objects using mouse in real tim - 05/07/08 07:57

Try replace :

eBall.x= (camera.x+cos(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt)));
eBall.y= -1*(camera.y+sin(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt)));
eBall.z= (camera.z+sin(cam_ang.tilt)*cam_dist);

to

vec_set(eBall.x,cam_dist.x);
vec_rotate(eBall.x,camera.pan);
vec_add(eBall.x,camera.x);
vec_set(eBall.pan,camera.pan);

Not tested btw.

Posted By: tex

Re: How to move 3d objects using mouse in real tim - 05/08/08 00:09

Thanx Vlau

Unfortunately:

vec_set(eBall.x,cam_dist.x);
vec_rotate(eBall.x,camera.pan);
vec_add(eBall.x,camera.x);
vec_set(eBall.pan,camera.pan);

doesn't seem to work.

cam_dist is a var so only has one value not x, y, z. Although it would be possible to define separate values for each of these.

I think the problem is not with updating the position coordinates for eBall but its orientation. Not sure how to get this and pass it to eBall after executing the orbit function...

will try something else now...
© 2024 lite-C Forums