godot/tests/test_physics_2d.cpp

404 lines
21 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* test_physics_2d.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +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. */
/*************************************************************************/
2014-02-10 01:10:30 +00:00
#include "test_physics_2d.h"
#include "core/os/main_loop.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/templates/map.h"
2014-02-10 01:10:30 +00:00
#include "scene/resources/texture.h"
#include "servers/display_server.h"
#include "servers/physics_server_2d.h"
#include "servers/rendering_server.h"
2014-02-10 01:10:30 +00:00
static const unsigned char convex_png[] = {
0x89, 0x50, 0x4e, 0x47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6c, 0x9c, 0x57, 0xbb, 0xfa, 0x25, 0x40, 0x14, 0x3, 0x81, 0x40, 0x34, 0x93, 0xc9, 0x2c, 0x57, 0x1c, 0x4, 0x0, 0x90, 0x58, 0x2c, 0xb6, 0x5e, 0xe9, 0xc1, 0x77, 0x1f, 0x10, 0x53, 0x53, 0x53, 0x52, 0xc5, 0x83, 0x14, 0x0, 0x70, 0x7e, 0xbf, 0x5f, 0xd0, 0x42, 0xf5, 0x95, 0x40, 0xf8, 0x7c, 0xbe, 0xcb, 0xa3, 0xa3, 0xa3, 0x3f, 0x1e, 0xbd, 0x1b, 0x0, 0x80, 0x1c, 0x1f, 0x1f, 0x87, 0xb4, 0x56, 0xfd, 0xaa, 0x5, 0x29, 0x51, 0x14, 0xbf, 0xf5, 0xf9, 0x7c, 0x97, 0x5a, 0xad, 0xbe, 0x12, 0x88, 0xf5, 0xf5, 0xf5, 0xd8, 0x83, 0x83, 0x54, 0xb5, 0x42, 0x8f, 0x66, 0x83, 0x94, 0xd6, 0xbd, 0x5f, 0xce, 0x7c, 0x38, 0x3c, 0x3c, 0xfc, 0xb3, 0x50, 0x28, 0xb8, 0xcb, 0x2, 0x1, 0x0, 0xdc, 0xf4, 0xf4, 0xf4, 0xfe, 0x73, 0x15, 0x2f, 0x17, 0xa4, 0x22, 0x91, 0x48, 0x50, 0xb5, 0x2d, 0x0, 0x80, 0x9b, 0x99, 0x99, 0x79, 0xfb, 0xdc, 0
2014-02-10 01:10:30 +00:00
};
class TestPhysics2DMainLoop : public MainLoop {
GDCLASS(TestPhysics2DMainLoop, MainLoop);
2014-02-10 01:10:30 +00:00
RID circle_img;
RID circle_shape;
RID space;
RID canvas;
RID ray;
RID ray_query;
Transform2D view_xform;
2014-02-10 01:10:30 +00:00
Vector2 ray_from, ray_to;
2014-02-10 01:10:30 +00:00
struct BodyShapeData {
RID image;
RID shape;
};
BodyShapeData body_shape_data[8];
2014-02-10 01:10:30 +00:00
void _create_body_shape_data() {
RenderingServer *vs = RenderingServer::get_singleton();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
2014-02-10 01:10:30 +00:00
// SEGMENT
{
Vector<uint8_t> pixels;
pixels.resize(32 * 2 * 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 32; j++) {
pixels.set(i * 32 * 2 + j * 2 + 0, (j == 0) ? 255 : 0);
pixels.set(i * 32 * 2 + j * 2 + 1, 255);
2014-02-10 01:10:30 +00:00
}
}
Ref<Image> image = memnew(Image(32, 2, 0, Image::FORMAT_LA8, pixels));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_SEGMENT].image = vs->texture_2d_create(image);
2014-02-10 01:10:30 +00:00
RID segment_shape = ps->segment_shape_create();
Rect2 sg(Point2(-16, 0), Point2(16, 0));
ps->shape_set_data(segment_shape, sg);
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_SEGMENT].shape = segment_shape;
2014-02-10 01:10:30 +00:00
}
// CIRCLE
{
Vector<uint8_t> pixels;
pixels.resize(32 * 32 * 2);
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
bool black = Vector2(i - 16, j - 16).length_squared() < 16 * 16;
2014-02-10 01:10:30 +00:00
pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16) ? 255 : 0);
pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0);
2014-02-10 01:10:30 +00:00
}
}
Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CIRCLE].image = vs->texture_2d_create(image);
2014-02-10 01:10:30 +00:00
RID circle_shape = ps->circle_shape_create();
ps->shape_set_data(circle_shape, 16);
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CIRCLE].shape = circle_shape;
2014-02-10 01:10:30 +00:00
}
// BOX
{
Vector<uint8_t> pixels;
pixels.resize(32 * 32 * 2);
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
bool black = i > 0 && i < 31 && j > 0 && j < 31;
2014-02-10 01:10:30 +00:00
pixels.set(i * 32 * 2 + j * 2 + 0, black ? 0 : 255);
pixels.set(i * 32 * 2 + j * 2 + 1, 255);
2014-02-10 01:10:30 +00:00
}
}
Ref<Image> image = memnew(Image(32, 32, 0, Image::FORMAT_LA8, pixels));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_RECTANGLE].image = vs->texture_2d_create(image);
2014-02-10 01:10:30 +00:00
RID rectangle_shape = ps->rectangle_shape_create();
ps->shape_set_data(rectangle_shape, Vector2(16, 16));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_RECTANGLE].shape = rectangle_shape;
2014-02-10 01:10:30 +00:00
}
// CAPSULE
{
Vector<uint8_t> pixels;
pixels.resize(32 * 64 * 2);
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 32; j++) {
int si = i > 48 ? i - 32 : (i < 16 ? i : 16);
bool black = Vector2(si - 16, j - 16).length_squared() < 16 * 16;
2014-02-10 01:10:30 +00:00
pixels.set(i * 32 * 2 + j * 2 + 0, (i == 16 || j == 16 || i == 48) ? 255 : 0);
pixels.set(i * 32 * 2 + j * 2 + 1, black ? 255 : 0);
2014-02-10 01:10:30 +00:00
}
}
Ref<Image> image = memnew(Image(32, 64, 0, Image::FORMAT_LA8, pixels));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CAPSULE].image = vs->texture_2d_create(image);
2014-02-10 01:10:30 +00:00
RID capsule_shape = ps->capsule_shape_create();
ps->shape_set_data(capsule_shape, Vector2(16, 32));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CAPSULE].shape = capsule_shape;
2014-02-10 01:10:30 +00:00
}
// CONVEX
{
Ref<Image> image = memnew(Image(convex_png));
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CONVEX_POLYGON].image = vs->texture_2d_create(image);
2014-02-10 01:10:30 +00:00
RID convex_polygon_shape = ps->convex_polygon_shape_create();
2014-02-10 01:10:30 +00:00
Vector<Vector2> arr;
Point2 sb(32, 32);
arr.push_back(Point2(20, 3) - sb);
arr.push_back(Point2(58, 23) - sb);
arr.push_back(Point2(55, 54) - sb);
arr.push_back(Point2(27, 60) - sb);
arr.push_back(Point2(5, 56) - sb);
arr.push_back(Point2(4, 20) - sb);
arr.push_back(Point2(11, 7) - sb);
ps->shape_set_data(convex_polygon_shape, arr);
2014-02-10 01:10:30 +00:00
body_shape_data[PhysicsServer2D::SHAPE_CONVEX_POLYGON].shape = convex_polygon_shape;
2014-02-10 01:10:30 +00:00
}
}
void _do_ray_query() {
/*
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
ps->query_intersection_segment(ray_query,ray_from,ray_to);
*/
2014-02-10 01:10:30 +00:00
}
protected:
void input_event(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
2014-02-10 01:10:30 +00:00
if (mb.is_valid()) {
if (mb->is_pressed()) {
Point2 p(mb->get_position().x, mb->get_position().y);
2014-02-10 01:10:30 +00:00
if (mb->get_button_index() == 1) {
ray_to = p;
2014-02-10 01:10:30 +00:00
_do_ray_query();
} else if (mb->get_button_index() == 2) {
ray_from = p;
2014-02-10 01:10:30 +00:00
_do_ray_query();
}
}
}
Ref<InputEventMouseMotion> mm = p_event;
2014-02-10 01:10:30 +00:00
if (mm.is_valid()) {
Point2 p = mm->get_position();
if (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT) {
ray_to = p;
2014-02-10 01:10:30 +00:00
_do_ray_query();
} else if (mm->get_button_mask() & MOUSE_BUTTON_MASK_RIGHT) {
ray_from = p;
2014-02-10 01:10:30 +00:00
_do_ray_query();
}
}
}
RID _add_body(PhysicsServer2D::ShapeType p_shape, const Transform2D &p_xform) {
RenderingServer *vs = RenderingServer::get_singleton();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
2014-02-10 01:10:30 +00:00
RID body = ps->body_create();
ps->body_add_shape(body, body_shape_data[p_shape].shape);
ps->body_set_space(body, space);
ps->body_set_continuous_collision_detection_mode(body, PhysicsServer2D::CCD_MODE_CAST_SHAPE);
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, p_xform);
2014-02-10 01:10:30 +00:00
//print_line("add body with xform: "+p_xform);
2014-02-10 01:10:30 +00:00
RID sprite = vs->canvas_item_create();
vs->canvas_item_set_parent(sprite, canvas);
vs->canvas_item_set_transform(sprite, p_xform);
Size2 imgsize(5, 5); //vs->texture_get_width(body_shape_data[p_shape].image), vs->texture_get_height(body_shape_data[p_shape].image));
vs->canvas_item_add_texture_rect(sprite, Rect2(-imgsize / 2.0, imgsize), body_shape_data[p_shape].image);
2014-02-10 01:10:30 +00:00
ps->body_set_force_integration_callback(body, callable_mp(this, &TestPhysics2DMainLoop::_body_moved), sprite);
2014-02-10 01:10:30 +00:00
return body;
}
void _add_world_boundary(const Vector2 &p_normal, real_t p_d) {
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
2014-02-10 01:10:30 +00:00
Array arr;
arr.push_back(p_normal);
arr.push_back(p_d);
RID world_boundary = ps->world_boundary_shape_create();
ps->shape_set_data(world_boundary, arr);
2014-02-10 01:10:30 +00:00
RID plane_body = ps->body_create();
ps->body_set_mode(plane_body, PhysicsServer2D::BODY_MODE_STATIC);
ps->body_set_space(plane_body, space);
ps->body_add_shape(plane_body, world_boundary);
2014-02-10 01:10:30 +00:00
}
void _add_concave(const Vector<Vector2> &p_points, const Transform2D &p_xform = Transform2D()) {
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
RenderingServer *vs = RenderingServer::get_singleton();
2014-02-10 01:10:30 +00:00
RID concave = ps->concave_polygon_shape_create();
ps->shape_set_data(concave, p_points);
RID body = ps->body_create();
ps->body_set_mode(body, PhysicsServer2D::BODY_MODE_STATIC);
ps->body_set_space(body, space);
ps->body_add_shape(body, concave);
ps->body_set_state(body, PhysicsServer2D::BODY_STATE_TRANSFORM, p_xform);
2014-02-10 01:10:30 +00:00
RID sprite = vs->canvas_item_create();
vs->canvas_item_set_parent(sprite, canvas);
vs->canvas_item_set_transform(sprite, p_xform);
for (int i = 0; i < p_points.size(); i += 2) {
vs->canvas_item_add_line(sprite, p_points[i], p_points[i + 1], Color(0, 0, 0), 2);
2014-02-10 01:10:30 +00:00
}
}
void _body_moved(Object *p_state, RID p_sprite) {
PhysicsDirectBodyState2D *state = (PhysicsDirectBodyState2D *)p_state;
RenderingServer::get_singleton()->canvas_item_set_transform(p_sprite, state->get_transform());
2014-02-10 01:10:30 +00:00
}
void _ray_query_callback(const RID &p_rid, ObjectID p_id, int p_shape, const Vector2 &p_point, const Vector2 &p_normal) {
2014-02-10 01:10:30 +00:00
Vector2 ray_end;
if (p_rid.is_valid()) {
ray_end = p_point;
2014-02-10 01:10:30 +00:00
} else {
ray_end = ray_to;
2014-02-10 01:10:30 +00:00
}
RenderingServer *vs = RenderingServer::get_singleton();
2014-02-10 01:10:30 +00:00
vs->canvas_item_clear(ray);
vs->canvas_item_add_line(ray, ray_from, ray_end, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2);
if (p_rid.is_valid()) {
vs->canvas_item_add_line(ray, ray_end, ray_end + p_normal * 20, p_rid.is_valid() ? Color(0, 1, 0.4) : Color(1, 0.4, 0), 2);
}
2014-02-10 01:10:30 +00:00
}
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("_ray_query_callback"), &TestPhysics2DMainLoop::_ray_query_callback);
2014-02-10 01:10:30 +00:00
}
public:
virtual void initialize() override {
RenderingServer *vs = RenderingServer::get_singleton();
PhysicsServer2D *ps = PhysicsServer2D::get_singleton();
2014-02-10 01:10:30 +00:00
space = ps->space_create();
ps->space_set_active(space, true);
2014-02-10 01:10:30 +00:00
ps->set_active(true);
ps->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, Vector2(0, 1));
ps->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY, 980);
2014-02-10 01:10:30 +00:00
{
RID vp = vs->viewport_create();
canvas = vs->canvas_create();
Size2i screen_size = DisplayServer::get_singleton()->window_get_size();
vs->viewport_attach_canvas(vp, canvas);
vs->viewport_set_size(vp, screen_size.x, screen_size.y);
vs->viewport_attach_to_screen(vp, Rect2(Vector2(), screen_size));
vs->viewport_set_active(vp, true);
Transform2D smaller;
2014-02-10 01:10:30 +00:00
//smaller.scale(Vector2(0.6,0.6));
//smaller.elements[2]=Vector2(100,0);
//view_xform = smaller;
vs->viewport_set_canvas_transform(vp, canvas, view_xform);
2014-02-10 01:10:30 +00:00
}
ray = vs->canvas_item_create();
vs->canvas_item_set_parent(ray, canvas);
2014-02-10 01:10:30 +00:00
//ray_query = ps->query_create(this,"_ray_query_callback",Variant());
//ps->query_intersection(ray_query,space);
_create_body_shape_data();
for (int i = 0; i < 32; i++) {
PhysicsServer2D::ShapeType types[4] = {
PhysicsServer2D::SHAPE_CIRCLE,
PhysicsServer2D::SHAPE_CAPSULE,
PhysicsServer2D::SHAPE_RECTANGLE,
PhysicsServer2D::SHAPE_CONVEX_POLYGON,
2014-02-10 01:10:30 +00:00
};
PhysicsServer2D::ShapeType type = types[i % 4];
//type=PhysicsServer2D::SHAPE_SEGMENT;
_add_body(type, Transform2D(i * 0.8, Point2(152 + i * 40, 100 - 40 * i)));
/*
if (i==0)
ps->body_set_mode(b,PhysicsServer2D::BODY_MODE_STATIC);
*/
2014-02-10 01:10:30 +00:00
}
//RID b= _add_body(PhysicsServer2D::SHAPE_CIRCLE,Transform2D(0,Point2(101,140)));
//ps->body_set_mode(b,PhysicsServer2D::BODY_MODE_STATIC);
2014-02-10 01:10:30 +00:00
Point2 prev;
Vector<Point2> parr;
for (int i = 0; i < 30; i++) {
Point2 p(i * 60, Math::randf() * 70 + 340);
if (i > 0) {
2014-02-10 01:10:30 +00:00
parr.push_back(prev);
parr.push_back(p);
}
prev = p;
2014-02-10 01:10:30 +00:00
}
_add_concave(parr);
//_add_world_boundary(Vector2(0.0,-1).normalized(),-300);
//_add_world_boundary(Vector2(1,0).normalized(),50);
//_add_world_boundary(Vector2(-1,0).normalized(),-600);
2014-02-10 01:10:30 +00:00
}
virtual bool process(double p_time) override {
2014-02-10 01:10:30 +00:00
return false;
}
virtual void finalize() override {
2014-02-10 01:10:30 +00:00
}
TestPhysics2DMainLoop() {}
};
namespace TestPhysics2D {
MainLoop *test() {
return memnew(TestPhysics2DMainLoop);
2014-02-10 01:10:30 +00:00
}
} // namespace TestPhysics2D