Skip to content

Commit

Permalink
feat: support bounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed Apr 28, 2024
1 parent f79702d commit f8a95c9
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 21 deletions.
87 changes: 77 additions & 10 deletions demo/src/main/java/io/github/jmecn/tiled/demo/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public void simpleInitApp() {
float sx = (float) obj.getX();
float sy = (float) obj.getY();


int layerIndex = tiledMap.getLayer("Trees").getIndex();
int layerIndex = tiledMap.getLayer("Objects").getIndex();
float y = mapRenderer.getLayerYIndex(layerIndex) + mapRenderer.getObjectTopDownYIndex(sy);

// Create player
Expand Down Expand Up @@ -143,7 +142,7 @@ private void initPhysics(PhysicsState physicsState, MapRenderer mapRenderer) {
String sensorBehavior = (String) collision.getProperties().get("sensor_behavior");
Body body = createObjectBody(physicsState, pos, size, collision, isSensor);
if (isSensor) {
SensorControl control = new SensorControl(body, sensorBehavior);// TODO cache for later use
SensorControl control = new SensorControl(body, sensorBehavior);
physicsState.addContactListener(control);
Spatial spatial = mapRenderer.getMapObjectSpatial(objGroup, obj);
if (spatial != null) {
Expand All @@ -152,6 +151,22 @@ private void initPhysics(PhysicsState physicsState, MapRenderer mapRenderer) {
}
}
}
} else {
String bodyType = (String) obj.getProperties().get("body_type");
if (bodyType == null) {
// not a physics object
continue;
}
boolean isSensor = Boolean.TRUE.equals(obj.getProperties().get("is_sensor"));
String sensorBehavior = (String) obj.getProperties().get("sensor_behavior");
Vector2f size = new Vector2f((float) obj.getWidth(), (float) obj.getHeight());
Vector2f pos = new Vector2f((float) obj.getX(), (float) obj.getY());
logger.info("Create object {} body at: {}, {}", obj.getName(), pos, size);
Body body = createObjectBody(physicsState, obj, isSensor);
if (isSensor) {
SensorControl control = new SensorControl(body, sensorBehavior);
physicsState.addContactListener(control);
}
}
}
}
Expand All @@ -173,7 +188,6 @@ private Body createTileBody(PhysicsState physicsState, Vector2f pos, Vector2f si

BodyDef bodyDef = new BodyDef();

float delta = (float) (size.y - obj.getHeight());
switch (obj.getShape()) {
case RECTANGLE: {
float hx = (float) (obj.getWidth() * 0.5);
Expand Down Expand Up @@ -203,7 +217,7 @@ private Body createTileBody(PhysicsState physicsState, Vector2f pos, Vector2f si
break;
}
default: {
logger.warn("Unsupported shape: {}", obj.getShape());
logger.warn("Unsupported tile collision shape: {}", obj.getShape());
return null;
}
}
Expand Down Expand Up @@ -273,20 +287,73 @@ private Body createObjectBody(PhysicsState physicsState, Vector2f pos, Vector2f
return physicsState.createBody(bodyDef, fixtureDef);
}


/**
* MapObject coordinate origin is on left-bottom corner.
* @param physicsState
* @param obj
*/
private Body createObjectBody(PhysicsState physicsState, MapObject obj, boolean isSensor) {
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
fixtureDef.isSensor = isSensor;

BodyDef bodyDef = new BodyDef();

switch (obj.getShape()) {
case RECTANGLE: {
float hx = (float) (obj.getWidth() * 0.5);
float hy = (float) (obj.getHeight() * 0.5);
PolygonShape shape = new PolygonShape();
shape.setAsBox(hx, hy, new Vec2(hx, hy), 0);
fixtureDef.shape = shape;
break;
}
case POLYGON: {
List<Vec2> vertices = new ArrayList<>();
for (Vector2f v : obj.getPoints()) {
vertices.add(new Vec2(v.x, v.y));
}
PolygonShape shape = new PolygonShape();
shape.set(vertices.toArray(new Vec2[0]), vertices.size());
fixtureDef.shape = shape;
break;
}
case ELLIPSE: {// box2d dose not support ellipse, use circle instead
float hx = (float) (obj.getWidth() * 0.5);
float hy = (float) (obj.getHeight() * 0.5);
CircleShape shape = new CircleShape();
shape.m_radius = Math.min(hx, hy);
shape.m_p.set(hx, -hy);
fixtureDef.shape = shape;
break;
}
default: {
logger.warn("Unsupported shape: {}", obj.getShape());
return null;
}
}

bodyDef.position.set((float) (obj.getX()), (float) (obj.getY()));
bodyDef.type = BodyType.STATIC;

return physicsState.createBody(bodyDef, fixtureDef);
}

private Body createPlayBody(PhysicsState physicsState, double x, double y, float width, float height) {
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;

float hx = width / 2;
float hy = height / 2;
float qx = width / 4;
float qy = height / 4;

PolygonShape shape = new PolygonShape();
shape.setAsBox(4, 4, new Vec2(8, -4), 0);
// CircleShape shape = new CircleShape();
// shape.m_radius = hx;
// shape.m_p.set(hx, hy);
shape.setAsBox(qx, qy, new Vec2(hx, -qy), 0);
fixtureDef.shape = shape;

BodyDef bodyDef = new BodyDef();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.jmecn.tiled.demo.control;

import com.jme3.math.FastMath;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
Expand All @@ -23,22 +24,44 @@
public class SensorControl extends AbstractControl implements ContactListener {
Logger logger = LoggerFactory.getLogger(SensorControl.class);

public static final String BEHAVIOR_HIDE = "hide";

private int count;// in case of multiple contacts
private final Body body;
private final String behavior;
private float opacity;

public SensorControl(Body body, String behavior) {
this.body = body;
this.behavior = behavior;
this.count = 0;
if (behavior.startsWith(BEHAVIOR_HIDE)) {
this.behavior = BEHAVIOR_HIDE;
this.opacity = 0.5f;// default
// "hide:0.5"
String[] parts = behavior.split(":");
if (parts.length == 2) {
try {
this.opacity = FastMath.clamp(Float.parseFloat(parts[1]), 0.0f, 1.0f);
} catch (NumberFormatException e) {
logger.error("Invalid behavior:{}", behavior);
}
}
} else {
this.opacity = 1.0f;
this.behavior = behavior;
}
}

@Override
protected void controlUpdate(float tpf) {
if (count > 0) {
setOpacity(0.6f);
if (BEHAVIOR_HIDE.equals(behavior)) {
setOpacity(opacity);
}
} else {
setOpacity(1.0f);
if (BEHAVIOR_HIDE.equals(behavior)) {
setOpacity(1.0f);
}
}
}

Expand Down
31 changes: 26 additions & 5 deletions demo/src/main/resources/Maps/jungle.tmx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="7" nextobjectid="43">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#22c07f" nextlayerid="7" nextobjectid="48">
<tileset firstgid="1" source="jungle.tsx"/>
<tileset firstgid="463" source="tree.tsx"/>
<layer id="1" name="Ground" width="30" height="20">
<data encoding="base64" compression="zlib">
eJy9lDEOwjAMRXOMrMBQUqSsiKFADwAMJfe/ColUiy8Tm7RNO7wlqvMa+8s7Y8x+Yw6Rc+S6MpZxmejl9YQXzpqx7jjiVvI24EhnPau/FXglR85J3g7qfebOOV6vYAXvaaGX3y9B3jB+P0TeQFC8VvCW9LyDN2k16CUfZcMVeDFHiX6Bl89I6nFjfvPaQi+neC3MJCh11NNWmN8cbyk4y9JdkvPyWTmljvZQDS/ONXFn/8Hzg7OUvATPqubV+OfM+aW9wb08M1J+ShjMd59wr7RbaoHZXZLnWt4tQO8j8tyYV+QD8JlpNA==
eJzlldENgzAMRD0GG7SAxAYYmIF2/1UapFg6WcSyk0aq1I/7w/diJz4GIhr+VI+kJ2j8opfluSZx0pK1F2o9ftqLs7YCF7+bkl4OzXDGMfd6gJf4LQaXyT7bnc7M5sw/FAt153mqPt6B+0S2ni2T3W+rVsfdRHqJcHv084tcecNRbmtmyA7uwVq9u5F66fV6U3OwFrkRNjKvtyr75GXrzPBmJTLFC9mePK3JyNJOYh9WTrZkVWR+vfcM/0c1mV8rPb+eORnRBxeiVjs=
</data>
</layer>
<layer id="2" name="Plants" width="30" height="20">
<data encoding="base64" compression="zlib">
eJxjYBh8oHagHTAKUIArEtsUjzpi4+0qBW6hBHyA0q54VdEPeA60AwiAi2h8WsbbLSS2MCOqnDganxLwBo3/Aasq/IAa8VaHxm8iUT8pcUPN9E6N8CMVROEQx1cWEQIlBOTRzcblBmLBLcJKqAJIjZ/BXgYNdQAA1sAR8Q==
eJxjYBh8oHagHTAKUIArkeqIjber5DqEQvABShPrH1oDz4F2AAFwEY1Py3i7hcQWZkSVE0fjUwLeoPE/YFWFH1Aj3urQ+E0k6iclbqiZ3qkRfqSCKDrYQQhQ6oZbhJVQBZAaP4O9DBrqAAA9pxDe
</data>
</layer>
<objectgroup id="6" name="Trees">
<objectgroup id="6" name="Objects">
<object id="29" gid="463" x="105.5" y="187" width="80" height="80"/>
<object id="31" gid="465" x="105.5" y="203" width="80" height="16"/>
<object id="33" gid="465" x="200" y="116.5" width="80" height="16"/>
Expand All @@ -24,8 +24,29 @@
<object id="40" gid="466" x="53.5" y="176.5" width="32" height="32"/>
<object id="41" gid="466" x="96" y="288" width="32" height="32"/>
<object id="42" gid="463" x="365" y="192.5" width="80" height="80"/>
<object id="43" name="north-bound" x="0" y="-8" width="480" height="8" visible="0">
<properties>
<property name="body_type" value="static"/>
</properties>
</object>
<object id="44" name="west-bound" x="-8" y="0" width="8" height="320" visible="0">
<properties>
<property name="body_type" value="static"/>
</properties>
</object>
<object id="45" name="east-bound" x="480" y="0" width="8" height="320" visible="0">
<properties>
<property name="body_type" value="static"/>
</properties>
</object>
<object id="46" name="south-bound" x="0" y="320" width="480" height="8" visible="0">
<properties>
<property name="body_type" value="static"/>
</properties>
</object>
<object id="47" gid="466" x="299.714" y="252.571" width="32" height="32"/>
</objectgroup>
<objectgroup id="4" name="Location" visible="0">
<objectgroup id="4" name="Location">
<object id="15" name="Start Point" x="70" y="242.833">
<point/>
</object>
Expand Down
35 changes: 35 additions & 0 deletions demo/src/main/resources/Maps/jungle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,39 @@
</object>
</objectgroup>
</tile>
<wangsets>
<wangset name="Grass" type="corner" tile="-1">
<wangcolor name="bush" color="#ff0000" tile="-1" probability="1"/>
<wangcolor name="grass" color="#00ff00" tile="-1" probability="1"/>
<wangcolor name="dirt" color="#0000ff" tile="-1" probability="1"/>
<wangtile tileid="23" wangid="0,1,0,1,0,1,0,1"/>
<wangtile tileid="32" wangid="0,1,0,2,0,1,0,1"/>
<wangtile tileid="33" wangid="0,1,0,2,0,2,0,1"/>
<wangtile tileid="34" wangid="0,1,0,1,0,2,0,1"/>
<wangtile tileid="35" wangid="0,2,0,1,0,2,0,2"/>
<wangtile tileid="36" wangid="0,2,0,2,0,1,0,2"/>
<wangtile tileid="38" wangid="0,1,0,3,0,1,0,1"/>
<wangtile tileid="39" wangid="0,1,0,3,0,3,0,1"/>
<wangtile tileid="40" wangid="0,1,0,1,0,3,0,1"/>
<wangtile tileid="41" wangid="0,3,0,1,0,3,0,3"/>
<wangtile tileid="42" wangid="0,3,0,3,0,1,0,3"/>
<wangtile tileid="45" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="54" wangid="0,2,0,2,0,1,0,1"/>
<wangtile tileid="55" wangid="0,2,0,2,0,2,0,2"/>
<wangtile tileid="56" wangid="0,1,0,1,0,2,0,2"/>
<wangtile tileid="57" wangid="0,1,0,2,0,2,0,2"/>
<wangtile tileid="58" wangid="0,2,0,2,0,2,0,1"/>
<wangtile tileid="60" wangid="0,3,0,3,0,1,0,1"/>
<wangtile tileid="61" wangid="0,3,0,3,0,3,0,3"/>
<wangtile tileid="62" wangid="0,1,0,1,0,3,0,3"/>
<wangtile tileid="63" wangid="0,1,0,3,0,3,0,3"/>
<wangtile tileid="64" wangid="0,3,0,3,0,3,0,1"/>
<wangtile tileid="76" wangid="0,2,0,1,0,1,0,1"/>
<wangtile tileid="77" wangid="0,2,0,1,0,1,0,2"/>
<wangtile tileid="78" wangid="0,1,0,1,0,1,0,2"/>
<wangtile tileid="82" wangid="0,3,0,1,0,1,0,1"/>
<wangtile tileid="83" wangid="0,3,0,1,0,1,0,3"/>
<wangtile tileid="84" wangid="0,1,0,1,0,1,0,3"/>
</wangset>
</wangsets>
</tileset>
13 changes: 10 additions & 3 deletions demo/src/main/resources/Maps/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<properties>
<property name="body_type" value="static"/>
<property name="is_sensor" type="bool" value="true"/>
<property name="sensor_behavior" value="hide"/>
<property name="sensor_behavior" value="hide:0.5"/>
</properties>
<polygon points="0,0 19.3724,-30.64 36.3726,-36.1749 76.5011,-14.2328 78.4779,4.15122 50.0124,33.6051 30.4423,33.6051 -0.197677,5.93032"/>
</object>
Expand All @@ -20,7 +20,7 @@
<properties>
<property name="body_type" value="static"/>
<property name="is_sensor" type="bool" value="true"/>
<property name="sensor_behavior" value="hide"/>
<property name="sensor_behavior" value="hide:0.5"/>
</properties>
<polygon points="0.197677,-3.36051 26.4888,-26.2911 55.5473,-22.9306 77.6872,-0.988386 77.6872,15.0235 49.024,39.9308 31.0353,40.1285 -0.395355,6.72103"/>
</object>
Expand All @@ -34,7 +34,6 @@
<property name="body_type" value="static"/>
</properties>
</object>
<object id="2" x="17" y="0" width="46" height="14"/>
</objectgroup>
</tile>
<tile id="3" x="16" y="304" width="32" height="32">
Expand All @@ -45,6 +44,14 @@
<property name="body_type" value="static"/>
</properties>
</object>
<object id="3" x="3.75587" y="15.2212">
<properties>
<property name="body_type" value="static"/>
<property name="is_sensor" type="bool" value="true"/>
<property name="sensor_behavior" value="hide:0.5"/>
</properties>
<polygon points="0,0 0.197677,-2.96516 7.11638,-9.29083 17.3956,-9.09316 23.919,-2.37213 24.2155,0"/>
</object>
</objectgroup>
</tile>
<tile id="4" x="48" y="304" width="32" height="16">
Expand Down

0 comments on commit f8a95c9

Please sign in to comment.