public static EntityPos LookAtTarget(this EntityPos agentPos, Vec3d targetPos) { var cartesianCoordinates = targetPos.SubCopy(agentPos.XYZ).Normalize(); var yaw = GameMath.TWOPI - (float)Math.Atan2(cartesianCoordinates.Z, cartesianCoordinates.X); var pitch = (float)Math.Asin(cartesianCoordinates.Y); var entityPos = agentPos.Copy(); entityPos.Yaw = yaw % GameMath.TWOPI; entityPos.Pitch = GameMath.PI - pitch; return(entityPos); }
/// <summary> /// If an attachment point called "Center" exists, then this method /// offsets the creatures collision box so that the Center attachment point is the center of the collision box. /// </summary> public void AdjustCollisionBoxToAnimation(float dtFac) { float[] hitboxOff = new float[4] { 0, 0, 0, 1 }; AttachmentPointAndPose apap = entity.AnimManager.Animator.GetAttachmentPointPose("Center"); if (apap == null) { return; } AttachmentPoint ap = apap.AttachPoint; float rotX = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateX : 0; float rotY = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateY : 0; float rotZ = entity.Properties.Client.Shape != null ? entity.Properties.Client.Shape.rotateZ : 0; float[] ModelMat = Mat4f.Create(); Mat4f.Identity(ModelMat); Mat4f.Translate(ModelMat, ModelMat, 0, entity.CollisionBox.Y2 / 2, 0); double[] quat = Quaterniond.Create(); Quaterniond.RotateX(quat, quat, entity.Pos.Pitch + rotX * GameMath.DEG2RAD); Quaterniond.RotateY(quat, quat, entity.Pos.Yaw + (rotY + 90) * GameMath.DEG2RAD); Quaterniond.RotateZ(quat, quat, entity.Pos.Roll + rotZ * GameMath.DEG2RAD); float[] qf = new float[quat.Length]; for (int k = 0; k < quat.Length; k++) { qf[k] = (float)quat[k]; } Mat4f.Mul(ModelMat, ModelMat, Mat4f.FromQuat(Mat4f.Create(), qf)); float scale = entity.Properties.Client.Size; Mat4f.Translate(ModelMat, ModelMat, 0, -entity.CollisionBox.Y2 / 2, 0f); Mat4f.Scale(ModelMat, ModelMat, new float[] { scale, scale, scale }); Mat4f.Translate(ModelMat, ModelMat, -0.5f, 0, -0.5f); tmpModelMat .Set(ModelMat) .Mul(apap.AnimModelMatrix) .Translate(ap.PosX / 16f, ap.PosY / 16f, ap.PosZ / 16f) ; EntityPos epos = entity.SidedPos; float[] endVec = Mat4f.MulWithVec4(tmpModelMat.Values, hitboxOff); float motionX = endVec[0] - (entity.CollisionBox.X1 - entity.OriginCollisionBox.X1); float motionZ = endVec[2] - (entity.CollisionBox.Z1 - entity.OriginCollisionBox.Z1); if (Math.Abs(motionX) > 0.00001 || Math.Abs(motionZ) > 0.00001) { EntityPos posMoved = epos.Copy(); posMoved.Motion.X = motionX; posMoved.Motion.Z = motionZ; moveDelta.Set(posMoved.Motion.X, posMoved.Motion.Y, posMoved.Motion.Z); collisionTester.ApplyTerrainCollision(entity, posMoved, dtFac, ref outposition); double reflectX = outposition.X - epos.X - motionX; double reflectZ = outposition.Z - epos.Z - motionZ; epos.Motion.X = reflectX; epos.Motion.Z = reflectZ; entity.CollisionBox.Set(entity.OriginCollisionBox); entity.CollisionBox.Translate(endVec[0], 0, endVec[2]); } //Console.WriteLine("{0}/{1}", reflectX, reflectZ); }