2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-03-28 13:19:05 +01:00
|
|
|
/* test_physics_3d.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
#include "test_physics_3d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
#include "core/math/quick_hull.h"
|
|
|
|
#include "core/os/main_loop.h"
|
|
|
|
#include "core/os/os.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
|
|
|
#include "core/templates/map.h"
|
2020-03-03 14:36:29 +01:00
|
|
|
#include "servers/display_server.h"
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "servers/physics_server_3d.h"
|
|
|
|
#include "servers/rendering_server.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
class TestPhysics3DMainLoop : public MainLoop {
|
|
|
|
GDCLASS(TestPhysics3DMainLoop, MainLoop);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
LINK_COUNT = 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
RID test_cube;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RID plane;
|
|
|
|
RID sphere;
|
|
|
|
RID light;
|
|
|
|
RID camera;
|
|
|
|
RID mover;
|
|
|
|
RID scenario;
|
|
|
|
RID space;
|
|
|
|
|
|
|
|
RID character;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
float ofs_x, ofs_y;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Point2 joy_direction;
|
|
|
|
|
|
|
|
List<RID> bodies;
|
2020-03-27 19:21:27 +01:00
|
|
|
Map<PhysicsServer3D::ShapeType, RID> type_shape_map;
|
|
|
|
Map<PhysicsServer3D::ShapeType, RID> type_mesh_map;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void body_changed_transform(Object *p_state, RID p_visual_instance) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectBodyState3D *state = (PhysicsDirectBodyState3D *)p_state;
|
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
2017-03-05 16:44:50 +01:00
|
|
|
Transform t = state->get_transform();
|
|
|
|
vs->instance_set_transform(p_visual_instance, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
bool quit;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
protected:
|
|
|
|
static void _bind_methods() {
|
2020-03-28 13:19:05 +01:00
|
|
|
ClassDB::bind_method("body_changed_transform", &TestPhysics3DMainLoop::body_changed_transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID create_body(PhysicsServer3D::ShapeType p_shape, PhysicsServer3D::BodyMode p_body, const Transform p_location, bool p_active_default = true, const Transform &p_shape_xform = Transform()) {
|
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RID mesh_instance = vs->instance_create2(type_mesh_map[p_shape], scenario);
|
|
|
|
RID body = ps->body_create(p_body, !p_active_default);
|
|
|
|
ps->body_set_space(body, space);
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_param(body, PhysicsServer3D::BODY_PARAM_BOUNCE, 0.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
//todo set space
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_add_shape(body, type_shape_map[p_shape]);
|
|
|
|
ps->body_set_force_integration_callback(body, this, "body_changed_transform", mesh_instance);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM, p_location);
|
2014-02-10 02:10:30 +01:00
|
|
|
bodies.push_back(body);
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (p_body == PhysicsServer3D::BODY_MODE_STATIC) {
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->instance_set_transform(mesh_instance, p_location);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RID create_static_plane(const Plane &p_plane) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID world_margin_shape = ps->shape_create(PhysicsServer3D::SHAPE_PLANE);
|
2020-02-21 12:19:24 +01:00
|
|
|
ps->shape_set_data(world_margin_shape, p_plane);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID b = ps->body_create(PhysicsServer3D::BODY_MODE_STATIC);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_set_space(b, space);
|
2014-02-10 02:10:30 +01:00
|
|
|
//todo set space
|
2020-02-21 12:19:24 +01:00
|
|
|
ps->body_add_shape(b, world_margin_shape);
|
2014-02-10 02:10:30 +01:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void configure_body(RID p_body, float p_mass, float p_friction, float p_bounce) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
|
|
|
ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_MASS, p_mass);
|
|
|
|
ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_FRICTION, p_friction);
|
|
|
|
ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_BOUNCE, p_bounce);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:50:29 +01:00
|
|
|
void initialize_shapes() {
|
2020-03-27 19:21:27 +01:00
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* SPHERE SHAPE */
|
2017-03-05 16:44:50 +01:00
|
|
|
RID sphere_mesh = vs->make_sphere_mesh(10, 20, 0.5);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_mesh_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID sphere_shape = ps->shape_create(PhysicsServer3D::SHAPE_SPHERE);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->shape_set_data(sphere_shape, 0.5);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_shape_map[PhysicsServer3D::SHAPE_SPHERE] = sphere_shape;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* BOX SHAPE */
|
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(0.5, 0.5, 0.5));
|
2014-02-10 02:10:30 +01:00
|
|
|
RID box_mesh = vs->mesh_create();
|
2020-05-25 19:20:45 +02:00
|
|
|
Geometry3D::MeshData box_data = Geometry3D::build_convex_mesh(box_planes);
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->mesh_add_surface_from_mesh_data(box_mesh, box_data);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_mesh_map[PhysicsServer3D::SHAPE_BOX] = box_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID box_shape = ps->shape_create(PhysicsServer3D::SHAPE_BOX);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->shape_set_data(box_shape, Vector3(0.5, 0.5, 0.5));
|
2020-03-27 19:21:27 +01:00
|
|
|
type_shape_map[PhysicsServer3D::SHAPE_BOX] = box_shape;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* CAPSULE SHAPE */
|
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
Vector<Plane> capsule_planes = Geometry3D::build_capsule_planes(0.5, 0.7, 12, Vector3::AXIS_Z);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
RID capsule_mesh = vs->mesh_create();
|
2020-05-25 19:20:45 +02:00
|
|
|
Geometry3D::MeshData capsule_data = Geometry3D::build_convex_mesh(capsule_planes);
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE);
|
2014-02-10 02:10:30 +01:00
|
|
|
Dictionary capsule_params;
|
2017-03-05 16:44:50 +01:00
|
|
|
capsule_params["radius"] = 0.5;
|
|
|
|
capsule_params["height"] = 1.4;
|
|
|
|
ps->shape_set_data(capsule_shape, capsule_params);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_shape_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_shape;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* CONVEX SHAPE */
|
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
Vector<Plane> convex_planes = Geometry3D::build_cylinder_planes(0.5, 0.7, 5, Vector3::AXIS_Z);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
RID convex_mesh = vs->mesh_create();
|
2020-05-25 19:20:45 +02:00
|
|
|
Geometry3D::MeshData convex_data = Geometry3D::build_convex_mesh(convex_planes);
|
2017-03-05 16:44:50 +01:00
|
|
|
QuickHull::build(convex_data.vertices, convex_data);
|
|
|
|
vs->mesh_add_surface_from_mesh_data(convex_mesh, convex_data);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
type_mesh_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID convex_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->shape_set_data(convex_shape, convex_data.vertices);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_shape_map[PhysicsServer3D::SHAPE_CONVEX_POLYGON] = convex_shape;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void make_trimesh(Vector<Vector3> p_faces, const Transform &p_xform = Transform()) {
|
2020-03-27 19:21:27 +01:00
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
|
|
|
RID trimesh_shape = ps->shape_create(PhysicsServer3D::SHAPE_CONCAVE_POLYGON);
|
2014-02-10 02:10:30 +01:00
|
|
|
ps->shape_set_data(trimesh_shape, p_faces);
|
2017-03-05 16:44:50 +01:00
|
|
|
p_faces = ps->shape_get_data(trimesh_shape); // optimized one
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<Vector3> normals; // for drawing
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_faces.size() / 3; i++) {
|
|
|
|
Plane p(p_faces[i * 3 + 0], p_faces[i * 3 + 1], p_faces[i * 3 + 2]);
|
2014-02-10 02:10:30 +01:00
|
|
|
normals.push_back(p.normal);
|
|
|
|
normals.push_back(p.normal);
|
|
|
|
normals.push_back(p.normal);
|
|
|
|
}
|
|
|
|
|
|
|
|
RID trimesh_mesh = vs->mesh_create();
|
|
|
|
Array d;
|
2020-03-27 19:21:27 +01:00
|
|
|
d.resize(RS::ARRAY_MAX);
|
|
|
|
d[RS::ARRAY_VERTEX] = p_faces;
|
|
|
|
d[RS::ARRAY_NORMAL] = normals;
|
|
|
|
vs->mesh_add_surface_from_arrays(trimesh_mesh, RS::PRIMITIVE_TRIANGLES, d);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RID triins = vs->instance_create2(trimesh_mesh, scenario);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID tribody = ps->body_create(PhysicsServer3D::BODY_MODE_STATIC);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_set_space(tribody, space);
|
2014-02-10 02:10:30 +01:00
|
|
|
//todo set space
|
|
|
|
ps->body_add_shape(tribody, trimesh_shape);
|
|
|
|
Transform tritrans = p_xform;
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_state(tribody, PhysicsServer3D::BODY_STATE_TRANSFORM, tritrans);
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->instance_set_transform(triins, tritrans);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void make_grid(int p_width, int p_height, float p_cellsize, float p_cellheight, const Transform &p_xform = Transform()) {
|
2020-03-17 07:33:00 +01:00
|
|
|
Vector<Vector<float>> grid;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
grid.resize(p_width);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_width; i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
grid.write[i].resize(p_height);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < p_height; j++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
grid.write[i].write[j] = 1.0 + Math::random(-p_cellheight, p_cellheight);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Vector3> faces;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 1; i < p_width; i++) {
|
|
|
|
for (int j = 1; j < p_height; j++) {
|
|
|
|
#define MAKE_VERTEX(m_x, m_z) \
|
|
|
|
faces.push_back(Vector3((m_x - p_width / 2) * p_cellsize, grid[m_x][m_z], (m_z - p_height / 2) * p_cellsize))
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MAKE_VERTEX(i, j - 1);
|
|
|
|
MAKE_VERTEX(i, j);
|
|
|
|
MAKE_VERTEX(i - 1, j);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MAKE_VERTEX(i - 1, j - 1);
|
|
|
|
MAKE_VERTEX(i, j - 1);
|
|
|
|
MAKE_VERTEX(i - 1, j);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
make_trimesh(faces, p_xform);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-05-20 17:38:03 +02:00
|
|
|
virtual void input_event(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
if (mm.is_valid() && mm->get_button_mask() & 4) {
|
|
|
|
ofs_y -= mm->get_relative().y / 200.0;
|
|
|
|
ofs_x += mm->get_relative().x / 200.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mm.is_valid() && mm->get_button_mask() & 1) {
|
|
|
|
float y = -mm->get_relative().y / 20.0;
|
|
|
|
float x = mm->get_relative().x / 20.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (mover.is_valid()) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
|
|
|
Transform t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM);
|
2017-03-05 16:44:50 +01:00
|
|
|
t.origin += Vector3(x, y, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void request_quit() {
|
2017-03-05 16:44:50 +01:00
|
|
|
quit = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-12-22 10:50:29 +01:00
|
|
|
virtual void initialize() override {
|
2017-03-05 16:44:50 +01:00
|
|
|
ofs_x = ofs_y = 0;
|
2020-12-22 10:50:29 +01:00
|
|
|
initialize_shapes();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
2017-03-05 16:44:50 +01:00
|
|
|
space = ps->space_create();
|
|
|
|
ps->space_set_active(space, true);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* LIGHT */
|
2017-11-10 03:34:01 +01:00
|
|
|
RID lightaux = vs->directional_light_create();
|
2014-02-10 02:10:30 +01:00
|
|
|
scenario = vs->scenario_create();
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->light_set_shadow(lightaux, true);
|
|
|
|
light = vs->instance_create2(lightaux, scenario);
|
2014-02-10 02:10:30 +01:00
|
|
|
Transform t;
|
2017-03-05 16:44:50 +01:00
|
|
|
t.rotate(Vector3(1.0, 0, 0), 0.6);
|
|
|
|
vs->instance_set_transform(light, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* CAMERA */
|
|
|
|
|
|
|
|
camera = vs->camera_create();
|
2017-01-15 13:49:58 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RID viewport = vs->viewport_create();
|
2020-03-03 14:36:29 +01:00
|
|
|
Size2i screen_size = DisplayServer::get_singleton()->window_get_size();
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->viewport_set_size(viewport, screen_size.x, screen_size.y);
|
|
|
|
vs->viewport_attach_to_screen(viewport, Rect2(Vector2(), screen_size));
|
|
|
|
vs->viewport_set_active(viewport, true);
|
|
|
|
vs->viewport_attach_camera(viewport, camera);
|
|
|
|
vs->viewport_set_scenario(viewport, scenario);
|
|
|
|
|
|
|
|
vs->camera_set_perspective(camera, 60, 0.1, 40.0);
|
|
|
|
vs->camera_set_transform(camera, Transform(Basis(), Vector3(0, 9, 12)));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Transform gxf;
|
2017-03-05 16:44:50 +01:00
|
|
|
gxf.basis.scale(Vector3(1.4, 0.4, 1.4));
|
|
|
|
gxf.origin = Vector3(-2, 1, -2);
|
|
|
|
make_grid(5, 5, 2.5, 1, gxf);
|
2014-02-10 02:10:30 +01:00
|
|
|
test_fall();
|
2017-03-05 16:44:50 +01:00
|
|
|
quit = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-12-22 10:50:29 +01:00
|
|
|
virtual bool physics_process(float p_time) override {
|
2016-10-03 21:33:42 +02:00
|
|
|
if (mover.is_valid()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
static float joy_speed = 10;
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
|
|
|
Transform t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM);
|
2017-03-05 16:44:50 +01:00
|
|
|
t.origin += Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time, 0);
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Transform cameratr;
|
2017-03-05 16:44:50 +01:00
|
|
|
cameratr.rotate(Vector3(0, 1, 0), ofs_x);
|
|
|
|
cameratr.rotate(Vector3(1, 0, 0), -ofs_y);
|
|
|
|
cameratr.translate(Vector3(0, 2, 8));
|
2020-03-27 19:21:27 +01:00
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->camera_set_transform(camera, cameratr);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return quit;
|
|
|
|
}
|
2020-12-22 10:50:29 +01:00
|
|
|
virtual void finalize() override {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_joint() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_hinge() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_character() {
|
2020-03-27 19:21:27 +01:00
|
|
|
RenderingServer *vs = RenderingServer::get_singleton();
|
|
|
|
PhysicsServer3D *ps = PhysicsServer3D::get_singleton();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-25 19:20:45 +02:00
|
|
|
Vector<Plane> capsule_planes = Geometry3D::build_capsule_planes(0.5, 1, 12, 5, Vector3::AXIS_Y);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
RID capsule_mesh = vs->mesh_create();
|
2020-05-25 19:20:45 +02:00
|
|
|
Geometry3D::MeshData capsule_data = Geometry3D::build_convex_mesh(capsule_planes);
|
2017-03-05 16:44:50 +01:00
|
|
|
vs->mesh_add_surface_from_mesh_data(capsule_mesh, capsule_data);
|
2020-03-27 19:21:27 +01:00
|
|
|
type_mesh_map[PhysicsServer3D::SHAPE_CAPSULE] = capsule_mesh;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RID capsule_shape = ps->shape_create(PhysicsServer3D::SHAPE_CAPSULE);
|
2014-02-10 02:10:30 +01:00
|
|
|
Dictionary capsule_params;
|
2017-03-05 16:44:50 +01:00
|
|
|
capsule_params["radius"] = 0.5;
|
|
|
|
capsule_params["height"] = 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
Transform shape_xform;
|
2017-03-05 16:44:50 +01:00
|
|
|
shape_xform.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
//shape_xform.origin=Vector3(1,1,1);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->shape_set_data(capsule_shape, capsule_params);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
RID mesh_instance = vs->instance_create2(capsule_mesh, scenario);
|
2020-03-27 19:21:27 +01:00
|
|
|
character = ps->body_create(PhysicsServer3D::BODY_MODE_CHARACTER);
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_set_space(character, space);
|
2014-02-10 02:10:30 +01:00
|
|
|
//todo add space
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_add_shape(character, capsule_shape);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ps->body_set_force_integration_callback(character, this, "body_changed_transform", mesh_instance);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
ps->body_set_state(character, PhysicsServer3D::BODY_STATE_TRANSFORM, Transform(Basis(), Vector3(-2, 5, -2)));
|
2014-02-10 02:10:30 +01:00
|
|
|
bodies.push_back(character);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_fall() {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 35; i++) {
|
2020-03-27 19:21:27 +01:00
|
|
|
static const PhysicsServer3D::ShapeType shape_idx[] = {
|
|
|
|
PhysicsServer3D::SHAPE_CAPSULE,
|
|
|
|
PhysicsServer3D::SHAPE_BOX,
|
|
|
|
PhysicsServer3D::SHAPE_SPHERE,
|
|
|
|
PhysicsServer3D::SHAPE_CONVEX_POLYGON
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::ShapeType type = shape_idx[i % 4];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
Transform t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
t.origin = Vector3(0.0 * i, 3.5 + 1.1 * i, 0.7 + 0.0 * i);
|
|
|
|
t.basis.rotate(Vector3(0.2, -1, 0), Math_PI / 2 * 0.6);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
create_body(type, PhysicsServer3D::BODY_MODE_RIGID, t);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
create_static_plane(Plane(Vector3(0, 1, 0), -1));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_activate() {
|
2020-03-27 19:21:27 +01:00
|
|
|
create_body(PhysicsServer3D::SHAPE_BOX, PhysicsServer3D::BODY_MODE_RIGID, Transform(Basis(), Vector3(0, 2, 0)), true);
|
2017-03-05 16:44:50 +01:00
|
|
|
create_static_plane(Plane(Vector3(0, 1, 0), -1));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 10:50:29 +01:00
|
|
|
virtual bool process(float p_time) override {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
TestPhysics3DMainLoop() {
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-28 13:19:05 +01:00
|
|
|
namespace TestPhysics3D {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MainLoop *test() {
|
2020-03-28 13:19:05 +01:00
|
|
|
return memnew(TestPhysics3DMainLoop);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-03-28 13:19:05 +01:00
|
|
|
} // namespace TestPhysics3D
|