Skip to content

Commit

Permalink
Feature/4.0/update core types (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho authored Jul 7, 2023
1 parent 10ab188 commit deadb5a
Show file tree
Hide file tree
Showing 28 changed files with 1,914 additions and 664 deletions.
10 changes: 5 additions & 5 deletions harness/tests/scripts/godot/tests/Invocation.gdj
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ registeredName = Invocation
fqName = godot.tests.Invocation
baseType = Node3D
supertypes = [
godot.Node3D,
godot.Node3D,
godot.Node,
godot.Object,
godot.core.KtObject,
kotlin.Any
]
signals = [
no_param,
no_param,
one_param,
two_param,
signal_with_multiple_targets
]
properties = [
button,
button,
enum_list,
vector_list,
enum_list_mutable,
Expand Down Expand Up @@ -76,7 +76,7 @@ properties = [
array
]
functions = [
target_function_one,
target_function_one,
target_function_two,
int_value,
long_value,
Expand Down Expand Up @@ -169,4 +169,4 @@ functions = [
nullable_string_is_null,
nullable_return_type,
create_variant_array_of_user_type
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BasisTest : Node() {

@RegisterFunction
fun getRotationQuat(basis: Basis): Quaternion {
return basis.getRotationQuat()
return basis.getRotationQuaternion()
}

@RegisterFunction
Expand Down
105 changes: 87 additions & 18 deletions kt/godot-library/src/main/kotlin/godot/core/AABB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -318,6 +324,21 @@ class AABB(
}
}

/**
* Returns `true` if the [AABB] has a surface or a length, and `false` if the [AABB] is empty (all components of
* [size] are zero or negative).
*/
fun hasSurface(): Boolean {
return (_size.x > CMP_EPSILON && _size.y > CMP_EPSILON && _size.z > CMP_EPSILON)
}

/**
* Returns `true` if the [AABB] has a volume, and `false` if the [AABB] is flat, empty, or has a negative [size].
*/
fun hasVolume(): Boolean {
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.
*/
Expand Down Expand Up @@ -396,6 +417,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."
}

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.
*/
Expand Down Expand Up @@ -445,6 +509,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.
*/
Expand Down
Loading

0 comments on commit deadb5a

Please sign in to comment.