1 registered members (tomaslolo),
1,542
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
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
OP
Serious User
|
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:
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
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
yes you are In the case your cubes are perfect, there is a simplified way to compute vertex positions.
90 degrees rotation
x' = -y;
y' = x;
so you should draw your square as follows
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
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Okay I am almost there , 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):
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]
#462454
10/01/16 06:17
10/01/16 06:17
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
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.
#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
OP
Serious User
|
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 I did manage to get 1 of the vectors right I think, but can't get the other one right:
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
Serious User
|
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.
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.
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 );
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|