summaryrefslogtreecommitdiff
path: root/src/com/replica/replicaisland/PhysicsComponent.java
blob: 958283a6bc59941b6fa61190db10a692324d99c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * 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.replica.replicaisland;

/**
 * Component that adds physics to its parent game object.  This component implements force
 * calculation based on mass, impulses, friction, and collisions.
 */
public class PhysicsComponent extends GameComponent {

    private float mMass;
    private float mBounciness; // 1.0 = super bouncy, 0.0 = zero bounce
    private float mInertia;
    private float mStaticFrictionCoeffecient;
    private float mDynamicFrictionCoeffecient;

    private static final float DEFAULT_MASS = 1.0f;
    private static final float DEFAULT_BOUNCINESS = 0.1f;
    private static final float DEFAULT_INERTIA = 0.01f;
    private static final float DEFAULT_STATIC_FRICTION_COEFFECIENT = 0.05f;
    private static final float DEFAULT_DYNAMIC_FRICTION_COEFFECIENT = 0.02f;
    
    PhysicsComponent() {
        super();
        reset();
        setPhase(ComponentPhases.POST_PHYSICS.ordinal());
    }
    
    @Override
    public void reset() {
        // TODO: no reason to call accessors here locally.
        setMass(DEFAULT_MASS);
        setBounciness(DEFAULT_BOUNCINESS);
        setInertia(DEFAULT_INERTIA);
        setStaticFrictionCoeffecient(DEFAULT_STATIC_FRICTION_COEFFECIENT);
        setDynamicFrictionCoeffecient(DEFAULT_DYNAMIC_FRICTION_COEFFECIENT);
    }

    @Override
    public void update(float timeDelta, BaseObject parent) {
        GameObject parentObject = (GameObject) parent;

        // we look to user data so that other code can provide impulses
        Vector2 impulseVector = parentObject.getImpulse();

        final Vector2 currentVelocity = parentObject.getVelocity();
        
        final Vector2 surfaceNormal = parentObject.getBackgroundCollisionNormal();
        if (surfaceNormal.length2() > 0.0f) {
            resolveCollision(currentVelocity, impulseVector, surfaceNormal, impulseVector);
        }

        VectorPool vectorPool = sSystemRegistry.vectorPool;

        // if our speed is below inertia, we need to overcome inertia before we can move.

        boolean physicsCausesMovement = true;

        final float inertiaSquared = getInertia() * getInertia();

        Vector2 newVelocity = vectorPool.allocate(currentVelocity);
        newVelocity.add(impulseVector);

        if (newVelocity.length2() < inertiaSquared) {
            physicsCausesMovement = false;
        }

        final boolean touchingFloor = parentObject.touchingGround();

        GravityComponent gravity = parentObject.findByClass(GravityComponent.class);

        if (touchingFloor && currentVelocity.y <= 0.0f && Math.abs(newVelocity.x) > 0.0f
                        && gravity != null) {
            final Vector2 gravityVector = gravity.getGravity();

            // if we were moving last frame, we'll use dynamic friction. Else
            // static.
            float frictionCoeffecient = Math.abs(currentVelocity.x) > 0.0f ? 
                        getDynamicFrictionCoeffecient() : getStaticFrictionCoeffecient();
            frictionCoeffecient *= timeDelta;

            // Friction = cofN, where cof = friction coefficient and N = force
            // perpendicular to the ground.
            final float maxFriction = Math.abs(gravityVector.y) * getMass()
                    * frictionCoeffecient;

            if (maxFriction > Math.abs(newVelocity.x)) {
                newVelocity.x = (0.0f);
            } else {
                newVelocity.x = (newVelocity.x
                        - (maxFriction * Utils.sign(newVelocity.x)));
            }
        }

        if (Math.abs(newVelocity.x) < 0.01f) {
            newVelocity.x = (0.0f);
        }

        if (Math.abs(newVelocity.y) < 0.01f) {
            newVelocity.y = (0.0f);
        }

        // physics-based movements means constant acceleration, always. set the target to the
        // velocity.
        if (physicsCausesMovement) {
            parentObject.setVelocity(newVelocity);
            parentObject.setTargetVelocity(newVelocity);
            parentObject.setAcceleration(Vector2.ZERO);
            parentObject.setImpulse(Vector2.ZERO);
        }

        vectorPool.release(newVelocity);
    }

