-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/4.0/update core types #465
Changes from 17 commits
eaf54bb
d52f0e9
9964d7f
799f81e
daa0881
dc9b01c
b4dc0e7
dd402ba
b6085f4
3dce1f6
3ec6494
8e6ba61
0c06a3d
a833396
599dcfa
30e337c
499a0cb
91d6a6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package godot.core | |
import godot.annotation.CoreTypeHelper | ||
import godot.util.CMP_EPSILON | ||
import godot.util.RealT | ||
import kotlin.math.min | ||
|
||
|
||
class AABB( | ||
p_position: Vector3, | ||
|
@@ -73,6 +75,19 @@ class AABB( | |
this(other._position, other._size) | ||
|
||
//API | ||
/** | ||
* Returns an AABB with equivalent position and size, modified so that the most-negative corner is the origin and | ||
* the size is positive. | ||
*/ | ||
fun abs() = AABB( | ||
Vector3( | ||
position.x + min(size.x, 0.0), | ||
position.y + min(size.y, 0.0), | ||
position.z + min(size.z, 0.0) | ||
), | ||
size.abs() | ||
) | ||
|
||
/** | ||
* Returns true if this AABB completely encloses another one. | ||
*/ | ||
|
@@ -127,11 +142,9 @@ class AABB( | |
} | ||
|
||
/** | ||
* Returns the volume of the AABB. | ||
* Returns the center of the [AABB], which is equal to [position] + ([size] / 2). | ||
*/ | ||
fun getArea(): RealT { | ||
return _size.x * _size.y * _size.z | ||
} | ||
fun getCenter() = position + (size * 0.5) | ||
|
||
/** | ||
* Gets the position of the 8 endpoints of the AABB in space. | ||
|
@@ -271,6 +284,13 @@ class AABB( | |
) + ofs | ||
} | ||
|
||
/** | ||
* Returns the volume of the AABB. | ||
*/ | ||
fun getVolume(): RealT { | ||
return _size.x * _size.y * _size.z | ||
} | ||
|
||
/** | ||
* Returns a copy of the AABB grown a given amount of units towards all the sides. | ||
*/ | ||
|
@@ -289,20 +309,6 @@ class AABB( | |
_size.z += 2.0 * amount | ||
} | ||
|
||
/** | ||
* Returns true if the AABB is flat or empty. | ||
*/ | ||
fun hasNoArea(): Boolean { | ||
return (_size.x <= CMP_EPSILON || _size.y <= CMP_EPSILON || _size.z <= CMP_EPSILON) | ||
} | ||
|
||
/** | ||
* Returns true if the AABB is empty. | ||
*/ | ||
fun hasNoSurface(): Boolean { | ||
return (_size.x <= CMP_EPSILON && _size.y <= CMP_EPSILON && _size.z <= CMP_EPSILON) | ||
} | ||
|
||
/** | ||
* Returns true if the AABB contains a point. | ||
*/ | ||
|
@@ -318,6 +324,20 @@ class AABB( | |
} | ||
} | ||
|
||
/** | ||
* Returns true if the AABB is empty. | ||
*/ | ||
fun hasSurface(): Boolean { | ||
return (_size.x > CMP_EPSILON && _size.y > CMP_EPSILON && _size.z > CMP_EPSILON) | ||
} | ||
|
||
/** | ||
* Returns true if the AABB is flat or empty. | ||
*/ | ||
fun hasVolume(): Boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
return (_size.x > CMP_EPSILON || _size.y > CMP_EPSILON || _size.z > CMP_EPSILON) | ||
} | ||
|
||
/** | ||
* Returns the intersection between two AABB. An empty AABB (size 0,0,0) is returned on failure. | ||
*/ | ||
|
@@ -396,6 +416,49 @@ class AABB( | |
return under && over | ||
} | ||
|
||
/** | ||
* Returns `true` if the given ray intersects with this [AABB]. Ray length is infinite. | ||
*/ | ||
fun intersectsRay(from: Vector3, dir: Vector3): Boolean { | ||
require(size.x >= 0 && size.y >= 0 && size.z >= 0) { | ||
"AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size." | ||
} | ||
Comment on lines
+424
to
+426
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a good idea to crash here? Shouldn't we log an error and return false instead? Same for other usages of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should better explain to jetbrains they have a vision regarding preprocessors that is not compatible with game industry. |
||
|
||
var c1 = Vector3() | ||
var c2 = Vector3() | ||
val end = position + size | ||
var near = -1e20 | ||
var far = 1e20 | ||
var axis = 0 | ||
|
||
for (i in 0..2) { | ||
if (dir[i] == 0.0) { | ||
if (from[i] < position[i] || from[i] > end[i]) { | ||
return false | ||
} | ||
} else { // ray not parallel to planes in this direction | ||
c1[i] = (position[i] - from[i]) / dir[i] | ||
c2[i] = (end[i] - from[i]) / dir[i] | ||
if (c1[i] > c2[i]) { | ||
val aux = c1 | ||
c1 = c2 | ||
c2 = aux | ||
} | ||
if (c1[i] > near) { | ||
near = c1[i] | ||
} | ||
if (c2[i] < far) { | ||
far = c2[i] | ||
} | ||
if (near > far || far < 0) { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Returns true if the AABB intersects the line segment between from and to. | ||
*/ | ||
|
@@ -445,6 +508,11 @@ class AABB( | |
return this._position.isEqualApprox(other._position) && this._size.isEqualApprox(other._size) | ||
} | ||
|
||
/** | ||
* Returns `true` if this [AABB] is finite, by calling [Vector3.isFinite] on each component. | ||
*/ | ||
fun isFinite() = position.isFinite() && size.isFinite() | ||
|
||
/** | ||
* Returns a larger AABB that contains both this AABB and with. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment is incorrect, it returns false if empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Former comment that I did not checked. Will change it.