Skip to content

Commit 1d46baa

Browse files
committed
Fixed physics body scale and added mesh and convex hull previews
1 parent 75faf50 commit 1d46baa

8 files changed

Lines changed: 670 additions & 109 deletions

File tree

editor/Catalog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ namespace {
131131
static const FastPropertyDescriptor kTransformProperties[] = {
132132
makeFastProperty<Transform, Vector3, &Transform::position>("position", PropertyType::Vector3, UpdateFlags_Transform | UpdateFlags_Layout_Anchors),
133133
makeFastProperty<Transform, Quaternion, &Transform::rotation>("rotation", PropertyType::Quat, UpdateFlags_Transform),
134-
makeFastProperty<Transform, Vector3, &Transform::scale>("scale", PropertyType::Vector3, UpdateFlags_Transform),
134+
makeFastProperty<Transform, Vector3, &Transform::scale>("scale", PropertyType::Vector3, UpdateFlags_Transform | UpdateFlags_Body2D | UpdateFlags_Body3D),
135135
makeFastProperty<Transform, bool, &Transform::visible>("visible", PropertyType::Bool, UpdateFlags_None),
136136
makeFastProperty<Transform, bool, &Transform::billboard>("billboard", PropertyType::Bool, UpdateFlags_Transform),
137137
makeFastProperty<Transform, bool, &Transform::fakeBillboard>("fakeBillboard", PropertyType::Bool, UpdateFlags_Transform),

editor/render/SceneRender2D.cpp

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Project.h"
44
#include "LineDrawUtils.h"
55

6+
#include <algorithm>
67
#include <cmath>
78

89

@@ -113,10 +114,21 @@ void editor::SceneRender2D::createOrUpdateBodyLines(Entity entity, const Transfo
113114

114115
float alpha = highlighted ? 1.0f : 0.35f;
115116
const Vector4 bodyColor(0.2f, 0.95f, 0.95f, alpha);
116-
const Matrix4 modelMatrix = transform.modelMatrix;
117+
const Vector2 entityScale(transform.scale.x, transform.scale.y);
118+
const Vector2 absScale(std::fabs(entityScale.x), std::fabs(entityScale.y));
119+
const float radiusScale = std::max(absScale.x, absScale.y);
120+
const Matrix4 bodyMatrix = Matrix4::translateMatrix(transform.worldPosition) * transform.worldRotation.getRotationMatrix();
121+
122+
auto scaledPoint = [&](const Vector2& point){
123+
return point * entityScale;
124+
};
125+
126+
auto scaledRadius = [&](float radius){
127+
return radius * radiusScale;
128+
};
117129

118130
auto toWorld = [&](const Vector2& point){
119-
return modelMatrix * Vector3(point.x, point.y, 0.0f);
131+
return bodyMatrix * Vector3(point.x, point.y, 0.0f);
120132
};
121133

122134
auto addCircle = [&](const Vector2& center, float radius, int segments){
@@ -197,20 +209,35 @@ void editor::SceneRender2D::createOrUpdateBodyLines(Entity entity, const Transfo
197209
for (size_t i = 0; i < body.numShapes; i++){
198210
const Shape2D& shape = body.shapes[i];
199211

212+
auto addPolygonEdges = [&](){
213+
for (size_t j = 0; j < shape.numVertices; j++){
214+
Vector2 p0 = scaledPoint(shape.vertices[j]);
215+
Vector2 p1 = scaledPoint(shape.vertices[(j + 1) % shape.numVertices]);
216+
bodyLinesObj->addLine(toWorld(p0), toWorld(p1), bodyColor);
217+
}
218+
};
219+
200220
if (shape.type == Shape2DType::POLYGON){
201221
if (shape.numVertices >= 3){
202-
if (shape.radius > 0.0f){
203-
addRoundedRect(shape.pointA, shape.pointB, shape.radius);
204-
}else{
205-
for (size_t j = 0; j < shape.numVertices; j++){
206-
const Vector2& p0 = shape.vertices[j];
207-
const Vector2& p1 = shape.vertices[(j + 1) % shape.numVertices];
208-
bodyLinesObj->addLine(toWorld(p0), toWorld(p1), bodyColor);
222+
if (shape.radius > 0.0f && shape.numVertices == 4){
223+
Vector2 minPt = scaledPoint(shape.vertices[0]);
224+
Vector2 maxPt = minPt;
225+
for (size_t j = 1; j < shape.numVertices; j++){
226+
Vector2 p = scaledPoint(shape.vertices[j]);
227+
minPt.x = std::min(minPt.x, p.x);
228+
minPt.y = std::min(minPt.y, p.y);
229+
maxPt.x = std::max(maxPt.x, p.x);
230+
maxPt.y = std::max(maxPt.y, p.y);
209231
}
232+
addRoundedRect(minPt, maxPt, scaledRadius(shape.radius));
233+
}else{
234+
addPolygonEdges();
210235
}
211236
}else{
212-
Vector2 minPt(std::min(shape.pointA.x, shape.pointB.x), std::min(shape.pointA.y, shape.pointB.y));
213-
Vector2 maxPt(std::max(shape.pointA.x, shape.pointB.x), std::max(shape.pointA.y, shape.pointB.y));
237+
Vector2 pointA = scaledPoint(shape.pointA);
238+
Vector2 pointB = scaledPoint(shape.pointB);
239+
Vector2 minPt(std::min(pointA.x, pointB.x), std::min(pointA.y, pointB.y));
240+
Vector2 maxPt(std::max(pointA.x, pointB.x), std::max(pointA.y, pointB.y));
214241

215242
Vector2 p0(minPt.x, minPt.y);
216243
Vector2 p1(maxPt.x, minPt.y);
@@ -223,11 +250,11 @@ void editor::SceneRender2D::createOrUpdateBodyLines(Entity entity, const Transfo
223250
bodyLinesObj->addLine(toWorld(p3), toWorld(p0), bodyColor);
224251
}
225252
}else if (shape.type == Shape2DType::CIRCLE){
226-
addCircle(shape.pointA, shape.radius, 24);
253+
addCircle(scaledPoint(shape.pointA), scaledRadius(shape.radius), 24);
227254
}else if (shape.type == Shape2DType::CAPSULE){
228-
Vector2 pointA = shape.pointA;
229-
Vector2 pointB = shape.pointB;
230-
float radius = shape.radius;
255+
Vector2 pointA = scaledPoint(shape.pointA);
256+
Vector2 pointB = scaledPoint(shape.pointB);
257+
float radius = scaledRadius(shape.radius);
231258

232259
if (radius <= 0.0f){
233260
continue;
@@ -254,15 +281,15 @@ void editor::SceneRender2D::createOrUpdateBodyLines(Entity entity, const Transfo
254281
addArc(pointA, radius, baseAngle + 0.5f * M_PI, baseAngle + 1.5f * M_PI, 12);
255282
addArc(pointB, radius, baseAngle - 0.5f * M_PI, baseAngle + 0.5f * M_PI, 12);
256283
}else if (shape.type == Shape2DType::SEGMENT){
257-
bodyLinesObj->addLine(toWorld(shape.pointA), toWorld(shape.pointB), bodyColor);
284+
bodyLinesObj->addLine(toWorld(scaledPoint(shape.pointA)), toWorld(scaledPoint(shape.pointB)), bodyColor);
258285
}else if (shape.type == Shape2DType::CHAIN){
259286
if (shape.numVertices >= 2){
260287
for (size_t j = 0; j < shape.numVertices - 1; j++){
261-
bodyLinesObj->addLine(toWorld(shape.vertices[j]), toWorld(shape.vertices[j + 1]), bodyColor);
288+
bodyLinesObj->addLine(toWorld(scaledPoint(shape.vertices[j])), toWorld(scaledPoint(shape.vertices[j + 1])), bodyColor);
262289
}
263290

264291
if (shape.loop){
265-
bodyLinesObj->addLine(toWorld(shape.vertices[shape.numVertices - 1]), toWorld(shape.vertices[0]), bodyColor);
292+
bodyLinesObj->addLine(toWorld(scaledPoint(shape.vertices[shape.numVertices - 1])), toWorld(scaledPoint(shape.vertices[0])), bodyColor);
266293
}
267294
}
268295
}
@@ -279,15 +306,19 @@ void editor::SceneRender2D::createOrUpdateJointLines(Entity entity, const Joint2
279306
return;
280307
}
281308

282-
auto getBodyWorldPoint = [&](Entity bodyEntity, const Vector2& localPoint){
309+
auto getBodyWorldPosition = [&](Entity bodyEntity){
283310
if (bodyEntity != NULL_ENTITY){
284311
Transform* transform = scene->findComponent<Transform>(bodyEntity);
285312
if (transform){
286-
return transform->modelMatrix * Vector3(localPoint.x, localPoint.y, 0.0f);
313+
return transform->worldPosition;
287314
}
288315
}
289316

290-
return Vector3(localPoint.x, localPoint.y, 0.0f);
317+
return Vector3::ZERO;
318+
};
319+
320+
auto toWorldPoint = [](const Vector2& point){
321+
return Vector3(point.x, point.y, 0.0f);
291322
};
292323

293324
float alpha = highlighted ? 1.0f : 0.35f;
@@ -297,8 +328,28 @@ void editor::SceneRender2D::createOrUpdateJointLines(Entity entity, const Joint2
297328
const Vector4 axisColor(0.2f, 0.9f, 1.0f, alpha);
298329
const Vector4 limitColor(1.0f, 0.35f, 0.35f, alpha);
299330

300-
Vector3 anchorA = getBodyWorldPoint(joint.bodyA, joint.anchorA);
301-
Vector3 anchorB = getBodyWorldPoint(joint.bodyB, joint.anchorB);
331+
Vector3 anchorA = getBodyWorldPosition(joint.bodyA);
332+
Vector3 anchorB = getBodyWorldPosition(joint.bodyB);
333+
334+
switch (joint.type){
335+
case Joint2DType::DISTANCE:
336+
if (!joint.autoAnchors){
337+
anchorA = toWorldPoint(joint.anchorA);
338+
anchorB = toWorldPoint(joint.anchorB);
339+
}
340+
break;
341+
case Joint2DType::REVOLUTE:
342+
case Joint2DType::PRISMATIC:
343+
case Joint2DType::WHEEL:
344+
case Joint2DType::WELD:
345+
anchorA = anchorB = toWorldPoint(joint.anchorA);
346+
break;
347+
case Joint2DType::MOUSE:
348+
anchorB = toWorldPoint(joint.target);
349+
break;
350+
case Joint2DType::MOTOR:
351+
break;
352+
}
302353

303354
jointLinesObj->addLine(anchorA, anchorB, jointColor);
304355

@@ -394,7 +445,6 @@ void editor::SceneRender2D::createOrUpdateJointLines(Entity entity, const Joint2
394445
}
395446
case Joint2DType::MOUSE:{
396447
Vector3 target(joint.target.x, joint.target.y, 0.0f);
397-
jointLinesObj->addLine(anchorA, target, Vector4(1.0f, 0.4f, 0.4f, alpha));
398448
LineDrawUtils::addCircle2D(jointLinesObj, target, 8.0f * zoom, Vector4(1.0f, 0.4f, 0.4f, alpha), 16);
399449
jointLinesObj->addLine(target + Vector3(-markSize, 0.0f, 0.0f), target + Vector3(markSize, 0.0f, 0.0f), limitColor);
400450
jointLinesObj->addLine(target + Vector3(0.0f, -markSize, 0.0f), target + Vector3(0.0f, markSize, 0.0f), limitColor);

0 commit comments

Comments
 (0)