Last active
October 19, 2018 11:42
-
-
Save dlon/f09be9ae8ebcfb198275e1c668743688 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2011 See AUTHORS file. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
******************************************************************************/ | |
package com.badlogic.gdx.tests.box2d; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.*; | |
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; | |
import java.util.Arrays; | |
public class ShapeCollisionTest extends Box2DTest { | |
Body b1; | |
Body b2; | |
@Override | |
protected void createWorld (World world) { | |
{ | |
EdgeShape shape = new EdgeShape(); | |
shape.set(new Vector2(-40.0f, 0), new Vector2(40, 0)); | |
FixtureDef fd = new FixtureDef(); | |
fd.shape = shape; | |
fd.friction = 0.3f; | |
BodyDef bd = new BodyDef(); | |
Body ground = world.createBody(bd); | |
ground.createFixture(fd); | |
shape.dispose(); | |
} | |
{ | |
PolygonShape shape = new PolygonShape(); | |
shape.setAsBox(1, 2f); | |
EdgeShape edgeShape = new EdgeShape(); | |
edgeShape.set(new Vector2(-10f, 0), new Vector2(10.0f, 0)); | |
CircleShape circleShape = new CircleShape(); | |
circleShape.setRadius(2f); | |
BodyDef def = new BodyDef(); | |
def.position.y = 10; | |
def.angle = (float)Math.toRadians(90); | |
def.type = BodyType.DynamicBody; | |
FixtureDef fixtureDef = new FixtureDef(); | |
fixtureDef.isSensor = true; | |
fixtureDef.density = 1f; | |
Body body = world.createBody(def); | |
fixtureDef.shape = shape; | |
body.createFixture(fixtureDef); | |
fixtureDef.shape = edgeShape; | |
body.createFixture(fixtureDef); | |
fixtureDef.shape = circleShape; | |
body.createFixture(fixtureDef); | |
b1 = body; | |
def = new BodyDef(); | |
def.position.x = 10; | |
def.position.y = 10; | |
def.angle = 0; | |
def.type = BodyType.DynamicBody; | |
body = world.createBody(def); | |
body.createFixture(shape, 1); | |
body.createFixture(circleShape, 1); | |
body.createFixture(edgeShape, 1); | |
b2 = body; | |
shape.dispose(); | |
circleShape.dispose(); | |
edgeShape.dispose(); | |
} | |
} | |
@Override | |
public void render() { | |
super.render(); | |
//Transform t1 = new Transform(b1.getTransform()); | |
//Transform t2 = new Transform(b2.getTransform()); | |
Transform t1 = b1.getTransform(); | |
Transform t2 = b2.getTransform(); | |
// generic overlap test (polygons) | |
System.out.println("testOverlap (polygons): " + Collision.testOverlap( | |
b1.getFixtureList().get(0).getShape(), | |
0, | |
b2.getFixtureList().get(0).getShape(), | |
0, | |
t1, t2 | |
)); | |
// polygon-polygon | |
Manifold m = Collision.collidePolygons( | |
(PolygonShape) b1.getFixtureList().get(0).getShape(), | |
t1, | |
(PolygonShape) b2.getFixtureList().get(0).getShape(), | |
t2 | |
); | |
System.out.println("Polygon-polygon: " + m.getPointCount() + "; " + m.getLocalPoint() + "; " + Arrays.toString(m.getPoints())); | |
// polygon-edge | |
m = Collision.collideEdgeAndPolygon( | |
(EdgeShape) b1.getFixtureList().get(1).getShape(), | |
t1, | |
(PolygonShape) b2.getFixtureList().get(0).getShape(), | |
t2 | |
); | |
System.out.println("Polygon-edge: " + m.getPointCount() + "; " + m.getLocalPoint() + "; " + Arrays.toString(m.getPoints())); | |
// polygon-circle | |
m = Collision.collidePolygonAndCircle( | |
(PolygonShape) b2.getFixtureList().get(0).getShape(), | |
t2, | |
(CircleShape) b1.getFixtureList().get(2).getShape(), | |
t1 | |
); | |
System.out.println("Polygon-circle: " + m.getPointCount() + "; " + m.getLocalPoint() + "; " + Arrays.toString(m.getPoints())); | |
// circle-circle | |
m = Collision.collideCircles( | |
(CircleShape) b1.getFixtureList().get(2).getShape(), | |
t1, | |
(CircleShape) b2.getFixtureList().get(1).getShape(), | |
t2 | |
); | |
System.out.println("Circle-circle: " + m.getPointCount() + "; " + m.getLocalPoint() + "; " + Arrays.toString(m.getPoints())); | |
// circle-edge | |
m = Collision.collideEdgeAndCircle( | |
(EdgeShape) b2.getFixtureList().get(2).getShape(), | |
t2, | |
(CircleShape) b1.getFixtureList().get(2).getShape(), | |
t1 | |
); | |
System.out.println("Circle-edge: " + m.getPointCount() + "; " + m.getLocalPoint() + "; " + Arrays.toString(m.getPoints())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment