Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (vicknick, howardR, sleakz), 674 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
check if entity is within rotated box #462415
09/28/16 16:02
09/28/16 16:02
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Hello,

I know its easy to check if an entity's center is within a (large) box by comparing its xyz position with the box position + box size. But how to do this if I rotate the box by e.g. 45 degrees?

Re: check if entity is within rotated box [Re: Reconnoiter] #462416
09/28/16 17:02
09/28/16 17:02
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
you might calculate the offset from the cube to the entity, rotate it the inverse rotation of the cube and check if it is inside the unrotated cube bounds.

Salud!

Re: check if entity is within rotated box [Re: txesmi] #462420
09/28/16 19:08
09/28/16 19:08
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
That sounds like a good plan, I am going to try that out. However before I try, I want to draw the box and rotate it. However in the test code below the vec_rotate doesn't rotate the quad (just for testing) properly:

Code:
vec_fill(vMin, -200);
vec_fill(vMax, 200);
vec_rotate(vMin, vector(camera.pan, 0, 0));
vec_rotate(vMax, vector(camera.pan, 0, 0));
draw_line3d(vector(vMin.x, vMin.y, vMin.z),NULL,100);  
draw_line3d(vector(vMax.x, vMin.y, vMin.z),drawcolor_vec,100);
draw_line3d(vector(vMax.x, vMax.y, vMin.z),drawcolor_vec,100); 
draw_line3d(vector(vMin.x, vMax.y, vMin.z),drawcolor_vec,100); 
draw_line3d(vector(vMin.x, vMin.y, vMin.z),drawcolor_vec,100);



I am probably making some math error.

Re: check if entity is within rotated box [Re: Reconnoiter] #462434
09/29/16 14:22
09/29/16 14:22
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
yes you are grin

In the case your cubes are perfect, there is a simplified way to compute vertex positions.

Code:
90 degrees rotation
x' = -y;
y' = x;



so you should draw your square as follows

Code:
draw_line3d(vector(vMax.x, vMax.y, vMin.z),NULL,100);  
draw_line3d(vector(-vMax.y, vMax.x, vMin.z),drawcolor_vec,100);
draw_line3d(vector(-vMax.x, -vMax.y, vMin.z),drawcolor_vec,100); 
draw_line3d(vector(vMax.y, -vMax.x, vMin.z),drawcolor_vec,100); 
draw_line3d(vector(vMax.x, vMax.y, vMin.z),drawcolor_vec,100);



Salud!

Re: check if entity is within rotated box [Re: txesmi] #462435
09/29/16 15:55
09/29/16 15:55
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Okay I am almost there grin , tnx so far, but its now always draws an square-like cube, but I want to allow a rectangle-like cube too. How to change this? Here is the code by the way but an example will do to (please note that vec is the position the cube is in the map):

Code:
vec_lerp(vec, vMin, vMax, 0.5);
draw_point3d(vec, COLOR_RED, 100, 50);

vec_sub(vMin, vec);
vec_sub(vMax, vec);
vec_rotate(vMin, vector(camera.pan, 0, 0));
vec_rotate(vMax, vector(camera.pan, 0, 0));

//bottom
draw_line3d(vector(vec.x + vMax.x, vec.y + vMax.y, vec.z + vMin.z),NULL,100);  
draw_line3d(vector(vec.x + -vMax.y, vec.y + vMax.x, vec.z + vMin.z),drawcolor_vec,100);
draw_line3d(vector(vec.x + -vMax.x, vec.y + -vMax.y, vec.z + vMin.z - k),drawcolor_vec,100); 
draw_line3d(vector(vec.x + vMax.y, vec.y + -vMax.x, vec.z + vMin.z),drawcolor_vec,100); 
draw_line3d(vector(vec.x + vMax.x, vec.y + vMax.y, vec.z + vMin.z),drawcolor_vec,100);
//top
draw_line3d(vector(vec.x + vMax.x, vec.y + vMax.y, vec.z + vMax.z),drawcolor_vec,100);
draw_line3d(vector(vec.x + -vMax.y, vec.y + vMax.x, vec.z + vMax.z),drawcolor_vec,100);
draw_line3d(vector(vec.x + -vMax.x, vec.y + -vMax.y, vec.z + vMax.z),drawcolor_vec,100); 
draw_line3d(vector(vec.x + vMax.y, vec.y + -vMax.x, vec.z + vMax.z),drawcolor_vec,100); 
draw_line3d(vector(vec.x + vMax.x, vec.y + vMax.y, vec.z + vMax.z),drawcolor_vec,100);
//other lines
draw_line3d(vector(vec.x + -vMax.y, vec.y + vMax.x, vec.z + vMin.z),NULL,100);  
draw_line3d(vector(vec.x + -vMax.y, vec.y + vMax.x, vec.z + vMax.z),drawcolor_vec,100);
draw_line3d(vector(vec.x + -vMax.x, vec.y + -vMax.y, vec.z + vMin.z),NULL,100);  
draw_line3d(vector(vec.x + -vMax.x, vec.y + -vMax.y, vec.z + vMax.z),drawcolor_vec,100);
draw_line3d(vector(vec.x + vMax.y, vec.y + -vMax.x, vec.z + vMin.z),NULL,100);  
draw_line3d(vector(vec.x + vMax.y, vec.y + -vMax.x, vec.z + vMax.z),drawcolor_vec,100);


Re: check if entity is within rotated box [Re: Reconnoiter] #462443
09/30/16 05:13
09/30/16 05:13
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
As far as I know there is no a so simplified way to rotate rectangles. Do you want to rotate them around the Z axis (pan) only?

Re: check if entity is within rotated box [Re: txesmi] #462446
09/30/16 08:53
09/30/16 08:53
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Yes I only want to rotate the box around the pan. I there perhaps some vector function I can use?

Re: check if entity is within rotated box [Re: Reconnoiter] #462454
10/01/16 06:17
10/01/16 06:17
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
No special functions, but in that case you only need to rotate two vertex to complete the shape.

Code:
#include <acknex.h>

#define PRAGMA_POINTER

void vec2d_rotate ( VECTOR *_v, var angle )
{
	var x = fcos(angle,_v->x) - fsin(angle,_v->y);
	_v->y = fsin(angle,_v->x) + fcos(angle,_v->y);
	_v->x = x;
}

void main ()
{
	wait(1);
	level_load ( "" );
	
	VECTOR vPoint;
	vec_set ( &vPoint, vector(300,240,100) );
	
	VECTOR vMax, vMin;
	vec_set ( &vMax, vector(400,200,200) );
	vec_set ( &vMin, vector(0,0,0) );
	
	VECTOR vCenter, vBounds;
	vec_lerp ( &vCenter, &vMax, &vMin, 0.5 );
	vec_diff ( &vBounds, &vMax, &vCenter );
	
	while ( !key_esc )
	{
		camera->pan = ang ( camera->pan + mickey.x * 0.3 );
		camera->tilt = clamp ( camera->tilt + mickey.y * 0.3, -85, 85 );
		vec_for_angle ( &camera->x, &camera->pan );
		vec_scale ( &camera->x, -1000 );
		VECTOR vMove;
		vec_set ( &vMove, vector(key_force.y,-key_force.x,0) );
		vec_normalize ( &vMove, 10*time_step );
		vec2d_rotate ( &vMove, camera->pan );
		vec_add ( &vPoint, &vMove );
		vec_add ( &camera->x, &vPoint );
		
		var nRotation = total_ticks * 2;
		
		VECTOR vOffset;
		vec_diff ( &vOffset, &vPoint, &vCenter );
		vec2d_rotate ( &vOffset, -nRotation );
		COLOR colCube;
		if ( ( abs(vOffset.x) < abs(vBounds.x) ) && ( abs(vOffset.y) < abs(vBounds.y) ) && ( abs(vOffset.z) < abs(vBounds.z) ) )
			vec_set ( &colCube, COLOR_GREEN );
		else
			vec_set ( &colCube, COLOR_RED );
		
		VECTOR vP1, vP2;
		vec_set ( &vP1, &vBounds );
		vec_set ( &vP2, vector(vBounds.x,-vBounds.y,-vBounds.z) );
		vec2d_rotate ( &vP1, nRotation );
		vec2d_rotate ( &vP2, nRotation );
		
		draw_point3d ( &vPoint, COLOR_WHITE, 100, 16 );
		
		draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, vCenter.z+vP1.z), NULL,     100 );  
		draw_line3d ( vector ( vCenter.x+vP2.x, vCenter.y+vP2.y, vCenter.z+vP1.z), &colCube, 100 );
		draw_line3d ( vector ( vCenter.x-vP1.x, vCenter.y-vP1.y, vCenter.z+vP1.z), &colCube, 100 ); 
		draw_line3d ( vector ( vCenter.x-vP2.x, vCenter.y-vP2.y, vCenter.z+vP1.z), &colCube, 100 ); 
		draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, vCenter.z+vP1.z), &colCube, 100 );
		draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, vCenter.z+vP2.z), &colCube, 100 );
		
		draw_line3d ( vector ( vCenter.x-vP1.x, vCenter.y-vP1.y, vCenter.z+vP2.z), NULL,     100 ); 
		draw_line3d ( vector ( vCenter.x-vP2.x, vCenter.y-vP2.y, vCenter.z+vP2.z), &colCube, 100 ); 
		draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, vCenter.z+vP2.z), &colCube, 100 );
		draw_line3d ( vector ( vCenter.x+vP2.x, vCenter.y+vP2.y, vCenter.z+vP2.z), &colCube, 100 );
		draw_line3d ( vector ( vCenter.x-vP1.x, vCenter.y-vP1.y, vCenter.z+vP2.z), &colCube, 100 ); 
		draw_line3d ( vector ( vCenter.x-vP1.x, vCenter.y-vP1.y, vCenter.z+vP1.z), &colCube, 100 ); 
		
		draw_line3d ( vector ( vCenter.x+vP2.x, vCenter.y+vP2.y, vCenter.z+vP2.z), NULL,     100 );
		draw_line3d ( vector ( vCenter.x+vP2.x, vCenter.y+vP2.y, vCenter.z+vP1.z), &colCube, 100 );
		
		draw_line3d ( vector ( vCenter.x-vP2.x, vCenter.y-vP2.y, vCenter.z+vP2.z), NULL,     100 );
		draw_line3d ( vector ( vCenter.x-vP2.x, vCenter.y-vP2.y, vCenter.z+vP1.z), &colCube, 100 );
		
		wait(1);
	}
	
	sys_exit ( NULL );
}



