Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2010 22:14
Show Gist options
  • Save anonymous/478997 to your computer and use it in GitHub Desktop.
Save anonymous/478997 to your computer and use it in GitHub Desktop.
var hitArray:Array = new Array();
//we FIRST move the player, then move the player BACKWARD if there's a collision.
player.sprite.x += player.xVelocity;
player.sprite.y += player.yVelocity;
player.sprite.z += player.zVelocity;
// Here, I do a 3D hit test to see the player is touching any of the objects in the scene.
// Those objects are then tested later to see WHERE they touch.
// I don't know if this is the best way to do it.
// The test is done with 3 midpoint collision checks (hitTest3D)
for each( var o:Object in _levelData ) {
if (Math3D.hitTest3D(player.sprite, o.data)) {
hitArray.push(o);
//trace("collision with box");
}
}
// If nothing is in the hit array, there is no collision; activate jump state.
if (hitArray.length == 0) player.JUMPING = true;
for (var i:int = 0; i < hitArray.length; i++) {
/* if (player.xVelocity > 0) {
if (player.sprite.xMax + player.xVelocity > hitArray[i].data.xMin)
player.xVelocity += (hitArray[i].data.xMin - player.sprite.xMax)
} else {
if (player.sprite.xMin + player.xVelocity < hitArray[i].data.xMax)
player.xVelocity += (hitArray[i].data.xMax - player.sprite.xMin)
}
if (player.yVelocity > 0) {
if (player.sprite.yMax + player.yVelocity > hitArray[i].data.yMin)
player.yVelocity += (hitArray[i].data.yMin - player.sprite.yMax)
} else {
if (player.sprite.yMin + player.yVelocity < hitArray[i].data.yMax)
player.yVelocity += (hitArray[i].data.yMax - player.sprite.yMin)
} */
if (player.zVelocity > 0) {
if (player.sprite.zMax > hitArray[i].data.zMin)
player.zVelocity = (hitArray[i].data.zMin - player.sprite.zMax)
} else {
if (player.sprite.zMin <= hitArray[i].data.zMax ) {
player.zVelocity = 0;
player.sprite.z = hitArray[i].data.zMax + (player.sprite.height>> 1);
//player.zVelocity = (hitArray[i].data.zMax - player.sprite.zMin)
player.JUMPING = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment