Android Open Source - flingbox Contact






From Project

Back to project page flingbox.

License

The source code is released under:

GNU General Public License

If you think the Android project flingbox listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  Flingbox - An OpenSource physics sandbox for Google's Android
 *  Copyright (C) 2009  Jon Ander Pealba & Endika Gutirrez
 */*  ww w . j av  a 2 s  .c o m*/
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.eside.flingbox.physics.collisions;

import edu.eside.flingbox.math.Intersect;
import edu.eside.flingbox.math.Vector2D;
import edu.eside.flingbox.physics.PhysicBody;
import edu.eside.flingbox.utils.PositionComparator;

/**
 * Class to handle contact between two bodies.
 */
public class Contact implements PositionComparator.Positionable {
    /** Colliding body in contact, this is the body in contact */
    public final PhysicBody collidingBody;
    /** Collided body in contact, this is the weighter body in contact */
    public final PhysicBody collidedBody;

    /** Contact's absolute position */
    public final Vector2D position;
    /** Contact's sense. This is a normalized vector */
    public final Vector2D sense;
    /** Contact's normal. This is a normalized vector */
    public final Vector2D normal;

    /** Intersection description */
    private final Intersect mIntersect;

    /** Contact's relative velocity */
    private final Vector2D mRelativeVelocity = new Vector2D();

    /** When false contact is not really a collision */
    private boolean mIsCollision;

    /**
     * Default constructor
     * 
     * @param bodyA
     * @param bodyB
     * @param position
     * @param sense
     * @param intersect
     */
    public Contact(PhysicBody bodyA, PhysicBody bodyB, Vector2D position,
            Vector2D sense, Intersect intersect) {
        if (!bodyA.isFixed() || bodyA.getBodyMass() < bodyB.getBodyMass()) {
            this.collidingBody = bodyA; // A is colliding
            this.collidedBody = bodyB;
        } else {
            this.collidingBody = bodyB; // B is colliding
            this.collidedBody = bodyA;
        }
        this.position = position;
        this.sense = sense.normalize();
        this.normal = Vector2D.normalVector(sense);
        mIntersect = intersect;

        processRelativeVelocity();
    }

    public Vector2D getBodysSide(PhysicBody body) {

        return null;
    }

    public boolean concerns(PhysicBody body) {
        return (this.collidingBody == body) || (this.collidedBody == body);
    }

    public PhysicBody otherBody(PhysicBody body) {
        if (!concerns(body))
            return null;
        return (this.collidingBody == body) ? this.collidedBody
                : this.collidingBody;
    }

    @Override
    public Vector2D getPosition() {
        return this.position;
    }

    public Vector2D getRelativeVelocity() {
        return mRelativeVelocity;
    }

    public Intersect getIntersect() {
        return mIntersect;
    }

    public boolean isCollision() {
        return mIsCollision;
    }

    /**
     * Must be called by constructor to process relative velocity and if is a
     * collision
     */
    private void processRelativeVelocity() {
        final PhysicBody bodyA = this.collidingBody;
        final PhysicBody bodyB = this.collidedBody;
        final Vector2D contactPointA = new Vector2D(this.position).sub(bodyA
                .getPosition());
        final Vector2D contactPointB = new Vector2D(this.position).sub(bodyB
                .getPosition());

        final Vector2D relativeVel = new Vector2D();
        relativeVel.add(bodyB.getVelocity());
        relativeVel.sub(bodyA.getVelocity());

        final Vector2D relativePosition = new Vector2D();
        relativePosition.add(contactPointB);
        relativePosition.sub(contactPointA);

        mIsCollision = relativeVel.isAtSameSide(relativePosition);

        relativeVel.add(Vector2D.normalVector(contactPointB).mul(
                bodyB.getAngularVelocity()));
        relativeVel.sub(Vector2D.normalVector(contactPointA).mul(
                bodyA.getAngularVelocity()));

        mRelativeVelocity.set(relativeVel);
    }

}




Java Source Code List

edu.eside.flingbox.BodySettingsDialog.java
edu.eside.flingbox.FlingboxActivity.java
edu.eside.flingbox.PreferencesActivity.java
edu.eside.flingbox.Preferences.java
edu.eside.flingbox.bodies.Body.java
edu.eside.flingbox.bodies.Polygon.java
edu.eside.flingbox.graphics.RenderBody.java
edu.eside.flingbox.graphics.RenderCamera.java
edu.eside.flingbox.graphics.RenderPolygon.java
edu.eside.flingbox.graphics.SceneRenderer.java
edu.eside.flingbox.input.SceneGestureDetector.java
edu.eside.flingbox.math.Intersect.java
edu.eside.flingbox.math.Matrix22.java
edu.eside.flingbox.math.PolygonUtils.java
edu.eside.flingbox.math.Vector2D.java
edu.eside.flingbox.physics.PhysicAtomicBody.java
edu.eside.flingbox.physics.PhysicBody.java
edu.eside.flingbox.physics.PhysicPolygon.java
edu.eside.flingbox.physics.ScenePhysics.java
edu.eside.flingbox.physics.collisions.Arbiter.java
edu.eside.flingbox.physics.collisions.ColliderPolygon.java
edu.eside.flingbox.physics.collisions.Collider.java
edu.eside.flingbox.physics.collisions.ContactSolver.java
edu.eside.flingbox.physics.collisions.Contact.java
edu.eside.flingbox.physics.gravity.GravitySource.java
edu.eside.flingbox.scene.DrawingBody.java
edu.eside.flingbox.scene.DrawingPolygon.java
edu.eside.flingbox.scene.Scene.java
edu.eside.flingbox.utils.PositionComparator.java
edu.eside.flingbox.xml.InvalidXmlException.java
edu.eside.flingbox.xml.XmlExporter.java
edu.eside.flingbox.xml.XmlImporter.java