Salud!

Re: check if entity is within rotated box [Re: txesmi] #462527
10/06/16 13:20
10/06/16 13:20
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Tnx txesmi, can vec2d_rotate also be changed so that it doesn't change the position of the vector and only the angle of the drawed selection box? I am using now for an 3d selection box too, where I already have the right the min/max vectors so vec2d_rotate messes up the position.

Through some dirty Acknex code grin I did manage to get 1 of the vectors right I think, but can't get the other one right:

Code:
VECTOR vOldP1, vDiff;
vec_set(vOldP1, vP1); //store
vec2d_rotate (vP1, camera.pan); //same as before
vec2d_rotate (vP2, camera.pan); //-
vec_diff(vDiff, vP2, vP1);
vec_set(vP1, vOldP1);
vec_set(vP2, vP1);
vec_add(vP2, vDiff); //now we have the correct vP2 XD


Last edited by Reconnoiter; 10/06/16 13:22.
Re: check if entity is within rotated box [Re: Reconnoiter] #462531
10/07/16 05:05
10/07/16 05:05
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
I am sorry but I don't get your question.

vec2d_rotate rotates a two-dimensional vector from its origin (x:0,y:0), same as vec_rotate does on a three-dimensional vector.
Code:
vec2d_rotate(v,ang) === vec_rotate(v,vector(ang,0,0))



In my last code example I computed the center and the bounds of a rectangular prism from two world space vectors considering the prism aligned to carthesian planes, then I rotate two simmetrical bounds vectors, been their origin the center of the prism.

Code:
VECTOR vMax, vMin;
vec_set ( &vMax, vector(400,200,200) );
vec_set ( &vMin, vector(0,0,0) );

VECTOR vCenter, vBounds;
vec_lerp ( &vCenter, &vMax, &vMin, 0.5 );
vec_diff ( &vBounds, &vMax, &vCenter );

...

VECTOR vP1, vP2;
vec_set ( &vP1, &vBounds );
vec_set ( &vP2, vector(vBounds.x,-vBounds.y,-vBounds.z) );
vec2d_rotate ( &vP1, nRotation );
vec2d_rotate ( &vP2, nRotation );


Page 1 of 2 1 2

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1