Help with vector math

Posted By: 3run

Help with vector math - 11/01/16 14:21

Can any one help me with some vector math? I'm trying to archive mouse detection, like on this video:
Door Test #1

Here some pictures (in video you can see that it works from any camera angle/positions):
Quote:



In order to move the door, I'm planing to use ROPE hinge, I need start and end positions for it. Start is going to be the hit position of the trace (interacton trace/mouse trace?), but how do I calculate that mouse position in the world? Tried mouse_pos3d, but I couldn't get it to work correctly.

Greets!

Edit: to make things clear... I do have working example with c_trace, mouse_pos3d and mouse_dir3d, but it's not what I wanted to archive.. What I need is exactly the same thing as on the video (I'm not sure, but probably they did not use any rays/traces), I just can't think of it, how to get position of the mouse in 3d world like that?
Posted By: Superku

Re: Help with vector math - 11/01/16 15:19

I'd just c_trace for the first hit, then use the resulting hit distance as the factor for the mouse pos calculation:

hitDistance = c_trace... // calculate once on first click
...
vec_set(temp,mouse_dir3d); // dir, not pos!
vec_scale(temp,hitDistance);
vec_add(temp,camera.x);
=> move to temp
Posted By: 3run

Re: Help with vector math - 11/01/16 15:29

Hey, Superku! Thank you, but this won't work sice camera will move around, thus the temp positon will move with it... If I'll grab the door and move closer to it, temp will go to the othr side of the door. Any ideas?
Posted By: Kartoffel

Re: Help with vector math - 11/01/16 15:39

you could recalculate the hit distance by using the distance between the camera and the initial click position on the door (you have to rotate this point with the door)
Posted By: 3run

Re: Help with vector math - 11/01/16 16:13

Just did that, still not what I'm looking for.. Have you guys watched the video? tongue
Posted By: 3run

Re: Help with vector math - 11/01/16 17:10

This is something that I want to archive at the end:
PhysicsAndMouseMotionBasedOpenables - PAMMBO (WIP)
Posted By: Reconnoiter

Re: Help with vector math - 11/01/16 19:51

Hmm dont know if this is accurate enough for you but I would do first something like this and maybe it helps to build upon / as a start (pseudo code):

Code:
//do a trace or check mouse_ent

ANGLE angle;
vec_to_angle(angle, vec_diff(NULL, door.x, actor.x));
var tmp = ang(angle.pan);

//front
if (tmp >= -90 && tmp <= 90) {
	...=is front...
}
else {
	...=is back...
}

//notice that I did it very rough here, you could add more steps between

//use mouse_force.y and possible x for movement (store in vector)
//with some exponential formula if mouse is moved very fast

//rotate that vector when needed if at the back of the door

//use that vector for some physx rotate force function or physx ent rotate (if registered as PH_CHAR)
// or movement function if it is for drawers

Posted By: 3run

Re: Help with vector math - 11/02/16 00:22

Well, I did something similar. But it required even more checks... So I had to check for front/back PLUS left/right side as well. Then I got it to work, but I thought that it's too difficult and inaccurate (as a solution), so I decided to ask here. I thought maybe you guys will have some better/easier solutions.
Posted By: Reconnoiter

Re: Help with vector math - 11/02/16 11:02

How about each frame get the 3d position of the cursor (its Not by trace, see code below) and vec_to_angle with between cursor point and door. And finally do something like storing mouse_force in a vector vec_rotate it with result out of vec_to_angle (or whatever, I am not so good at that part shocked ).

Code:
//Credit to Superku iirc
VECTOR PosCursor_vec;
function SetPosMouseCursor() {//stores result in a global vector called PosCursor_vec
	var f;
	vec_set(PosCursor_vec, mouse_dir3d);
	vec_scale(PosCursor_vec, 10); // depending on the camera height it can be beneficial to scale the dir vector for increased accuracy (because of the limited accuracy of var)
	if (PosCursor_vec.z == 0) PosCursor_vec.z = 0.01; 
	f = -mouse_pos3d.z / PosCursor_vec.z;
	vec_scale(PosCursor_vec, f);
	vec_add(PosCursor_vec, mouse_pos3d);
}



Code:
function .....door.... () {
...
while....

SetPosMouseCursor();
		draw_point3d(PosCursor_vec, COLOR_GREEN, 100, 5);
		
		ANGLE angle;
		vec_to_angle(angle, vec_diff(NULL, my.x, PosCursor_vec));
		var tmp = ang(angle.pan);

		if (mouse_left) { //you will probably want to change this part to something more sensical
			VECTOR _vforce;
			vec_set(_vforce, vec_rotate(vector(mouse_force.y * time_step*20, mouse_force.x * time_step*20, 0), vector(tmp, 0, 0)));
			vec_add(_pan, _vforce);
		}
		
		pXent_rotate(my, nullvector, vector(_pan.pan, 0, 0));
}



Note place your door at 0 z or similar, otherwise you will have to expand/change the SetPosMouseCursor function a bit.
Posted By: 3run

Re: Help with vector math - 11/02/16 13:26

I'll give it a try! Thank you man! laugh
© 2024 lite-C Forums