Implemented debug collision shape visualization for the terrain. Also fixed the winding order for the isometric mesher's returned shape.

This commit is contained in:
Relintai 2022-03-04 14:17:55 +01:00
parent 72100b029c
commit 60a7e84a5d
6 changed files with 76 additions and 4 deletions

View File

@ -22,8 +22,8 @@ SOFTWARE.
#include "terrain_2d_mesher_default.h"
#include "../../world/default/terrain_2d_chunk_default.h"
#include "../../library/terrain_2d_material_cache.h"
#include "../../world/default/terrain_2d_chunk_default.h"
#include "../../defines.h"
@ -73,6 +73,11 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref<Terrain2DChunk> ch) {
Physics2DServer::get_singleton()->body_set_state(body_rid, Physics2DServer::BODY_STATE_TRANSFORM, chunk->get_transform());
}
#if TOOLS_ENABLED
chunk->_debug_terrain_collider_transforms.clear();
#endif
Physics2DServer::get_singleton()->body_clear_shapes(body_rid);
Physics2DServer::get_singleton()->body_set_mode(body_rid, Physics2DServer::BODY_MODE_STATIC);
Physics2DServer::get_singleton()->body_attach_object_instance_id(body_rid, get_instance_id());
//TODO
@ -92,7 +97,18 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref<Terrain2DChunk> ch) {
mcache = _library->material_cache_get(chunk->material_cache_key_get());
}
int texture_scale = get_texture_scale();
#if TOOLS_ENABLED
SceneTree *st = SceneTree::get_singleton();
bool debug_shapes = false;
if (st) {
if (Engine::get_singleton()->is_editor_hint()) {
//debug_shapes = show_collision;
} else {
debug_shapes = st->is_debugging_collisions_hint();
}
}
#endif
int margin_start = chunk->get_margin_start();
//z_size + margin_start is fine, x, and z are in data space.
@ -108,6 +124,12 @@ void Terrain2DMesherDefault::create_tile_colliders(Ref<Terrain2DChunk> ch) {
if ((flags & Terrain2DChunkDefault::FLAG_CHANNEL_COLLIDER) != 0) {
Transform2D coll_transform(0, mesh_transform_terrain.xform(Vector2((x)*cell_size_x, y * cell_size_y)));
#if TOOLS_ENABLED
if (debug_shapes) {
chunk->_debug_terrain_collider_transforms.push_back(coll_transform);
}
#endif
Physics2DServer::get_singleton()->body_add_shape(body_rid, tile_collider_shape->get_rid(), coll_transform);
}
}

View File

@ -635,10 +635,10 @@ Ref<Shape2D> Terrain2DMesherIsometric::create_terrain_tile_collider_shape(Ref<Te
Transform2D mesh_transform_terrain = chunk->mesh_transform_terrain_get();
points.push_back(mesh_transform_terrain.xform(Vector2(0, 0)));
points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, 0)));
points.push_back(mesh_transform_terrain.xform(Vector2(0, _cell_size_y)));
points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, _cell_size_y)));
points.push_back(mesh_transform_terrain.xform(Vector2(_cell_size_x, 0)));
sh->set_points(points);
return sh;

View File

@ -727,6 +727,43 @@ void Terrain2DChunkDefault::_draw() {
//Note: the transform parameter is not implemented in gles2
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(0), prop_mesh_rid, Transform2D(), Color(1, 1, 1, 1), prop_texture_rid, RID());
}
#if TOOLS_ENABLED
SceneTree *st = SceneTree::get_singleton();
bool debug_shapes = false;
if (st) {
if (Engine::get_singleton()->is_editor_hint()) {
//debug_shapes = show_collision;
} else {
debug_shapes = st->is_debugging_collisions_hint();
}
}
if (debug_shapes) {
if (_debug_canvas_item == RID()) {
_debug_canvas_item = VisualServer::get_singleton()->canvas_item_create();
if (_voxel_world) {
VisualServer::get_singleton()->canvas_item_set_parent(_debug_canvas_item, get_voxel_world()->get_canvas_item());
}
VisualServer::get_singleton()->canvas_item_set_transform(_debug_canvas_item, get_transform());
VisualServer::get_singleton()->canvas_item_set_z_index(_debug_canvas_item, 1);
}
VisualServer::get_singleton()->canvas_item_clear(_debug_canvas_item);
Color debug_collision_color = st->get_debug_collisions_color();
Ref<Shape2D> shape = get_default_tile_shape();
for (int i = 0; i < _debug_terrain_collider_transforms.size(); ++i) {
VisualServer::get_singleton()->canvas_item_add_set_transform(_debug_canvas_item, _debug_terrain_collider_transforms[i]);
shape->draw(_debug_canvas_item, debug_collision_color);
}
}
#endif
}
//Lights

View File

@ -202,6 +202,9 @@ public:
//Todo make this a generic thing
Vector<Transform2D> _mesh_transforms;
RID _2d_body_rid;
#if TOOLS_ENABLED
Vector<Transform2D> _debug_terrain_collider_transforms;
#endif
protected:
virtual void _channel_setup();

View File

@ -1325,6 +1325,12 @@ void Terrain2DChunk::_world_transform_changed() {
if (get_canvas_item_count() > 0) {
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(0), t);
}
#if TOOLS_ENABLED
if (_debug_canvas_item.is_valid()) {
VisualServer::get_singleton()->canvas_item_set_transform(_debug_canvas_item, t);
}
#endif
}
/*

View File

@ -364,6 +364,10 @@ public:
Terrain2DChunk();
~Terrain2DChunk();
#if TOOLS_ENABLED
RID _debug_canvas_item;
#endif
protected:
virtual void _enter_tree();
virtual void _exit_tree();