3 registered members (AndrewAMD, Quad, 1 invisible),
927
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: check if entity is within rotated box
[Re: txesmi]
#462538
10/07/16 10:06
10/07/16 10:06
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
I think I can explain this the best way through images. The grid shows how the camera.pan is. The red/yellow/green lines are my drawed boxes. Green mouse cursors are the begin and end points of my mouse selection. (note that boxes are 2d on the images but 3d in my game) First one without your vec2d_rotate function: Second is with your vec2d_rotate function: Third, this is with your vec2d_rotate function and with the tweak I posted in my previous post (only 1 of the 2 vectors are correct): Fourth, this is what I want:
|
|
|
Re: check if entity is within rotated box
[Re: Reconnoiter]
#462544
10/07/16 16:12
10/07/16 16:12
|
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
|
Ok, I got it! The only difference with my previous example is that you need to convert the bounds vector to that pseudo view space before computing the symmetric point of the rectangle. Here you go:
#include <acknex.h>
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 mouseOnZ0 ( VECTOR *_v )
{
vec_set ( _v, &mouse_dir3d );
vec_scale ( _v, -camera->z/_v->z );
vec_add ( _v, &camera->x );
}
void onMouseLeft ()
{
VECTOR vStart, vEnd;
mouseOnZ0 ( &vStart );
while ( mouse_left )
{
mouseOnZ0 ( &vEnd );
VECTOR vCenter, vBounds;
vec_lerp ( &vCenter, &vStart, &vEnd, 0.5 );
vec_diff ( &vBounds, &vStart, &vCenter );
vec2d_rotate ( &vBounds, -camera->pan );
VECTOR vP1, vP2;
vec_set ( &vP1, &vBounds );
vec_set ( &vP2, vector(vBounds.x,-vBounds.y,-vBounds.z) );
vec2d_rotate ( &vP1, camera->pan );
vec2d_rotate ( &vP2, camera->pan );
draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, 0), COLOR_RED, 0 );
draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, 0), COLOR_RED, 100 );
draw_line3d ( vector ( vCenter.x+vP2.x, vCenter.y+vP2.y, 0), COLOR_RED, 100 );
draw_line3d ( vector ( vCenter.x-vP1.x, vCenter.y-vP1.y, 0), COLOR_RED, 100 );
draw_line3d ( vector ( vCenter.x-vP2.x, vCenter.y-vP2.y, 0), COLOR_RED, 100 );
draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, 0), COLOR_RED, 100 );
draw_line3d ( vector ( vCenter.x+vP1.x, vCenter.y+vP1.y, 0), COLOR_RED, 0 );
wait(1);
}
}
void main ()
{
video_mode = 10;
mouse_mode = 4;
wait(1);
level_load ( "" );
camera->tilt = -30;
camera->pan = 30;
on_mouse_left = onMouseLeft;
while ( !key_esc )
{
camera->pan += key_force.x * 10 * time_step;
vec_for_angle ( &camera->x, &camera->pan );
vec_scale ( &camera->x, -1000 );
// World space axis
draw_line3d ( vector(1000,0,0), COLOR_WHITE, 0 );
draw_line3d ( vector(1000,0,0), COLOR_WHITE, 100 );
draw_line3d ( vector(-1000,0,0), COLOR_WHITE, 100 );
draw_line3d ( vector(-1000,0,0), COLOR_WHITE, 0 );
draw_line3d ( vector(0,1000,0), COLOR_WHITE, 0 );
draw_line3d ( vector(0,1000,0), COLOR_WHITE, 100 );
draw_line3d ( vector(0,-1000,0), COLOR_WHITE, 100 );
draw_line3d ( vector(0,-1000,0), COLOR_WHITE, 0 );
wait(1);
}
sys_exit ( NULL );
}
Salud! edited______________ a bit faster version
VECTOR vCenter, vBounds;
vec_lerp ( &vCenter, &vStart, &vEnd, 0.5 );
vec_diff ( &vBounds, &vStart, &vCenter );
VECTOR vP1, vP2;
vec_set ( &vP1, &vBounds );
vec_set ( &vP2, &vBounds );
vec2d_rotate ( &vP2, -camera->pan );
vP2.y *= -1;
vP2.z *= -1;
vec2d_rotate ( &vP2, camera->pan );
Last edited by txesmi; 10/08/16 07:21.
|
|
|
Re: check if entity is within rotated box
[Re: txesmi]
#462551
10/08/16 11:53
10/08/16 11:53
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Works like a charm now, tnx! For others; if anyone has now problems with selecting entities within the bounds add this line before comparing if the enitity's vOffset is within/below the selectionbox's vBounds: vec2d_rotate (vBounds, -camera.pan);
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|