@Override public void draw(Batch batch, float parentAlpha) { batch.draw(texture, getX(), getY(), getWidth(), getHeight()); for(int i = 0; i < xCells; i++) for(int j = 0; j < yCells; j++) if(cells[i][j] != null) { batch.draw(cells[i][j].texture, getX() + i * getWidth() / xCells, getY() + j * getHeight() / yCells, getWidth() / xCells, getHeight() / yCells); if(cells[i][j].price > money.$) batch.draw(cellX, getX() + i * getWidth() / xCells, getY() + j * getHeight() / yCells, getWidth() / xCells, getHeight() / yCells); } if (paused) return; if(Gdx.input.isTouched() && lastCell != null) { Ray ray = camera.getPickRay(Gdx.input.getX(), Gdx.input.getY()); Vector3 pos = Tools.closestRayTest(btcollisionWorld, new ClosestRayResultCallback(ray.origin, new Vector3(ray.direction).setLength(9999).add(ray.origin))); if(pos != null && Tools.closestRayTestObject(btcollisionWorld, new ClosestRayResultCallback(ray.origin, new Vector3(ray.direction).setLength(9999).add(ray.origin))).getUserValue() == 0) { Vector3 vector = world.getVector(pos); if(vector != null && ghost != null) { ghost.transform.setToTranslation(pos); ghost.transform.rotate(vector, 30); } if(ghost != null) modelBatch.render(ghost, environment); } } }
@Override public void create () { super.create(); instructions = "Tap a box to ray cast\nLong press to toggle debug mode\nSwipe for next test\nCtrl+drag to rotate\nScroll to zoom"; // Create the entities world.add("ground", -7f, 0f, -7f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 1f); for (int x = 0; x < BOXCOUNT_X; x++) { for (int y = 0; y < BOXCOUNT_Y; y++) { for (int z = 0; z < BOXCOUNT_Z; z++) { world.add("box", BOXOFFSET_X + x, BOXOFFSET_Y + y, BOXOFFSET_Z + z).setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f); } } } rayTestCB = new ClosestRayResultCallback(Vector3.Zero, Vector3.Z); }
public static btCollisionObject closestRayTestObject(btCollisionWorld world, ClosestRayResultCallback callback) { callback.getRayFromWorld(rayFrom); callback.getRayToWorld(rayTo); callback.setCollisionObject(null); callback.setClosestHitFraction(1f); world.rayTest(rayFrom, rayTo, callback); if (callback.hasHit()) return callback.getCollisionObject(); return null; }
public static Vector3 closestRayTest(btCollisionWorld world, ClosestRayResultCallback callback) { callback.getRayFromWorld(rayFrom); callback.getRayToWorld(rayTo); callback.setCollisionObject(null); callback.setClosestHitFraction(1f); world.rayTest(rayFrom, rayTo, callback); if (callback.hasHit()) { callback.getHitPointWorld(tmp); return tmp; } return null; }
@Override public boolean touchDown (int screenX, int screenY, int pointer, int button) { boolean result = false; if (button == Buttons.LEFT) { Ray ray = camera.getPickRay(screenX, screenY); tmpV1.set(ray.direction).scl(10f).add(ray.origin); ClosestRayResultCallback cb = new ClosestRayResultCallback(ray.origin, tmpV1); world.collisionWorld.rayTest(ray.origin, tmpV1, cb); if (cb.hasHit()) { btRigidBody body = (btRigidBody)(cb.getCollisionObject()); if (body != null && !body.isStaticObject() && !body.isKinematicObject()) { pickedBody = body; body.setActivationState(Collision.DISABLE_DEACTIVATION); cb.getHitPointWorld(tmpV); tmpV.mul(body.getCenterOfMassTransform().inv()); pickConstraint = new btPoint2PointConstraint(body, tmpV); btConstraintSetting setting = pickConstraint.getSetting(); setting.setImpulseClamp(30f); setting.setTau(0.001f); pickConstraint.setSetting(setting); ((btDynamicsWorld)world.collisionWorld).addConstraint(pickConstraint); pickDistance = tmpV1.sub(camera.position).len(); result = true; } } cb.dispose(); } return result ? result : super.touchDown(screenX, screenY, pointer, button); }
private void executeRayCast(Vector3 position, Vector3 end, RayResultCallback callback) { raycastReport.reset(); world.rayTest(position, end, callback); raycastReport.hit = callback.hasHit(); if (raycastReport.hit) { float length = position.dst(end); raycastReport.hitDistance = length * callback.getClosestHitFraction(); if (callback instanceof ClosestRayResultCallback) { ClosestRayResultCallback cb = (ClosestRayResultCallback) callback; Vector3 normal = tmp; cb.getHitNormalWorld(tmp); raycastReport.hitNormal.set(normal.x, normal.y, normal.z); } } }
private void clearResult(ClosestRayResultCallback result) { result.setCollisionObject(null); result.setClosestHitFraction(1f); }
@Override public void create() { logger.info("Setting up camera"); camera = new PerspectiveCamera(40, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // camera.position.set(40f, 30f, 40f); camera.position.set(45f, 29f, 73f); // camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // camera.position.set(4f, 3f, 4f); camera.lookAt(0, 2f, 0); camera.near = 1f; camera.far = 500f; camera.update(); cameraInputController = new CameraInputController(camera); Gdx.input.setInputProcessor(cameraInputController); // Gdx.input.setInputProcessor(new InputMultiplexer(cameraController, this, new GestureDetector(this))); logger.info("Loading models"); models = new HashMap<>(); ModelBuilder builder = new ModelBuilder(); models.put("box", builder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(new Color(0.8f, 0f, 0f, 0f))), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal)); G3dModelLoader loader = new G3dModelLoader(new JsonReader()); models.put("hub", loader.loadModel(Gdx.files.internal("data/hubreal.g3dj"))); models.put("rim", loader.loadModel(Gdx.files.internal("data/rimreal.g3dj"))); models.put("spoke", loader.loadModel(Gdx.files.internal("data/spoke.g3dj"))); logger.info("Let there be light"); batch = new ModelBatch(); environment = new Environment(); environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.6f, 0.6f, 0.6f, 1f)); environment.add(new DirectionalLight().set(0.3f, 0.3f, 0.3f, -5f, -3f, -1f)); environment.add(new DirectionalLight().set(0.2f, 0.2f, 0.2f, 5f, 8f, 0f)); environment.add(new DirectionalLight().set(0.7f, 0.7f, 0.7f, -3f, -2f, -5f)); logger.info("Initializing Bullet"); Bullet.init(); logger.info("Creating simulation"); // create world & stuff? spokeAngles = new SpokeAngles(); // spokeAngles.create2l2t(); // spokeAngles.createCrowFoot(); // spokeAngles.createNx(3); // spokeAngles.createPer3mutation(new int[]{3, 14, 5, 0, 7, 2, 9, 4, 11, 6, 13, 8, 15, 10, 1, 12}); // 3x as well! // spokeAngles.createPermutation(new int[]{14, 2, 3, 15, 7, 5, 6, 10, 4, 9, 8, 11, 12, 0, 1, 13}); // spokeAngles.createPermutation(new int[]{2, 0, 1, 5, 3, 8, 10, 4, 12, 6, 11, 7, 9, 15, 13, 14}); // spokeAngles.createPermutation(new int[]{0, 15, 3, 2, 4, 9, 6, 7, 8, 5, 10, 11, 12, 13, 14, 1}); spokeAngles.createRandom(); // spokeAngles.createBack(); // spokeAngles.createSingel(); simulation = new Simulation("sim1", models, spokeAngles, true); rayResultCallback = new ClosestRayResultCallback(new Vector3(), new Vector3()); rayResultCallback.setCollisionFilterGroup(SimulationConstants.RAY_GROUP); logger.info("Setup complete"); }
private static void setCallbackRayPositions(ClosestRayResultCallback cb, Vector3 from, Vector3 to) { cb.setRayFromWorld(from); cb.setRayToWorld(to); }