# Legacy GM3D Bounding Box [SOLVED]

#### Prometheus1998_

##### Member
I'm trying to create a function analogous to point_in_rectangle() for checking if an object is in a 3D area.

My idea was to, first, check if the distance between the center of the region and the object being checked is less than or equal to one half of the diagonal of the cube (the diameter of a sphere touching the corners), using the function "point_distance_3d." Then, if that function returns true, the script would calculate if the object is within a rectangle using "point_in_rectangle" as the second step of the operation.

For some reason or another, it does not center on the area it's supposed to. It actually appears to be offset by a full 144 pixels.

I've tried rewriting the script with different coordinates, including calculating the cube from its center point, but it consistently returns with incorrect results. What am I missing?

Take 1:
Code:
``````///point_in_cube(point_x,point_y,point_z,x1,y1,z1,x2,y2,z2)

var point_x = argument0;
var point_y = argument1;
var point_z = argument2;
var x1 = argument3;
var y1 = argument4;
var z1 = argument5;
var x2 = argument6;
var y2 = argument7;
var z2 = argument8;

//Calculate center of the cube
var xc, yc, zc;
xc = round((x1 + x2) * 0.5);
yc = round((y1 + y2) * 0.5);
zc = round((z1 + z2) * 0.5);

//Get distance between top left corner and the center of the cube
var dist1 = point_distance_3d(x1,y1,z1,xc,yc,zc);
//Get distance between the target object and the center of the cube
var dist2 = point_distance_3d(point_x,point_y,point_z,xc,yc,zc);

//Check if the target object is within the allowed distance
if dist2 <= dist1{
//If it is, check if it is also within the rectangle
if point_in_rectangle(point_x,point_y,x1,y1,x2,y2){
//If it is, return true. In all other cases, return false.
return true;
}
else{
return false;
}
}
else{
return false;
}``````

Take 2:
t_size is 32
c_size = 8
Code:
``````///point_in_cube(point_x,point_y,point_z)

//Formula for diagonal of a cube
var d1 = (sqrt(3) * (c_size * t_size)) * 0.5;

//Calculate the top face of the bounding box
var x1, y1, x2, y2;
x1 = self.x - ((c_size * t_size) * 0.5);
y1 = self.y - ((c_size * t_size) * 0.5);
x2 = self.x + ((c_size * t_size) * 0.5);
y2 = self.y + ((c_size * t_size) * 0.5);

//Check if the target object is within the allowed distance
if point_distance_3d(argument0,argument1,argument2,self.x,self.y,self.z) <= d1{
//If it is, check if it is also within the rectangle
if point_in_rectangle(argument0,argument1,x1,y1,x2,y2){
//If it is, return true. In all other cases, return false.
return true;
}
else{
return false;
}
}
else{
return false;
}``````

My current thought is that it's treating the bottom left corner as the center of the box, but I can't find what might be causing that.

EDIT:
I was overcomplicating things. All I needed to do was check if the point is in a 2d rectangle and then test its z value; if it's between z1 and z2, then the point is in the box.
Code:
``````if point_in_rectangle(px,py,x1,y1,x2,y2){
if is_between(pz,z1,z2) = true{
return true;
}
else{
return false;
}
}
else{
return false;
}``````
is_between uses ShadeX91's code to evaluate if x1 < n < x2, found here: https://www.reddit.com/r/gamemaker/..._if_a_value_is_in_range_between_other/d68j5ns

Last edited: