2017-11-04 20:52:59 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* shape_bullet.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-05 00:50:27 +01:00
|
|
|
/* https://godotengine.org */
|
2017-11-04 20:52:59 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-11-04 20:52:59 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "shape_bullet.h"
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
#include "btRayShape.h"
|
|
|
|
#include "bullet_physics_server.h"
|
|
|
|
#include "bullet_types_converter.h"
|
|
|
|
#include "bullet_utilities.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2017-11-04 20:52:59 +01:00
|
|
|
#include "shape_owner_bullet.h"
|
|
|
|
|
2018-08-31 09:40:50 +02:00
|
|
|
#include <BulletCollision/CollisionDispatch/btInternalEdgeUtility.h>
|
2018-01-05 00:50:27 +01:00
|
|
|
#include <BulletCollision/CollisionShapes/btConvexPointCloudShape.h>
|
|
|
|
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
|
|
|
|
#include <btBulletCollisionCommon.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
ShapeBullet::ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
ShapeBullet::~ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
2018-07-10 14:50:14 +02:00
|
|
|
btCollisionShape *ShapeBullet::create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-12-23 18:23:12 +01:00
|
|
|
btVector3 s;
|
|
|
|
G_TO_B(p_implicit_scale, s);
|
2018-07-10 14:50:14 +02:00
|
|
|
return create_bt_shape(s, p_extra_edge);
|
2017-12-23 18:23:12 +01:00
|
|
|
}
|
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
btCollisionShape *ShapeBullet::prepare(btCollisionShape *p_btShape) const {
|
|
|
|
p_btShape->setUserPointer(const_cast<ShapeBullet *>(this));
|
2018-07-10 14:50:14 +02:00
|
|
|
p_btShape->setMargin(margin);
|
2017-11-04 20:52:59 +01:00
|
|
|
return p_btShape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeBullet::notifyShapeChanged() {
|
|
|
|
for (Map<ShapeOwnerBullet *, int>::Element *E = owners.front(); E; E = E->next()) {
|
2018-10-06 16:50:10 +02:00
|
|
|
ShapeOwnerBullet *owner = static_cast<ShapeOwnerBullet *>(E->key());
|
|
|
|
owner->shape_changed(owner->find_shape(this));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeBullet::add_owner(ShapeOwnerBullet *p_owner) {
|
|
|
|
Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
|
|
|
|
if (E) {
|
|
|
|
E->get()++;
|
|
|
|
} else {
|
|
|
|
owners[p_owner] = 1; // add new owner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeBullet::remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody) {
|
|
|
|
Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!E) {
|
2020-05-10 12:56:01 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-11-04 20:52:59 +01:00
|
|
|
E->get()--;
|
|
|
|
if (p_permanentlyFromThisBody || 0 >= E->get()) {
|
|
|
|
owners.erase(E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShapeBullet::is_owner(ShapeOwnerBullet *p_owner) const {
|
|
|
|
return owners.has(p_owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Map<ShapeOwnerBullet *, int> &ShapeBullet::get_owners() const {
|
|
|
|
return owners;
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:50:14 +02:00
|
|
|
void ShapeBullet::set_margin(real_t p_margin) {
|
|
|
|
margin = p_margin;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t ShapeBullet::get_margin() const {
|
|
|
|
return margin;
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
btEmptyShape *ShapeBullet::create_shape_empty() {
|
|
|
|
return bulletnew(btEmptyShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
btStaticPlaneShape *ShapeBullet::create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant) {
|
|
|
|
return bulletnew(btStaticPlaneShape(planeNormal, planeConstant));
|
|
|
|
}
|
|
|
|
|
|
|
|
btSphereShape *ShapeBullet::create_shape_sphere(btScalar radius) {
|
|
|
|
return bulletnew(btSphereShape(radius));
|
|
|
|
}
|
|
|
|
|
|
|
|
btBoxShape *ShapeBullet::create_shape_box(const btVector3 &boxHalfExtents) {
|
|
|
|
return bulletnew(btBoxShape(boxHalfExtents));
|
|
|
|
}
|
|
|
|
|
2020-02-23 19:41:31 +01:00
|
|
|
btCapsuleShape *ShapeBullet::create_shape_capsule(btScalar radius, btScalar height) {
|
|
|
|
return bulletnew(btCapsuleShape(radius, height));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:53:28 +02:00
|
|
|
btCylinderShape *ShapeBullet::create_shape_cylinder(btScalar radius, btScalar height) {
|
|
|
|
return bulletnew(btCylinderShape(btVector3(radius, height / 2.0, radius)));
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
btConvexPointCloudShape *ShapeBullet::create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling) {
|
|
|
|
return bulletnew(btConvexPointCloudShape(&p_vertices[0], p_vertices.size(), p_local_scaling));
|
|
|
|
}
|
|
|
|
|
|
|
|
btScaledBvhTriangleMeshShape *ShapeBullet::create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling) {
|
|
|
|
if (p_mesh_shape) {
|
|
|
|
return bulletnew(btScaledBvhTriangleMeshShape(p_mesh_shape, p_local_scaling));
|
|
|
|
} else {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
|
2017-11-04 20:52:59 +01:00
|
|
|
const btScalar ignoredHeightScale(1);
|
|
|
|
const int YAxis = 1; // 0=X, 1=Y, 2=Z
|
|
|
|
const bool flipQuadEdges = false;
|
2020-02-17 22:06:54 +01:00
|
|
|
const void *heightsPtr = p_heights.ptr();
|
2017-11-04 20:52:59 +01:00
|
|
|
|
2019-05-16 10:18:48 +02:00
|
|
|
btHeightfieldTerrainShape *heightfield = bulletnew(btHeightfieldTerrainShape(p_width, p_depth, heightsPtr, ignoredHeightScale, p_min_height, p_max_height, YAxis, PHY_FLOAT, flipQuadEdges));
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
// The shape can be created without params when you do PhysicsServer3D.shape_create(PhysicsServer3D.SHAPE_HEIGHTMAP)
|
2020-05-14 16:41:43 +02:00
|
|
|
if (heightsPtr) {
|
2019-05-16 10:18:48 +02:00
|
|
|
heightfield->buildAccelerator(16);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-05-16 10:18:48 +02:00
|
|
|
|
|
|
|
return heightfield;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:59:57 +01:00
|
|
|
btRayShape *ShapeBullet::create_shape_ray(real_t p_length, bool p_slips_on_slope) {
|
|
|
|
btRayShape *r(bulletnew(btRayShape(p_length)));
|
|
|
|
r->setSlipsOnSlope(p_slips_on_slope);
|
|
|
|
return r;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PLANE */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
PlaneShapeBullet::PlaneShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void PlaneShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant PlaneShapeBullet::get_data() const {
|
|
|
|
return plane;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType PlaneShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_PLANE;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlaneShapeBullet::setup(const Plane &p_plane) {
|
|
|
|
plane = p_plane;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *PlaneShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-11-04 20:52:59 +01:00
|
|
|
btVector3 btPlaneNormal;
|
|
|
|
G_TO_B(plane.normal, btPlaneNormal);
|
2020-05-10 16:47:11 +02:00
|
|
|
return prepare(PlaneShapeBullet::create_shape_plane(btPlaneNormal, plane.d));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sphere */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
SphereShapeBullet::SphereShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void SphereShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant SphereShapeBullet::get_data() const {
|
|
|
|
return radius;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType SphereShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_SPHERE;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SphereShapeBullet::setup(real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *SphereShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-07-10 14:50:14 +02:00
|
|
|
return prepare(ShapeBullet::create_shape_sphere(radius * p_implicit_scale[0] + p_extra_edge));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Box */
|
2017-12-06 21:36:34 +01:00
|
|
|
BoxShapeBullet::BoxShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void BoxShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant BoxShapeBullet::get_data() const {
|
|
|
|
Vector3 g_half_extents;
|
|
|
|
B_TO_G(half_extents, g_half_extents);
|
|
|
|
return g_half_extents;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType BoxShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_BOX;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoxShapeBullet::setup(const Vector3 &p_half_extents) {
|
|
|
|
G_TO_B(p_half_extents, half_extents);
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *BoxShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-07-10 14:50:14 +02:00
|
|
|
return prepare(ShapeBullet::create_shape_box((half_extents * p_implicit_scale) + btVector3(p_extra_edge, p_extra_edge, p_extra_edge)));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Capsule */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
CapsuleShapeBullet::CapsuleShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void CapsuleShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
Dictionary d = p_data;
|
|
|
|
ERR_FAIL_COND(!d.has("radius"));
|
|
|
|
ERR_FAIL_COND(!d.has("height"));
|
|
|
|
setup(d["height"], d["radius"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant CapsuleShapeBullet::get_data() const {
|
|
|
|
Dictionary d;
|
|
|
|
d["radius"] = radius;
|
|
|
|
d["height"] = height;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType CapsuleShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_CAPSULE;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CapsuleShapeBullet::setup(real_t p_height, real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
height = p_height;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *CapsuleShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2020-10-08 17:19:34 +02:00
|
|
|
return prepare(ShapeBullet::create_shape_capsule(radius * p_implicit_scale[0] + p_extra_edge, height * p_implicit_scale[1]));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:53:28 +02:00
|
|
|
/* Cylinder */
|
|
|
|
|
|
|
|
CylinderShapeBullet::CylinderShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
|
|
|
|
|
|
|
void CylinderShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
Dictionary d = p_data;
|
|
|
|
ERR_FAIL_COND(!d.has("radius"));
|
|
|
|
ERR_FAIL_COND(!d.has("height"));
|
|
|
|
setup(d["height"], d["radius"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant CylinderShapeBullet::get_data() const {
|
|
|
|
Dictionary d;
|
|
|
|
d["radius"] = radius;
|
|
|
|
d["height"] = height;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType CylinderShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_CYLINDER;
|
2018-06-13 00:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CylinderShapeBullet::setup(real_t p_height, real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
height = p_height;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *CylinderShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin) {
|
2018-06-13 00:53:28 +02:00
|
|
|
return prepare(ShapeBullet::create_shape_cylinder(radius * p_implicit_scale[0] + p_margin, height * p_implicit_scale[1] + p_margin));
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
/* Convex polygon */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
ConvexPolygonShapeBullet::ConvexPolygonShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void ConvexPolygonShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvexPolygonShapeBullet::get_vertices(Vector<Vector3> &out_vertices) {
|
|
|
|
const int n_of_vertices = vertices.size();
|
|
|
|
out_vertices.resize(n_of_vertices);
|
|
|
|
for (int i = n_of_vertices - 1; 0 <= i; --i) {
|
2018-07-25 03:11:03 +02:00
|
|
|
B_TO_G(vertices[i], out_vertices.write[i]);
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant ConvexPolygonShapeBullet::get_data() const {
|
|
|
|
ConvexPolygonShapeBullet *variable_self = const_cast<ConvexPolygonShapeBullet *>(this);
|
|
|
|
Vector<Vector3> out_vertices;
|
|
|
|
variable_self->get_vertices(out_vertices);
|
|
|
|
return out_vertices;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType ConvexPolygonShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_CONVEX_POLYGON;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvexPolygonShapeBullet::setup(const Vector<Vector3> &p_vertices) {
|
2018-01-18 21:37:17 +01:00
|
|
|
// Make a copy of vertices
|
2017-11-04 20:52:59 +01:00
|
|
|
const int n_of_vertices = p_vertices.size();
|
|
|
|
vertices.resize(n_of_vertices);
|
|
|
|
for (int i = n_of_vertices - 1; 0 <= i; --i) {
|
|
|
|
G_TO_B(p_vertices[i], vertices[i]);
|
|
|
|
}
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *ConvexPolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!vertices.size()) {
|
2018-08-29 17:34:26 +02:00
|
|
|
// This is necessary since 0 vertices
|
|
|
|
return prepare(ShapeBullet::create_shape_empty());
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-12-23 18:23:12 +01:00
|
|
|
btCollisionShape *cs(ShapeBullet::create_shape_convex(vertices));
|
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
|
|
|
return cs;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Concave polygon */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
ConcavePolygonShapeBullet::ConcavePolygonShapeBullet() :
|
2020-05-12 17:01:17 +02:00
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
ConcavePolygonShapeBullet::~ConcavePolygonShapeBullet() {
|
|
|
|
if (meshShape) {
|
|
|
|
delete meshShape->getMeshInterface();
|
2018-08-31 09:40:50 +02:00
|
|
|
delete meshShape->getTriangleInfoMap();
|
|
|
|
bulletdelete(meshShape);
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
2020-02-17 22:06:54 +01:00
|
|
|
faces = Vector<Vector3>();
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConcavePolygonShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant ConcavePolygonShapeBullet::get_data() const {
|
|
|
|
return faces;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType ConcavePolygonShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_CONCAVE_POLYGON;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
void ConcavePolygonShapeBullet::setup(Vector<Vector3> p_faces) {
|
2017-11-04 20:52:59 +01:00
|
|
|
faces = p_faces;
|
|
|
|
if (meshShape) {
|
|
|
|
/// Clear previous created shape
|
|
|
|
delete meshShape->getMeshInterface();
|
2018-08-31 09:40:50 +02:00
|
|
|
delete meshShape->getTriangleInfoMap();
|
2017-11-04 20:52:59 +01:00
|
|
|
bulletdelete(meshShape);
|
|
|
|
}
|
|
|
|
int src_face_count = faces.size();
|
|
|
|
if (0 < src_face_count) {
|
|
|
|
// It counts the faces and assert the array contains the correct number of vertices.
|
|
|
|
ERR_FAIL_COND(src_face_count % 3);
|
2018-04-02 08:29:34 +02:00
|
|
|
|
|
|
|
btTriangleMesh *shapeInterface = bulletnew(btTriangleMesh);
|
2017-11-04 20:52:59 +01:00
|
|
|
src_face_count /= 3;
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector3 *r = p_faces.ptr();
|
|
|
|
const Vector3 *facesr = r;
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
btVector3 supVec_0;
|
|
|
|
btVector3 supVec_1;
|
|
|
|
btVector3 supVec_2;
|
|
|
|
for (int i = 0; i < src_face_count; ++i) {
|
2018-10-05 15:15:54 +02:00
|
|
|
G_TO_B(facesr[i * 3 + 0], supVec_0);
|
2017-11-04 20:52:59 +01:00
|
|
|
G_TO_B(facesr[i * 3 + 1], supVec_1);
|
|
|
|
G_TO_B(facesr[i * 3 + 2], supVec_2);
|
|
|
|
|
2018-10-05 15:15:54 +02:00
|
|
|
// Inverted from standard godot otherwise btGenerateInternalEdgeInfo generates wrong edge info
|
|
|
|
shapeInterface->addTriangle(supVec_2, supVec_1, supVec_0);
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool useQuantizedAabbCompression = true;
|
|
|
|
|
|
|
|
meshShape = bulletnew(btBvhTriangleMeshShape(shapeInterface, useQuantizedAabbCompression));
|
2018-10-05 15:15:54 +02:00
|
|
|
|
|
|
|
if (GLOBAL_DEF("physics/3d/smooth_trimesh_collision", false)) {
|
|
|
|
btTriangleInfoMap *triangleInfoMap = new btTriangleInfoMap();
|
|
|
|
btGenerateInternalEdgeInfo(meshShape, triangleInfoMap);
|
|
|
|
}
|
2017-11-04 20:52:59 +01:00
|
|
|
} else {
|
2020-04-02 01:20:12 +02:00
|
|
|
meshShape = nullptr;
|
2017-11-04 20:52:59 +01:00
|
|
|
ERR_PRINT("The faces count are 0, the mesh shape cannot be created");
|
|
|
|
}
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *ConcavePolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-11-04 20:52:59 +01:00
|
|
|
btCollisionShape *cs = ShapeBullet::create_shape_concave(meshShape);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!cs) {
|
2020-04-02 01:20:12 +02:00
|
|
|
// This is necessary since if 0 faces the creation of concave return null
|
2017-11-04 20:52:59 +01:00
|
|
|
cs = ShapeBullet::create_shape_empty();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-12-23 18:23:12 +01:00
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
2018-10-05 15:15:54 +02:00
|
|
|
cs->setMargin(0);
|
2017-12-23 18:23:12 +01:00
|
|
|
return cs;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Height map shape */
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
HeightMapShapeBullet::HeightMapShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void HeightMapShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
|
|
|
|
Dictionary d = p_data;
|
|
|
|
ERR_FAIL_COND(!d.has("width"));
|
|
|
|
ERR_FAIL_COND(!d.has("depth"));
|
|
|
|
ERR_FAIL_COND(!d.has("heights"));
|
|
|
|
|
2018-03-27 21:45:27 +02:00
|
|
|
real_t l_min_height = 0.0;
|
|
|
|
real_t l_max_height = 0.0;
|
|
|
|
|
|
|
|
// If specified, min and max height will be used as precomputed values
|
2020-05-14 16:41:43 +02:00
|
|
|
if (d.has("min_height")) {
|
2018-03-27 21:45:27 +02:00
|
|
|
l_min_height = d["min_height"];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (d.has("max_height")) {
|
2018-03-27 21:45:27 +02:00
|
|
|
l_max_height = d["max_height"];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-03-27 21:45:27 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(l_min_height > l_max_height);
|
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
int l_width = d["width"];
|
|
|
|
int l_depth = d["depth"];
|
2018-08-05 20:43:47 +02:00
|
|
|
|
2020-07-24 13:08:01 +02:00
|
|
|
ERR_FAIL_COND_MSG(l_width < 2, "Map width must be at least 2.");
|
|
|
|
ERR_FAIL_COND_MSG(l_depth < 2, "Map depth must be at least 2.");
|
|
|
|
|
2018-08-05 20:43:47 +02:00
|
|
|
// TODO This code will need adjustments if real_t is set to `double`,
|
|
|
|
// because that precision is unnecessary for a heightmap and Bullet doesn't support it...
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<real_t> l_heights;
|
2018-08-05 20:43:47 +02:00
|
|
|
Variant l_heights_v = d["heights"];
|
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
if (l_heights_v.get_type() == Variant::PACKED_FLOAT32_ARRAY) {
|
2018-08-05 20:43:47 +02:00
|
|
|
// Ready-to-use heights can be passed
|
|
|
|
|
|
|
|
l_heights = l_heights_v;
|
|
|
|
|
|
|
|
} else if (l_heights_v.get_type() == Variant::OBJECT) {
|
|
|
|
// If an image is passed, we have to convert it to a format Bullet supports.
|
|
|
|
// this would be expensive to do with a script, so it's nice to have it here.
|
|
|
|
|
|
|
|
Ref<Image> l_image = l_heights_v;
|
|
|
|
ERR_FAIL_COND(l_image.is_null());
|
|
|
|
|
|
|
|
// Float is the only common format between Godot and Bullet that can be used for decent collision.
|
|
|
|
// (Int16 would be nice too but we still don't have it)
|
|
|
|
// We could convert here automatically but it's better to not be intrusive and let the caller do it if necessary.
|
|
|
|
ERR_FAIL_COND(l_image->get_format() != Image::FORMAT_RF);
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray im_data = l_image->get_data();
|
2018-08-05 20:43:47 +02:00
|
|
|
|
2018-12-27 10:29:04 +01:00
|
|
|
l_heights.resize(l_image->get_width() * l_image->get_height());
|
2018-08-05 20:43:47 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
real_t *w = l_heights.ptrw();
|
|
|
|
const uint8_t *r = im_data.ptr();
|
|
|
|
float *rp = (float *)r;
|
2018-08-05 20:43:47 +02:00
|
|
|
// At this point, `rp` could be used directly for Bullet, but I don't know how safe it would be.
|
|
|
|
|
|
|
|
for (int i = 0; i < l_heights.size(); ++i) {
|
|
|
|
w[i] = rp[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ERR_FAIL_MSG("Expected PackedFloat32Array or float Image.");
|
2018-08-05 20:43:47 +02:00
|
|
|
}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(l_width <= 0);
|
|
|
|
ERR_FAIL_COND(l_depth <= 0);
|
2018-03-27 21:45:27 +02:00
|
|
|
ERR_FAIL_COND(l_heights.size() != (l_width * l_depth));
|
|
|
|
|
|
|
|
// Compute min and max heights if not specified.
|
|
|
|
if (!d.has("min_height") && !d.has("max_height")) {
|
2020-02-17 22:06:54 +01:00
|
|
|
const real_t *r = l_heights.ptr();
|
2019-04-04 22:22:18 +02:00
|
|
|
int heights_size = l_heights.size();
|
2018-03-27 21:45:27 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < heights_size; ++i) {
|
|
|
|
real_t h = r[i];
|
|
|
|
|
2019-04-04 22:22:18 +02:00
|
|
|
if (h < l_min_height) {
|
2018-03-27 21:45:27 +02:00
|
|
|
l_min_height = h;
|
2019-04-04 22:22:18 +02:00
|
|
|
} else if (h > l_max_height) {
|
2018-03-27 21:45:27 +02:00
|
|
|
l_max_height = h;
|
2019-04-04 22:22:18 +02:00
|
|
|
}
|
2018-03-27 21:45:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(l_heights, l_width, l_depth, l_min_height, l_max_height);
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant HeightMapShapeBullet::get_data() const {
|
|
|
|
ERR_FAIL_V(Variant());
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType HeightMapShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_HEIGHTMAP;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
void HeightMapShapeBullet::setup(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
|
2018-03-27 21:45:27 +02:00
|
|
|
// TODO cell size must be tweaked using localScaling, which is a shared property for all Bullet shapes
|
|
|
|
|
2018-08-05 20:43:47 +02:00
|
|
|
// If this array is resized outside of here, it should be preserved due to CoW
|
|
|
|
heights = p_heights;
|
2018-03-27 21:45:27 +02:00
|
|
|
|
2017-11-04 20:52:59 +01:00
|
|
|
width = p_width;
|
|
|
|
depth = p_depth;
|
2018-03-27 21:45:27 +02:00
|
|
|
min_height = p_min_height;
|
|
|
|
max_height = p_max_height;
|
2017-11-04 20:52:59 +01:00
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *HeightMapShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-03-27 21:45:27 +02:00
|
|
|
btCollisionShape *cs(ShapeBullet::create_shape_height_field(heights, width, depth, min_height, max_height));
|
2017-12-23 18:23:12 +01:00
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
|
|
|
return cs;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ray shape */
|
2017-12-06 21:36:34 +01:00
|
|
|
RayShapeBullet::RayShapeBullet() :
|
2020-05-12 17:01:17 +02:00
|
|
|
ShapeBullet() {}
|
2017-11-04 20:52:59 +01:00
|
|
|
|
|
|
|
void RayShapeBullet::set_data(const Variant &p_data) {
|
2018-02-19 20:59:57 +01:00
|
|
|
Dictionary d = p_data;
|
|
|
|
setup(d["length"], d["slips_on_slope"]);
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant RayShapeBullet::get_data() const {
|
2018-02-19 20:59:57 +01:00
|
|
|
Dictionary d;
|
|
|
|
d["length"] = length;
|
|
|
|
d["slips_on_slope"] = slips_on_slope;
|
|
|
|
return d;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType RayShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer3D::SHAPE_RAY;
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 20:59:57 +01:00
|
|
|
void RayShapeBullet::setup(real_t p_length, bool p_slips_on_slope) {
|
2017-11-04 20:52:59 +01:00
|
|
|
length = p_length;
|
2018-02-19 20:59:57 +01:00
|
|
|
slips_on_slope = p_slips_on_slope;
|
2017-11-04 20:52:59 +01:00
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:22:59 +02:00
|
|
|
btCollisionShape *RayShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-07-10 14:50:14 +02:00
|
|
|
return prepare(ShapeBullet::create_shape_ray(length * p_implicit_scale[1] + p_extra_edge, slips_on_slope));
|
2017-11-04 20:52:59 +01:00
|
|
|
}
|