Skip to content

Commit

Permalink
Better render for collision shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
Retera committed Sep 11, 2022
1 parent 8d0acff commit a3b8932
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ public void attachment(final Attachment attachment) {
private void toggleSelection(final Set<Vertex> selection, final Vertex position) {
if (selection.contains(position)) {
selection.remove(position);
}
else {
} else {
selection.add(position);
}
}
Expand Down Expand Up @@ -490,8 +489,7 @@ public UndoAction selectHDUnusedNodes() {
usedBones.add(bone);
}
}
}
else {
} else {
for (final Bone bone : vertex.getBoneAttachments()) {
usedBones.add(bone);
}
Expand All @@ -500,7 +498,10 @@ public UndoAction selectHDUnusedNodes() {
}
final Set<Vertex> allSelection = new HashSet<>();
for (final IdObject node : model.getEditableIdObjects()) {
if ((node instanceof Bone) && !usedBones.contains(node)) {
if (node instanceof Bone && !usedBones.contains(node) && !(node instanceof Helper)) {
if (node.getChildrenNodes() != null && !node.getChildrenNodes().isEmpty()) {
System.out.println("note: should maybe be helper: " + node.getName());
}
allSelection.add(node.getPivotPoint());
}
}
Expand Down Expand Up @@ -555,7 +556,7 @@ public static void hitTest(final List<Vertex> selectedItems, final Rectangle2D a
final double x = coordinateSystem.convertX(vertexX);
final double vertexY = geosetVertex.getCoord(dim2);
final double y = coordinateSystem.convertY(vertexY);
if ((distance(x, y, minX, minY) <= (vertexSize / 2.0)) || (distance(x, y, maxX, maxY) <= (vertexSize / 2.0))
if (distance(x, y, minX, minY) <= vertexSize / 2.0 || distance(x, y, maxX, maxY) <= vertexSize / 2.0
|| area.contains(vertexX, vertexY)) {
selectedItems.add(geosetVertex);
}
Expand All @@ -567,13 +568,13 @@ public static boolean hitTest(final Vertex vertex, final Point2D point, final Co
final double y = coordinateSystem.convertY(vertex.getCoord(coordinateSystem.getPortSecondXYZ()));
final double px = coordinateSystem.convertX(point.getX());
final double py = coordinateSystem.convertY(point.getY());
return Point2D.distance(px, py, x, y) <= (vertexSize / 2.0);
return Point2D.distance(px, py, x, y) <= vertexSize / 2.0;
}

public static double distance(final double vertexX, final double vertexY, final double x, final double y) {
final double dx = x - vertexX;
final double dy = y - vertexY;
return Math.sqrt((dx * dx) + (dy * dy));
return Math.sqrt(dx * dx + dy * dy);
}

@Override
Expand Down Expand Up @@ -731,10 +732,21 @@ public void eventObject(final EventObject eventObject) {
@Override
public void collisionShape(final CollisionShape collisionShape) {
handleDefaultNode(point, axes, collisionShape);
int vertexIndex = 0;
// TODO probably doesnt work in obscure case of pyramid or cylinder collision
// shape or whatever:
final boolean collisionSphere = !collisionShape.getFlags().contains("Box");

for (final Vertex vertex : collisionShape.getVertices()) {
if (hitTest(vertex, CoordinateSystem.Util.geom(axes, point), axes, IdObject.DEFAULT_CLICK_RADIUS)) {
double objectClickRadius = IdObject.DEFAULT_CLICK_RADIUS;
if (collisionSphere && vertexIndex == 0 && collisionShape.getExtents() != null
&& collisionShape.getExtents().hasBoundsRadius()) {
objectClickRadius = collisionShape.getExtents().getBoundsRadius() * 2;
}
if (hitTest(vertex, CoordinateSystem.Util.geom(axes, point), axes, objectClickRadius)) {
mouseOverVertex = true;
}
vertexIndex++;
}
}

Expand Down Expand Up @@ -832,8 +844,19 @@ public void collisionShape(final CollisionShape collisionShape) {
hitTest(selectedItems, area, collisionShape.getPivotPoint(), coordinateSystem,
collisionShape.getClickRadius(coordinateSystem) * CoordinateSystem.Util.getZoom(coordinateSystem)
* 2);
int vertexIndex = 0;
// TODO probably doesnt work in obscure case of pyramid or cylinder collision
// shape or whatever:
final boolean collisionSphere = !collisionShape.getFlags().contains("Box");

for (final Vertex vertex : collisionShape.getVertices()) {
hitTest(selectedItems, area, vertex, coordinateSystem, IdObject.DEFAULT_CLICK_RADIUS);
double objectClickRadius = IdObject.DEFAULT_CLICK_RADIUS;
if (collisionSphere && vertexIndex == 0 && collisionShape.getExtents() != null
&& collisionShape.getExtents().hasBoundsRadius()) {
objectClickRadius = collisionShape.getExtents().getBoundsRadius() * 2;
}
hitTest(selectedItems, area, vertex, coordinateSystem, objectClickRadius);
vertexIndex++;
}
}

Expand Down Expand Up @@ -885,7 +908,7 @@ public void rawScale(final double centerX, final double centerY, final double ce
final double scaleY, final double scaleZ) {
super.rawScale(centerX, centerY, centerZ, scaleX, scaleY, scaleZ);
for (final IdObject b : model.getEditableIdObjects()) {
if (selectionManager.getSelection().contains(b.getPivotPoint())) {
if (selectionManager.getSelection().contains(b.getPivotPoint()) || collisionShapeSelected(b)) {
b.apply(new IdObjectVisitor() {
@Override
public void ribbonEmitter(final RibbonEmitter particleEmitter) {
Expand Down Expand Up @@ -931,7 +954,7 @@ public void eventObject(final EventObject eventObject) {
@Override
public void collisionShape(final CollisionShape collisionShape) {
final ExtLog extents = collisionShape.getExtents();
if ((extents != null) && (scaleX == scaleY) && (scaleY == scaleZ)) {
if (extents != null && scaleX == scaleY && scaleY == scaleZ) {
extents.setBoundsRadius(extents.getBoundsRadius() * scaleX);
}
}
Expand Down Expand Up @@ -965,6 +988,11 @@ public void attachment(final Attachment attachment) {
}
}

private boolean collisionShapeSelected(final IdObject b) {
return b instanceof CollisionShape && ((CollisionShape) b).getVertices().size() > 0
&& selectionManager.getSelection().contains(((CollisionShape) b).getVertices().get(0));
}

@Override
public void rawTranslate(final double x, final double y, final double z) {
super.rawTranslate(x, y, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public void light(final Light light) {
// vertexSize * 3, yCoord);
// graphics.drawLine(xCoord, yCoord - vertexSize * 3, xCoord, yCoord +
// vertexSize * 3);
graphics.drawImage(lightImage, xCoord - (lightImage.getWidth(null) / 2),
yCoord - (lightImage.getHeight(null) / 2), lightImage.getWidth(null), lightImage.getHeight(null), null);
graphics.drawImage(lightImage, xCoord - lightImage.getWidth(null) / 2, yCoord - lightImage.getHeight(null) / 2,
lightImage.getWidth(null), lightImage.getHeight(null), null);

final int attenuationStart = (int) (light.getAttenuationStart() * zoom);
if (attenuationStart > 0) {
Expand Down Expand Up @@ -135,7 +135,7 @@ public void collisionShape(final CollisionShape collisionShape) {
@Override
public void camera(final Camera camera) {
graphics.setColor(Color.GREEN.darker());
final Graphics2D g2 = ((Graphics2D) graphics.create());
final Graphics2D g2 = (Graphics2D) graphics.create();
final Vertex ver = camera.getPosition();
final Vertex targ = camera.getTargetPosition();
// final boolean verSel = selection.contains(ver);
Expand Down Expand Up @@ -172,7 +172,7 @@ public void camera(final Camera camera) {
// }

g2.translate(end.x, end.y);
g2.rotate(-((Math.PI / 2) + Math.atan2(end.x - start.x, end.y - start.y)));
g2.rotate(-(Math.PI / 2 + Math.atan2(end.x - start.x, end.y - start.y)));
final double zoom = CoordinateSystem.Util.getZoom(coordinateSystem);
final int size = (int) (20 * zoom);
final double dist = start.distance(end);
Expand All @@ -181,7 +181,7 @@ public void camera(final Camera camera) {
// g2.setColor(Color.orange.darker());
// }
// Cam
g2.fillRect((int) dist - vertexSize, 0 - vertexSize, 1 + (vertexSize * 2), 1 + (vertexSize * 2));
g2.fillRect((int) dist - vertexSize, 0 - vertexSize, 1 + vertexSize * 2, 1 + vertexSize * 2);
g2.drawRect((int) dist - size, -size, size * 2, size * 2);

// if (tarSel) {
Expand All @@ -190,7 +190,7 @@ public void camera(final Camera camera) {
// g2.setColor(Color.green.darker());
// }
// Target
g2.fillRect(0 - vertexSize, 0 - vertexSize, 1 + (vertexSize * 2), 1 + (vertexSize * 2));
g2.fillRect(0 - vertexSize, 0 - vertexSize, 1 + vertexSize * 2, 1 + vertexSize * 2);
g2.drawLine(0, 0, size, size);// (int)Math.round(vp.convertX(targ.getCoord(vp.getPortFirstXYZ())+5)),
// (int)Math.round(vp.convertY(targ.getCoord(vp.getPortSecondXYZ())+5)));
g2.drawLine(0, 0, size, -size);// (int)Math.round(vp.convertX(targ.getCoord(vp.getPortFirstXYZ())-5)),
Expand All @@ -211,7 +211,7 @@ public static void drawNodeImage(final Graphics2D graphics, final byte xDimensio
final CoordinateSystem coordinateSystem, final IdObject attachment, final Image nodeImage) {
final int xCoord = (int) coordinateSystem.convertX(attachment.getPivotPoint().getCoord(xDimension));
final int yCoord = (int) coordinateSystem.convertY(attachment.getPivotPoint().getCoord(yDimension));
graphics.drawImage(nodeImage, xCoord - (nodeImage.getWidth(null) / 2), yCoord - (nodeImage.getHeight(null) / 2),
graphics.drawImage(nodeImage, xCoord - nodeImage.getWidth(null) / 2, yCoord - nodeImage.getHeight(null) / 2,
nodeImage.getWidth(null), nodeImage.getHeight(null), null);
}

Expand All @@ -221,8 +221,8 @@ public static void drawCollisionShape(final Graphics2D graphics, final Color col
final Vertex pivotPoint = collisionShape.getPivotPoint();
final ArrayList<Vertex> vertices = collisionShape.getVertices();
graphics.setColor(color);
final int xCoord = (int) coordinateSystem.convertX(pivotPoint.getCoord(xDimension));
final int yCoord = (int) coordinateSystem.convertY(pivotPoint.getCoord(yDimension));
int xCoord = (int) coordinateSystem.convertX(pivotPoint.getCoord(xDimension));
int yCoord = (int) coordinateSystem.convertY(pivotPoint.getCoord(yDimension));
if (collisionShape.getFlags().contains("Box")) {
if (vertices.size() > 1) {
final Vertex vertex = vertices.get(0);
Expand All @@ -244,6 +244,11 @@ public static void drawCollisionShape(final Graphics2D graphics, final Color col
if (collisionShape.getExtents() != null) {
final double zoom = CoordinateSystem.Util.getZoom(coordinateSystem);
final double boundsRadius = collisionShape.getExtents().getBoundsRadius() * zoom;
if (vertices.size() > 0) {
final Vertex firstVertex = vertices.get(0);
xCoord = (int) coordinateSystem.convertX(firstVertex.getCoord(xDimension));
yCoord = (int) coordinateSystem.convertY(firstVertex.getCoord(yDimension));
}
graphics.drawOval((int) (xCoord - boundsRadius), (int) (yCoord - boundsRadius),
(int) (boundsRadius * 2), (int) (boundsRadius * 2));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void light(final Light light) {
// vertexSize * 3, yCoord);
// graphics.drawLine(xCoord, yCoord - vertexSize * 3, xCoord, yCoord +
// vertexSize * 3);
graphics.drawImage(lightImage, xCoord - (lightImage.getWidth(null) / 2),
yCoord - (lightImage.getHeight(null) / 2), lightImage.getWidth(null), lightImage.getHeight(null), null);
graphics.drawImage(lightImage, xCoord - lightImage.getWidth(null) / 2, yCoord - lightImage.getHeight(null) / 2,
lightImage.getWidth(null), lightImage.getHeight(null), null);

final int attenuationStart = (int) (light.getAttenuationStart() * zoom);
if (attenuationStart > 0) {
Expand Down Expand Up @@ -141,7 +141,7 @@ public void collisionShape(final CollisionShape collisionShape) {
@Override
public void camera(final Camera camera) {
graphics.setColor(Color.GREEN.darker());
final Graphics2D g2 = ((Graphics2D) graphics.create());
final Graphics2D g2 = (Graphics2D) graphics.create();
final Vertex ver = camera.getPosition();
final Vertex targ = camera.getTargetPosition();
// final boolean verSel = selection.contains(ver);
Expand Down Expand Up @@ -178,7 +178,7 @@ public void camera(final Camera camera) {
// }

g2.translate(end.x, end.y);
g2.rotate(-((Math.PI / 2) + Math.atan2(end.x - start.x, end.y - start.y)));
g2.rotate(-(Math.PI / 2 + Math.atan2(end.x - start.x, end.y - start.y)));
final double zoom = CoordinateSystem.Util.getZoom(coordinateSystem);
final int size = (int) (20 * zoom);
final double dist = start.distance(end);
Expand All @@ -187,7 +187,7 @@ public void camera(final Camera camera) {
// g2.setColor(Color.orange.darker());
// }
// Cam
g2.fillRect((int) dist - vertexSize, 0 - vertexSize, 1 + (vertexSize * 2), 1 + (vertexSize * 2));
g2.fillRect((int) dist - vertexSize, 0 - vertexSize, 1 + vertexSize * 2, 1 + vertexSize * 2);
g2.drawRect((int) dist - size, -size, size * 2, size * 2);

// if (tarSel) {
Expand All @@ -196,7 +196,7 @@ public void camera(final Camera camera) {
// g2.setColor(Color.green.darker());
// }
// Target
g2.fillRect(0 - vertexSize, 0 - vertexSize, 1 + (vertexSize * 2), 1 + (vertexSize * 2));
g2.fillRect(0 - vertexSize, 0 - vertexSize, 1 + vertexSize * 2, 1 + vertexSize * 2);
g2.drawLine(0, 0, size, size);// (int)Math.round(vp.convertX(targ.getCoord(vp.getPortFirstXYZ())+5)),
// (int)Math.round(vp.convertY(targ.getCoord(vp.getPortSecondXYZ())+5)));
g2.drawLine(0, 0, size, -size);// (int)Math.round(vp.convertX(targ.getCoord(vp.getPortFirstXYZ())-5)),
Expand All @@ -217,7 +217,7 @@ public static void drawNodeImage(final Graphics2D graphics, final byte xDimensio
final CoordinateSystem coordinateSystem, final IdObject attachment, final Image nodeImage) {
final int xCoord = (int) coordinateSystem.convertX(attachment.getPivotPoint().getCoord(xDimension));
final int yCoord = (int) coordinateSystem.convertY(attachment.getPivotPoint().getCoord(yDimension));
graphics.drawImage(nodeImage, xCoord - (nodeImage.getWidth(null) / 2), yCoord - (nodeImage.getHeight(null) / 2),
graphics.drawImage(nodeImage, xCoord - nodeImage.getWidth(null) / 2, yCoord - nodeImage.getHeight(null) / 2,
nodeImage.getWidth(null), nodeImage.getHeight(null), null);
}

Expand Down Expand Up @@ -245,8 +245,8 @@ public static void drawCollisionShape(final Graphics2D graphics, final Color col
final Vertex pivotPoint = collisionShape.getPivotPoint();
final ArrayList<Vertex> vertices = collisionShape.getVertices();
graphics.setColor(color);
final int xCoord = (int) coordinateSystem.convertX(pivotPoint.getCoord(xDimension));
final int yCoord = (int) coordinateSystem.convertY(pivotPoint.getCoord(yDimension));
int xCoord = (int) coordinateSystem.convertX(pivotPoint.getCoord(xDimension));
int yCoord = (int) coordinateSystem.convertY(pivotPoint.getCoord(yDimension));
if (collisionShape.getFlags().contains("Box")) {
if (vertices.size() > 1) {
final Vertex vertex = vertices.get(0);
Expand All @@ -270,6 +270,11 @@ public static void drawCollisionShape(final Graphics2D graphics, final Color col
if (collisionShape.getExtents() != null) {
final double zoom = CoordinateSystem.Util.getZoom(coordinateSystem);
final double boundsRadius = collisionShape.getExtents().getBoundsRadius() * zoom;
if (vertices.size() > 0) {
final Vertex firstVertex = vertices.get(0);
xCoord = (int) coordinateSystem.convertX(firstVertex.getCoord(xDimension));
yCoord = (int) coordinateSystem.convertY(firstVertex.getCoord(yDimension));
}
graphics.drawOval((int) (xCoord - boundsRadius), (int) (yCoord - boundsRadius),
(int) (boundsRadius * 2), (int) (boundsRadius * 2));
drawNodeImage(graphics, xDimension, yDimension, coordinateSystem, collisionShape, collisionImage);
Expand Down
12 changes: 9 additions & 3 deletions craft3data/src/com/hiveworkshop/wc3/mdl/CollisionShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,16 @@ public double getClickRadius(final CoordinateSystem coordinateSystem) {
return DEFAULT_CLICK_RADIUS / CoordinateSystem.Util.getZoom(coordinateSystem);
}
}
if (extents == null) {
return DEFAULT_CLICK_RADIUS / CoordinateSystem.Util.getZoom(coordinateSystem);
return DEFAULT_CLICK_RADIUS / CoordinateSystem.Util.getZoom(coordinateSystem);
}

public double getSphereClickRadius(final CoordinateSystem coordinateSystem) {
if (extents != null) {
if (extents.hasBoundsRadius()) {
return extents.getBoundsRadius();
}
}
return extents.getBoundsRadius();
return DEFAULT_CLICK_RADIUS / CoordinateSystem.Util.getZoom(coordinateSystem);
}

@Override
Expand Down
Loading

0 comments on commit a3b8932

Please sign in to comment.