Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.replica.replicaisland;
     18 
     19 // Simple collision detection component for objects not requiring complex collision (projectiles, etc)
     20 public class SimpleCollisionComponent extends GameComponent {
     21 	private Vector2 mPreviousPosition;
     22 	private Vector2 mCurrentPosition;
     23 	private Vector2 mMovementDirection;
     24 	private Vector2 mHitPoint;
     25 	private Vector2 mHitNormal;
     26 
     27 	public SimpleCollisionComponent() {
     28 		super();
     29 		setPhase(ComponentPhases.COLLISION_DETECTION.ordinal());
     30 		mPreviousPosition = new Vector2();
     31 		mCurrentPosition = new Vector2();
     32 		mMovementDirection = new Vector2();
     33 		mHitPoint = new Vector2();
     34 		mHitNormal = new Vector2();
     35 	}
     36 
     37 	@Override
     38 	public void reset() {
     39 		mPreviousPosition.zero();
     40 		mCurrentPosition.zero();
     41 		mMovementDirection.zero();
     42 		mHitPoint.zero();
     43 		mHitNormal.zero();
     44 	}
     45 
     46 	@Override
     47     public void update(float timeDelta, BaseObject parent) {
     48         GameObject parentObject = (GameObject) parent;
     49 
     50         if (mPreviousPosition.length2() > 0.0f) {
     51         	mCurrentPosition.set(parentObject.getCenteredPositionX(), parentObject.getCenteredPositionY());
     52         	mMovementDirection.set(mCurrentPosition);
     53         	mMovementDirection.subtract(mPreviousPosition);
     54         	if (mMovementDirection.length2() > 0.0f) {
     55         		final CollisionSystem collision = sSystemRegistry.collisionSystem;
     56         		if (collision != null) {
     57         			final boolean hit = collision.castRay(mPreviousPosition, mCurrentPosition,
     58         					mMovementDirection, mHitPoint, mHitNormal, parentObject);
     59 
     60         			if (hit) {
     61         				// snap
     62         				final float halfWidth = parentObject.width / 2.0f;
     63         				final float halfHeight = parentObject.height / 2.0f;
     64         				if (!Utils.close(mHitNormal.x, 0.0f)) {
     65         					parentObject.getPosition().x = mHitPoint.x - halfWidth;
     66         				}
     67 
     68         				if (!Utils.close(mHitNormal.y, 0.0f)) {
     69         					parentObject.getPosition().y = mHitPoint.y - halfHeight;
     70         				}
     71 
     72         				final TimeSystem timeSystem = sSystemRegistry.timeSystem;
     73 
     74     	                if (timeSystem != null) {
     75     	                    float time = timeSystem.getGameTime();
     76     	                   if (mHitNormal.x > 0.0f) {
     77 	                            parentObject.setLastTouchedLeftWallTime(time);
     78 	                        } else if (mHitNormal.x < 0.0) {
     79 	                            parentObject.setLastTouchedRightWallTime(time);
     80 	                        }
     81 
     82 	                        if (mHitNormal.y > 0.0f) {
     83 	                            parentObject.setLastTouchedFloorTime(time);
     84 	                        } else if (mHitNormal.y < 0.0f) {
     85 	                            parentObject.setLastTouchedCeilingTime(time);
     86 	                        }
     87     	                }
     88 
     89     	                parentObject.setBackgroundCollisionNormal(mHitNormal);
     90 
     91         			}
     92         		}
     93         	}
     94         }
     95 
     96         mPreviousPosition.set(parentObject.getCenteredPositionX(), parentObject.getCenteredPositionY());
     97 	}
     98 }
     99