change point of rotation in-game

Posted By: xbox

change point of rotation in-game - 07/18/14 20:11

Is it possible to alter the "origin" of a WMB entity in-game so it can be rotated around this new point? If so, any pointers as to how I can do so? I need to be able to load a map, grab a subentities coordinate, and rotate the map around that point.
Posted By: txesmi

Re: change point of rotation in-game - 07/18/14 23:41

Hi,
you will need to rotate the offset vector from the rotation point to the origin of the entity.

Code:
vec_sub ( vTemp, ent.x, vRotOrigin );
vec_rotate ( vTemp, vector ( ang(angRotation - ent.pan), 0, 0 ) );
vec_add ( vTemp, vRotOrigin );
vec_set ( ent.x, vTemp );
ent.pan = angRotation;

Posted By: CD_saber

Re: change point of rotation in-game - 07/26/14 09:20

I have the same problem. TXESMI, could you give us an example, using the given parameters (vTemp etc. ?)

Thanks!
Posted By: txesmi

Re: change point of rotation in-game - 07/26/14 12:14

Here you go!

Code:
#include <acknex.h>
#define PRAGMA_PATH "%EXE_DIR%\\templates\\models"

void ent_rotateXY_from_point ( ENTITY *ent, VECTOR *vRotOrigin, var angRotation )
{
	VECTOR vTemp;
	vec_diff ( vTemp, ent.x, vRotOrigin );
	vec_rotate ( vTemp, vector ( angRotation, 0, 0 ) );
	vec_add ( vTemp, vRotOrigin );
	vec_set ( ent.x, vTemp );
	ent.pan += angRotation;
}

void main ()
{
	level_load ( "" );
	camera.x = -500;
	camera.z = 200;
	camera.tilt = -20;
	ENTITY *ent = ent_create ( "buggy.mdl", nullvector, NULL );
	while ( !key_esc )
	{
		ent_rotateXY_from_point ( ent, ent.max_x, time_step * 5 );
		wait(1);
	}
	sys_exit(NULL);
}



Salud!
Posted By: xbox

Re: change point of rotation in-game - 07/26/14 16:45

My sincerest apologies as I try to understand vector manipulation, I've been playing with this code trying to change the point which is pans around and I'm getting no where. The level I loaded through ent_create originally panned around the outer most x point(max_x) so I changed it with a vector I created as one of the points to rotate around, however when I replace ent->max_x with a different vector, the entity just rotates around 0,0,0 no matter what.

Normally after a few minutes of trial and error I can figure it out, but I can't think straight long enough today grin might be time for more coffee...
Posted By: txesmi

Re: change point of rotation in-game - 07/27/14 07:38

Hi, I have been testing the function with big and small origin offsets and did not find any case that the vector length get null. How do you declare the rotation origin vector?

Anyway I realized that the function call alters the length of the computed offset vector because of the innacuracy of the variables so I would save the offset vector of the unrotated state of the entity and rotate it with the absolute rotation of the entity each frame. This way the offset vector mantains its length during the run.

Code:
#include <acknex.h>

#define PRAGMA_PATH "%EXE_DIR%\\templates\\models"

#define vOriginX   skill20
#define vOriginY   skill21
#define vOriginZ   skill22
#define vOffsetX   skill23
#define vOffsetY   skill24
#define vOffsetZ   skill25

void ent_offset_rotateXY ( ENTITY *ent, var angRotation )
{
	ent.pan += angRotation;
	vec_set ( ent.x, ent.vOffsetX );
	vec_rotate ( ent.x, vector ( ent.pan, 0, 0 ) );
	vec_add ( ent.x, ent.vOriginX );
}

void main ()
{
	level_load ( "" );
	camera.x = -500;
	camera.z = 200;
	camera.tilt = -20;
	ENTITY *ent = ent_create ( "buggy.mdl", nullvector, NULL );
	vec_set ( ent.vOriginX, ent.max_x );
	vec_diff ( ent.vOffsetX, ent.x, ent.vOriginX );
	
	while ( !key_esc )
	{
		ent_offset_rotateXY ( ent, time_step * 5 );
		wait(1);
	}
}




Posted By: xbox

Re: change point of rotation in-game - 07/28/14 16:57

This is the vector length I'm using, nothing special.
Code:
vector(0,248,24);


The only thing I changed was in the main function here.

Code:
ENTITY *ent = ent_create ( "Room02.wmb", nullvector, NULL );
ent_create( "cube.mdl", vector(0,248,24), NULL); //Point to rotate around
ent_create( "cube.mdl", vector(0,0,0), NULL); //origin reference
vec_set ( ent.vOriginX, vector(0,0,0));
vec_diff ( ent.vOffsetX, vector(0,248,24), ent.vOriginX );


Maybe I'm reading this wrong and that is causing the issue. Here's what I think the code is doing.

I set the vector (0,0,0) to the vOrigin vector, I then calculate the difference between the vOrigin and the new point I want to rotate around, (0,248,24), and store it to the offset vector.

When I run this code, the map rotates around vector(0,0,0), and I can see the other cube that is supposed to rotating around off to the side, but it's clearly not the point it's rotating around.
Posted By: txesmi

Re: change point of rotation in-game - 07/28/14 20:21

Mixed alltogether laugh

The origin of an entity is 'ent.x', you know. 'ent.vOriginX' is the origin of the rotation in world space. 'ent.vOffsetX' is the offset vector from the rotation origin to the unrotated entity origin. You do not need to change its calculation:
Code:
vec_diff ( ent.vOffsetX, ent.x, ent.vOriginX );

Posted By: xbox

Re: change point of rotation in-game - 07/29/14 04:34

....and there it is. It's working beautifully. I appreciate your help and patience with this. The worst part is, this was supposed to be of the easier parts of this project... grin
© 2024 lite-C Forums