private void checkCollisionSound(Contact contact, Body bodyA, Body bodyB) { WorldManifold manifold = contact.getWorldManifold(); Vector2 contactPoint = manifold.getPoints()[0]; Vector2 vel1 = bodyA.getLinearVelocityFromWorldPoint(contactPoint); Vector2 vel2 = bodyB.getLinearVelocityFromWorldPoint(contactPoint); Vector2 impactVelocity = vel1.sub(vel2); float impactLen = impactVelocity.len(); if (impactLen > MIN_BALL_SOUND_SPEED) { float dot = Math.abs(impactVelocity.dot(manifold.getNormal())); if (dot > MIN_IMPACT_SOUND_SPEED) { float volume = (float) Math.min((dot - MIN_IMPACT_SOUND_SPEED) / MAX_IMPACT_SOUND_SPEED, 1.0); SoundManager.getInstance().play(R.raw.bounce_1, volume); } } }
private boolean isPlayerGrounded (float deltaTime) { groundedPlatform = null; Array<Contact> contactList = world.getContactList(); for (int i = 0; i < contactList.size; i++) { Contact contact = contactList.get(i); if (contact.isTouching() && (contact.getFixtureA() == playerSensorFixture || contact.getFixtureB() == playerSensorFixture)) { Vector2 pos = player.getPosition(); WorldManifold manifold = contact.getWorldManifold(); boolean below = true; for (int j = 0; j < manifold.getNumberOfContactPoints(); j++) { below &= (manifold.getPoints()[j].y < pos.y - 1.5f); } if (below) { if (contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) { groundedPlatform = (Platform)contact.getFixtureA().getBody().getUserData(); } if (contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) { groundedPlatform = (Platform)contact.getFixtureB().getBody().getUserData(); } return true; } return false; } } return false; }
/** * Draw contact points. * @param renderer The shape renderer. * @param contact The contact. */ protected void drawContact(Graphics renderer, Contact contact) { WorldManifold worldManifold = contact.getWorldManifold(); if(worldManifold.getNumberOfContactPoints() == 0) return; Vector2 point = worldManifold.getPoints()[0]; point.x -= FlxG.camera.scroll.x / B2FlxB.RATIO; point.y -= FlxG.camera.scroll.y / B2FlxB.RATIO; // TODO: BUG: invisible shape renderer when use circle // renderer.drawCircle(point.x * B2FlxB.RATIO, point.y * B2FlxB.RATIO, 1f); renderer.drawRect(point.x * B2FlxB.RATIO, point.y * B2FlxB.RATIO, 1f, 1f); }
public WorldManifold getWorldManifold() { return contact.getWorldManifold(); }
@Override public void render () { // first we update the world. For simplicity // we use the delta time provided by the Graphics // instance. Normally you'll want to fix the time // step. long start = TimeUtils.nanoTime(); world.step(Gdx.graphics.getDeltaTime(), 8, 3); float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f; // next we clear the color buffer and set the camera // matrices Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); // next we render the ground body renderBox(groundBody, 50, 1); // next we render each box via the SpriteBatch. // for this we have to set the projection matrix of the // spritebatch to the camera's combined matrix. This will // make the spritebatch work in world coordinates batch.getProjectionMatrix().set(camera.combined); batch.begin(); for (int i = 0; i < boxes.size(); i++) { Body box = boxes.get(i); Vector2 position = box.getPosition(); // that's the box's center position float angle = MathUtils.radiansToDegrees * box.getAngle(); // the rotation angle around the center batch.draw(textureRegion, position.x - 1, position.y - 1, // the bottom left corner of the box, unrotated 1f, 1f, // the rotation center relative to the bottom left corner of the box 2, 2, // the width and height of the box 1, 1, // the scale on the x- and y-axis angle); // the rotation angle } batch.end(); // next we use the debug renderer. Note that we // simply apply the camera again and then call // the renderer. the camera.apply() call is actually // not needed as the opengl matrices are already set // by the spritebatch which in turn uses the camera matrices :) debugRenderer.render(world, camera.combined); // finally we render all contact points renderer.setProjectionMatrix(camera.combined); renderer.begin(ShapeType.Point); renderer.setColor(0, 1, 0, 1); for (int i = 0; i < world.getContactCount(); i++) { Contact contact = world.getContactList().get(i); // we only render the contact if it actually touches if (contact.isTouching()) { // get the world manifold from which we get the // contact points. A manifold can have 0, 1 or 2 // contact points. WorldManifold manifold = contact.getWorldManifold(); int numContactPoints = manifold.getNumberOfContactPoints(); for (int j = 0; j < numContactPoints; j++) { Vector2 point = manifold.getPoints()[j]; renderer.point(point.x, point.y, 0); } } } renderer.end(); // finally we render the time it took to update the world // for this we have to set the projection matrix again, so // we work in pixel coordinates batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); batch.begin(); font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, 20); batch.end(); }
@Override public void handlePreSolve(Contact contact, Manifold oldManifold) { Box2DPhysicsObject passenger = getPassenger(contact); MovingPlatform movingPlatform = getMovingPlatform(contact); if (contact.isEnabled()) { if (passenger.getGrupo().equals(GRUPO.HERO) && !((Hero) passenger).getState().equals(StateHero.WALKING)) { contact.setFriction(100f); } else if (passenger.getGrupo().equals(GRUPO.HERO) && !((Hero) passenger).getState().equals(StateHero.WALKING)) { contact.setFriction(0); } } WorldManifold manifold = contact.getWorldManifold(); for (Vector2 point : manifold.getPoints()) { Vector2 pointVelPlatform = movingPlatform.getBodyA().getLinearVelocityFromWorldPoint(point); Vector2 pointVelOther = passenger.getBodyA().getLinearVelocityFromWorldPoint(point); Vector2 relativeVel = movingPlatform.getBodyA().getLocalVector(pointVelOther.sub(pointVelPlatform)); if (relativeVel.y < -1) { movingPlatform.getPassengers().add(passenger); movingPlatform.enabled = true; return; } else if (relativeVel.y < 1) { Vector2 relativePoint = movingPlatform.getBodyA().getLocalPoint(point); float platformFaceY = 0.5f; if (relativePoint.y > platformFaceY - 0.05) { if (contact.getFixtureA().equals(((Hero) passenger).getHeroPhysicsFixture())) System.out.println(" relativeVel < 1 " + relativeVel); movingPlatform.getPassengers().add(passenger); movingPlatform.enabled = true; Vector2 force = contact.getWorldManifold().getNormal(); if (Math.abs(force.x) == 1f && force.y == 0f) { if (contact.getFixtureA().equals(((Hero) passenger).getHeroPhysicsFixture())) { force.scl(-6); } else if (contact.getFixtureB().equals(((Hero) passenger).getHeroPhysicsFixture())) { force.scl(6); } passenger.getBodyA().applyLinearImpulse(force, passenger.getBodyA().getWorldCenter(), true); System.out.println("Fuerza colision Platform: " + force); } return; } } } movingPlatform.enabled = false; contact.setEnabled(movingPlatform.enabled); }