    protected void resolveCollision(Vector2 velocity, Vector2 impulse, Vector2 opposingNormal,
                    Vector2 outputImpulse) {
        VectorPool vectorPool = sSystemRegistry.vectorPool;

        outputImpulse.set(impulse);

        Vector2 collisionNormal = vectorPool.allocate(opposingNormal);

        collisionNormal.normalize();

        Vector2 relativeVelocity = vectorPool.allocate(velocity);
        relativeVelocity.add(impulse);

        final float dotRelativeAndNormal = relativeVelocity.dot(collisionNormal);

        // make sure the motion of the entity requires resolution
        if (dotRelativeAndNormal < 0.0f) {
            final float coefficientOfRestitution = getBounciness(); // 0 = perfectly inelastic,
                                                                    // 1 = perfectly elastic

            // calculate an impulse to apply to the entity
            float j = (-(1 + coefficientOfRestitution) * dotRelativeAndNormal);

            j /= ((collisionNormal.dot(collisionNormal)) * (1 / getMass()));

            Vector2 entity1Adjust = vectorPool.allocate(collisionNormal);

            entity1Adjust.set(collisionNormal);
            entity1Adjust.multiply(j);
            entity1Adjust.divide(getMass());
            entity1Adjust.add(impulse);
            outputImpulse.set(entity1Adjust);
            vectorPool.release(entity1Adjust);

        }

        vectorPool.release(collisionNormal);
        vectorPool.release(relativeVelocity);
    }

    protected void resolveCollision(Vector2 velocity, Vector2 impulse, Vector2 opposingNormal,
                    float otherMass, Vector2 otherVelocity, Vector2 otherImpulse,
                    float otherBounciness, Vector2 outputImpulse) {
        VectorPool vectorPool = sSystemRegistry.vectorPool;

        Vector2 collisionNormal = vectorPool.allocate(opposingNormal);
        collisionNormal.normalize();

        Vector2 entity1Velocity = vectorPool.allocate(velocity);
        entity1Velocity.add(impulse);

        Vector2 entity2Velocity = vectorPool.allocate(otherVelocity);
        entity2Velocity.add(otherImpulse);

        Vector2 relativeVelocity = vectorPool.allocate(entity1Velocity);
        relativeVelocity.subtract(entity2Velocity);

        final float dotRelativeAndNormal = relativeVelocity.dot(collisionNormal);

        // make sure the entities' motion requires resolution
        if (dotRelativeAndNormal < 0.0f) {
            final float bounciness = Math.min(getBounciness() + otherBounciness, 1.0f);
            final float coefficientOfRestitution = bounciness;  // 0 = perfectly inelastic,
                                                                // 1 = perfectly elastic
            
            // calculate an impulse to apply to both entities
            float j = (-(1 + coefficientOfRestitution) * dotRelativeAndNormal);

            j /= ((collisionNormal.dot(collisionNormal)) * (1 / getMass() + 1 / otherMass));

            Vector2 entity1Adjust = vectorPool.allocate(collisionNormal);
            entity1Adjust.multiply(j);
            entity1Adjust.divide(getMass());
            entity1Adjust.add(impulse);

            outputImpulse.set(entity1Adjust);

            // TODO: Deal impulses both ways.
            /*
             * Vector3 entity2Adjust = (collisionNormal j); 
             * entity2Adjust[0] /= otherMass;
             * entity2Adjust[1] /= otherMass; 
             * entity2Adjust[2] /= otherMass;
             * 
             * const Vector3 newEntity2Impulse = otherImpulse + entity2Adjust;
             */

            vectorPool.release(entity1Adjust);
        }

        vectorPool.release(collisionNormal);
        vectorPool.release(entity1Velocity);
        vectorPool.release(entity2Velocity);
        vectorPool.release(relativeVelocity);
    }

    public float getMass() {
        return mMass;
    }

    public void setMass(float mass) {
        mMass = mass;
    }

    public float getBounciness() {
        return mBounciness;
    }

    public void setBounciness(float bounciness) {
        mBounciness = bounciness;
    }

    public float getInertia() {
        return mInertia;
    }

    public void setInertia(float inertia) {
        mInertia = inertia;
    }

    public float getStaticFrictionCoeffecient() {
        return mStaticFrictionCoeffecient;
    }

    public void setStaticFrictionCoeffecient(float staticFrictionCoeffecient) {
        mStaticFrictionCoeffecient = staticFrictionCoeffecient;
    }

    public float getDynamicFrictionCoeffecient() {
        return mDynamicFrictionCoeffecient;
    }

    public void setDynamicFrictionCoeffecient(float dynamicFrictionCoeffecient) {
        mDynamicFrictionCoeffecient = dynamicFrictionCoeffecient;
    }

}