2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* shape_bullet.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2018-01-04 23:50:27 +00:00
|
|
|
/* https://godotengine.org */
|
2017-11-04 19:52:59 +00:00
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-11-04 19:52:59 +00: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-04 23:50:27 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
#include "btRayShape.h"
|
|
|
|
#include "bullet_physics_server.h"
|
|
|
|
#include "bullet_types_converter.h"
|
|
|
|
#include "bullet_utilities.h"
|
2018-10-05 13:15:54 +00:00
|
|
|
#include "core/project_settings.h"
|
2017-11-04 19:52:59 +00:00
|
|
|
#include "shape_owner_bullet.h"
|
|
|
|
|
2018-08-31 07:40:50 +00:00
|
|
|
#include <BulletCollision/CollisionDispatch/btInternalEdgeUtility.h>
|
2018-01-04 23:50:27 +00:00
|
|
|
#include <BulletCollision/CollisionShapes/btConvexPointCloudShape.h>
|
|
|
|
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
|
|
|
|
#include <btBulletCollisionCommon.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@author AndreaCatania
|
|
|
|
*/
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
ShapeBullet::ShapeBullet() :
|
|
|
|
margin(0.04) {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
ShapeBullet::~ShapeBullet() {}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *ShapeBullet::create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-12-23 17:23:12 +00:00
|
|
|
btVector3 s;
|
|
|
|
G_TO_B(p_implicit_scale, s);
|
2018-07-10 12:50:14 +00:00
|
|
|
return create_bt_shape(s, p_extra_edge);
|
2017-12-23 17:23:12 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
btCollisionShape *ShapeBullet::prepare(btCollisionShape *p_btShape) const {
|
|
|
|
p_btShape->setUserPointer(const_cast<ShapeBullet *>(this));
|
2018-07-10 12:50:14 +00:00
|
|
|
p_btShape->setMargin(margin);
|
2017-11-04 19:52:59 +00:00
|
|
|
return p_btShape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShapeBullet::notifyShapeChanged() {
|
|
|
|
for (Map<ShapeOwnerBullet *, int>::Element *E = owners.front(); E; E = E->next()) {
|
2018-10-06 14:50:10 +00:00
|
|
|
ShapeOwnerBullet *owner = static_cast<ShapeOwnerBullet *>(E->key());
|
|
|
|
owner->shape_changed(owner->find_shape(this));
|
2017-11-04 19:52:59 +00: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);
|
2018-01-08 15:49:04 +00:00
|
|
|
if (!E) return;
|
2017-11-04 19:52:59 +00: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 12:50:14 +00:00
|
|
|
void ShapeBullet::set_margin(real_t p_margin) {
|
|
|
|
margin = p_margin;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t ShapeBullet::get_margin() const {
|
|
|
|
return margin;
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:52:59 +00: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
btCapsuleShapeZ *ShapeBullet::create_shape_capsule(btScalar radius, btScalar height) {
|
|
|
|
return bulletnew(btCapsuleShapeZ(radius, height));
|
|
|
|
}
|
|
|
|
|
2018-06-12 22:53:28 +00:00
|
|
|
btCylinderShape *ShapeBullet::create_shape_cylinder(btScalar radius, btScalar height) {
|
|
|
|
return bulletnew(btCylinderShape(btVector3(radius, height / 2.0, radius)));
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:52:59 +00: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 {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:45:27 +00:00
|
|
|
btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
|
2017-11-04 19:52:59 +00:00
|
|
|
const btScalar ignoredHeightScale(1);
|
|
|
|
const int YAxis = 1; // 0=X, 1=Y, 2=Z
|
|
|
|
const bool flipQuadEdges = false;
|
|
|
|
const void *heightsPtr = p_heights.read().ptr();
|
|
|
|
|
2018-03-27 19:45:27 +00:00
|
|
|
return bulletnew(btHeightfieldTerrainShape(p_width, p_depth, heightsPtr, ignoredHeightScale, p_min_height, p_max_height, YAxis, PHY_FLOAT, flipQuadEdges));
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 19:59:57 +00: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 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PLANE */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
PlaneShapeBullet::PlaneShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void PlaneShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant PlaneShapeBullet::get_data() const {
|
|
|
|
return plane;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType PlaneShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_PLANE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaneShapeBullet::setup(const Plane &p_plane) {
|
|
|
|
plane = p_plane;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *PlaneShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-11-04 19:52:59 +00:00
|
|
|
btVector3 btPlaneNormal;
|
|
|
|
G_TO_B(plane.normal, btPlaneNormal);
|
|
|
|
return prepare(PlaneShapeBullet::create_shape_plane(btPlaneNormal, plane.d));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sphere */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
SphereShapeBullet::SphereShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void SphereShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant SphereShapeBullet::get_data() const {
|
|
|
|
return radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType SphereShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_SPHERE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SphereShapeBullet::setup(real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *SphereShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
|
|
|
return prepare(ShapeBullet::create_shape_sphere(radius * p_implicit_scale[0] + p_extra_edge));
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Box */
|
2017-12-06 20:36:34 +00:00
|
|
|
BoxShapeBullet::BoxShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType BoxShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_BOX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoxShapeBullet::setup(const Vector3 &p_half_extents) {
|
|
|
|
G_TO_B(p_half_extents, half_extents);
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *BoxShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
|
|
|
return prepare(ShapeBullet::create_shape_box((half_extents * p_implicit_scale) + btVector3(p_extra_edge, p_extra_edge, p_extra_edge)));
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Capsule */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
CapsuleShapeBullet::CapsuleShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType CapsuleShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_CAPSULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CapsuleShapeBullet::setup(real_t p_height, real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
height = p_height;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *CapsuleShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
|
|
|
return prepare(ShapeBullet::create_shape_capsule(radius * p_implicit_scale[0] + p_extra_edge, height * p_implicit_scale[1] + p_extra_edge));
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 22:53:28 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType CylinderShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_CYLINDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CylinderShapeBullet::setup(real_t p_height, real_t p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
height = p_height;
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
btCollisionShape *CylinderShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin) {
|
|
|
|
return prepare(ShapeBullet::create_shape_cylinder(radius * p_implicit_scale[0] + p_margin, height * p_implicit_scale[1] + p_margin));
|
|
|
|
}
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
/* Convex polygon */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
ConvexPolygonShapeBullet::ConvexPolygonShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00: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 01:11:03 +00:00
|
|
|
B_TO_G(vertices[i], out_vertices.write[i]);
|
2017-11-04 19:52:59 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType ConvexPolygonShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_CONVEX_POLYGON;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConvexPolygonShapeBullet::setup(const Vector<Vector3> &p_vertices) {
|
2018-01-18 20:37:17 +00:00
|
|
|
// Make a copy of vertices
|
2017-11-04 19:52:59 +00: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();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *ConvexPolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-08-29 15:34:26 +00:00
|
|
|
if (!vertices.size())
|
|
|
|
// This is necessary since 0 vertices
|
|
|
|
return prepare(ShapeBullet::create_shape_empty());
|
2017-12-23 17:23:12 +00:00
|
|
|
btCollisionShape *cs(ShapeBullet::create_shape_convex(vertices));
|
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
|
|
|
return cs;
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Concave polygon */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
ConcavePolygonShapeBullet::ConcavePolygonShapeBullet() :
|
|
|
|
ShapeBullet(),
|
|
|
|
meshShape(NULL) {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
ConcavePolygonShapeBullet::~ConcavePolygonShapeBullet() {
|
|
|
|
if (meshShape) {
|
|
|
|
delete meshShape->getMeshInterface();
|
2018-08-31 07:40:50 +00:00
|
|
|
delete meshShape->getTriangleInfoMap();
|
|
|
|
bulletdelete(meshShape);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
faces = PoolVector<Vector3>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcavePolygonShapeBullet::set_data(const Variant &p_data) {
|
|
|
|
setup(p_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant ConcavePolygonShapeBullet::get_data() const {
|
|
|
|
return faces;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType ConcavePolygonShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_CONCAVE_POLYGON;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConcavePolygonShapeBullet::setup(PoolVector<Vector3> p_faces) {
|
|
|
|
faces = p_faces;
|
|
|
|
if (meshShape) {
|
|
|
|
/// Clear previous created shape
|
|
|
|
delete meshShape->getMeshInterface();
|
2018-08-31 07:40:50 +00:00
|
|
|
delete meshShape->getTriangleInfoMap();
|
2017-11-04 19:52:59 +00: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 06:29:34 +00:00
|
|
|
|
|
|
|
btTriangleMesh *shapeInterface = bulletnew(btTriangleMesh);
|
2017-11-04 19:52:59 +00:00
|
|
|
src_face_count /= 3;
|
|
|
|
PoolVector<Vector3>::Read r = p_faces.read();
|
|
|
|
const Vector3 *facesr = r.ptr();
|
|
|
|
|
|
|
|
btVector3 supVec_0;
|
|
|
|
btVector3 supVec_1;
|
|
|
|
btVector3 supVec_2;
|
|
|
|
for (int i = 0; i < src_face_count; ++i) {
|
2018-10-05 13:15:54 +00:00
|
|
|
G_TO_B(facesr[i * 3 + 0], supVec_0);
|
2017-11-04 19:52:59 +00:00
|
|
|
G_TO_B(facesr[i * 3 + 1], supVec_1);
|
|
|
|
G_TO_B(facesr[i * 3 + 2], supVec_2);
|
|
|
|
|
2018-10-05 13:15:54 +00:00
|
|
|
// Inverted from standard godot otherwise btGenerateInternalEdgeInfo generates wrong edge info
|
|
|
|
shapeInterface->addTriangle(supVec_2, supVec_1, supVec_0);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool useQuantizedAabbCompression = true;
|
|
|
|
|
|
|
|
meshShape = bulletnew(btBvhTriangleMeshShape(shapeInterface, useQuantizedAabbCompression));
|
2018-10-05 13:15:54 +00:00
|
|
|
|
|
|
|
if (GLOBAL_DEF("physics/3d/smooth_trimesh_collision", false)) {
|
|
|
|
btTriangleInfoMap *triangleInfoMap = new btTriangleInfoMap();
|
|
|
|
btGenerateInternalEdgeInfo(meshShape, triangleInfoMap);
|
|
|
|
}
|
2017-11-04 19:52:59 +00:00
|
|
|
} else {
|
|
|
|
meshShape = NULL;
|
|
|
|
ERR_PRINT("The faces count are 0, the mesh shape cannot be created");
|
|
|
|
}
|
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *ConcavePolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2017-11-04 19:52:59 +00:00
|
|
|
btCollisionShape *cs = ShapeBullet::create_shape_concave(meshShape);
|
2017-12-23 17:23:12 +00:00
|
|
|
if (!cs)
|
2017-11-04 19:52:59 +00:00
|
|
|
// This is necessary since if 0 faces the creation of concave return NULL
|
|
|
|
cs = ShapeBullet::create_shape_empty();
|
2017-12-23 17:23:12 +00:00
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
2018-10-05 13:15:54 +00:00
|
|
|
cs->setMargin(0);
|
2017-12-23 17:23:12 +00:00
|
|
|
return cs;
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Height map shape */
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
HeightMapShapeBullet::HeightMapShapeBullet() :
|
|
|
|
ShapeBullet() {}
|
2017-11-04 19:52:59 +00: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 19:45:27 +00: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
|
|
|
|
if (d.has("min_height"))
|
|
|
|
l_min_height = d["min_height"];
|
|
|
|
if (d.has("max_height"))
|
|
|
|
l_max_height = d["max_height"];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(l_min_height > l_max_height);
|
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
int l_width = d["width"];
|
|
|
|
int l_depth = d["depth"];
|
2018-08-05 18:43:47 +00: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...
|
|
|
|
|
|
|
|
PoolVector<real_t> l_heights;
|
|
|
|
Variant l_heights_v = d["heights"];
|
|
|
|
|
|
|
|
if (l_heights_v.get_type() == Variant::POOL_REAL_ARRAY) {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
PoolByteArray im_data = l_image->get_data();
|
|
|
|
|
|
|
|
l_heights.resize(l_image->get_width() * l_image->get_width());
|
|
|
|
|
|
|
|
PoolRealArray::Write w = l_heights.write();
|
|
|
|
PoolByteArray::Read r = im_data.read();
|
|
|
|
float *rp = (float *)r.ptr();
|
|
|
|
// 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 {
|
|
|
|
ERR_EXPLAIN("Expected PoolRealArray or float Image.");
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(l_width <= 0);
|
|
|
|
ERR_FAIL_COND(l_depth <= 0);
|
2018-03-27 19:45:27 +00: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")) {
|
|
|
|
|
|
|
|
PoolVector<real_t>::Read r = heights.read();
|
|
|
|
int heights_size = heights.size();
|
|
|
|
|
|
|
|
for (int i = 0; i < heights_size; ++i) {
|
|
|
|
real_t h = r[i];
|
|
|
|
|
|
|
|
if (h < l_min_height)
|
|
|
|
l_min_height = h;
|
|
|
|
else if (h > l_max_height)
|
|
|
|
l_max_height = h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(l_heights, l_width, l_depth, l_min_height, l_max_height);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant HeightMapShapeBullet::get_data() const {
|
|
|
|
ERR_FAIL_V(Variant());
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType HeightMapShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_HEIGHTMAP;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:45:27 +00:00
|
|
|
void HeightMapShapeBullet::setup(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
|
|
|
|
// TODO cell size must be tweaked using localScaling, which is a shared property for all Bullet shapes
|
|
|
|
|
2018-08-05 18:43:47 +00:00
|
|
|
// If this array is resized outside of here, it should be preserved due to CoW
|
|
|
|
heights = p_heights;
|
2018-03-27 19:45:27 +00:00
|
|
|
|
2017-11-04 19:52:59 +00:00
|
|
|
width = p_width;
|
|
|
|
depth = p_depth;
|
2018-03-27 19:45:27 +00:00
|
|
|
min_height = p_min_height;
|
|
|
|
max_height = p_max_height;
|
2017-11-04 19:52:59 +00:00
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *HeightMapShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
2018-03-27 19:45:27 +00:00
|
|
|
btCollisionShape *cs(ShapeBullet::create_shape_height_field(heights, width, depth, min_height, max_height));
|
2017-12-23 17:23:12 +00:00
|
|
|
cs->setLocalScaling(p_implicit_scale);
|
|
|
|
prepare(cs);
|
|
|
|
return cs;
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ray shape */
|
2017-12-06 20:36:34 +00:00
|
|
|
RayShapeBullet::RayShapeBullet() :
|
|
|
|
ShapeBullet(),
|
2018-02-19 19:59:57 +00:00
|
|
|
length(1),
|
|
|
|
slips_on_slope(false) {}
|
2017-11-04 19:52:59 +00:00
|
|
|
|
|
|
|
void RayShapeBullet::set_data(const Variant &p_data) {
|
2018-02-19 19:59:57 +00:00
|
|
|
|
|
|
|
Dictionary d = p_data;
|
|
|
|
setup(d["length"], d["slips_on_slope"]);
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant RayShapeBullet::get_data() const {
|
2018-02-19 19:59:57 +00:00
|
|
|
|
|
|
|
Dictionary d;
|
|
|
|
d["length"] = length;
|
|
|
|
d["slips_on_slope"] = slips_on_slope;
|
|
|
|
return d;
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::ShapeType RayShapeBullet::get_type() const {
|
|
|
|
return PhysicsServer::SHAPE_RAY;
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:59:57 +00:00
|
|
|
void RayShapeBullet::setup(real_t p_length, bool p_slips_on_slope) {
|
2017-11-04 19:52:59 +00:00
|
|
|
length = p_length;
|
2018-02-19 19:59:57 +00:00
|
|
|
slips_on_slope = p_slips_on_slope;
|
2017-11-04 19:52:59 +00:00
|
|
|
notifyShapeChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:50:14 +00:00
|
|
|
btCollisionShape *RayShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
|
|
|
|
return prepare(ShapeBullet::create_shape_ray(length * p_implicit_scale[1] + p_extra_edge, slips_on_slope));
|
2017-11-04 19:52:59 +00:00
|
|
|
}
|