public override void Update() { currentPick = PerformPicking(); if (PickingInsideSphere()) { if (dragging) { TGCVector3 normalizedTo = currentPick; normalizedTo.Normalize(); var cross = TGCVector3.Cross(normalizedFrom, normalizedTo); var newRotation = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(normalizedFrom, normalizedTo))); var currentRotation = TGCQuaternion.Multiply(stacked, newRotation); shark.Transform = baseSharkTransform * TGCMatrix.RotationTGCQuaternion(currentRotation); if (Input.buttonUp(TgcD3dInput.MouseButtons.BUTTON_LEFT)) { stacked = currentRotation; shark.Transform = baseSharkTransform * TGCMatrix.RotationTGCQuaternion(stacked); dragging = false; } } else if (Input.buttonPressed(TgcD3dInput.MouseButtons.BUTTON_LEFT)) { dragging = true; fromDrag = currentPick; normalizedFrom = TGCVector3.Normalize(fromDrag); } } UpdateArrows(); }
/// <summary> /// Convierte un TGCQuaternion a rotación de Euler /// </summary> private TGCVector3 quaternionToEuler(TGCQuaternion quat) { //TODO revisar que esta conversion a Euler ande bien var mat = TGCMatrix.RotationTGCQuaternion(quat); var matrixGetRotation = TGCVector3.Empty; //gets the x axis rotation from the matrix matrixGetRotation.X = (float)Math.Asin(mat.M32); var cosX = (float)Math.Cos(matrixGetRotation.X); //checks for gimbal lock if (cosX < 0.005) { matrixGetRotation.Z = 0; matrixGetRotation.Y = Math.Sign(-mat.M21) * (float)Math.Acos(mat.M11); } //normal calculation else { matrixGetRotation.Z = Math.Sign(mat.M12) * (float)Math.Acos(mat.M22 / cosX); matrixGetRotation.Y = Math.Sign(mat.M31) * (float)Math.Acos(mat.M33 / cosX); //converts the rotations because the x axis rotation can't be bigger than 90 and -90 if (Math.Sign(mat.M22) == -1 && matrixGetRotation.Z == 0) { var pi = (float)Math.PI; matrixGetRotation.Z += pi; matrixGetRotation.Y += pi; } } return(matrixGetRotation); }
public void Update(float elapsedTime) { TGCQuaternion rotationX = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 1.0f, 0.0f), Geometry.DegreeToRadian(90f /* + anguloEntreVectores*15*/)); TGCVector3 PosicionA = posicionInicial; TGCVector3 PosicionB = jugador.GetPosicion(); TGCVector3 DireccionA = new TGCVector3(0, 0, -1); TGCVector3 DireccionB = PosicionB - PosicionA; if (DireccionB.Length() >= 15f && PosicionA.Z > PosicionB.Z + 10f) { DireccionB.Normalize(); // anguloEntreVectores = (float)Math.Acos(TGCVector3.Dot(DireccionA, DireccionB)); var cross = TGCVector3.Cross(DireccionA, DireccionB); var newRotation = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(DireccionA, DireccionB))); quaternionAuxiliar = rotationX * newRotation; mainMesh.Transform = baseScaleRotation * TGCMatrix.RotationTGCQuaternion(rotationX * newRotation) * baseQuaternionTranslation; } else { mainMesh.Transform = baseScaleRotation * TGCMatrix.RotationTGCQuaternion(quaternionAuxiliar) * baseQuaternionTranslation; } //codigo de prueba------ tiempo += .1f + elapsedTime; if (tiempo > 15f) { Disparar(PosicionB); tiempo = 0f; } //-------- }
private TGCQuaternion QuaternionDireccion(TGCVector3 direccionDisparoNormalizado) { TGCVector3 DireccionA = new TGCVector3(0, 0, -1); TGCVector3 cross = TGCVector3.Cross(DireccionA, direccionDisparoNormalizado); TGCQuaternion newRotation = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(DireccionA, direccionDisparoNormalizado))); return(newRotation); }
public TgcSkeletalBone(int index, string name, TGCVector3 startPosition, TGCQuaternion startRotation) { Index = index; Name = name; StartPosition = startPosition; StartRotation = startRotation; MatLocal = TGCMatrix.RotationTGCQuaternion(StartRotation) * TGCMatrix.Translation(StartPosition); }
/// <summary> /// Devuelve la rotacion que se debe aplicar a la matriz de transformacion para que la entidad apunte hacia una direccion. /// </summary> /// <param name="lookDir">Vector normalizado que define la direccion a la que debe mirar la entidad.</param> private void LookAt(TGCVector3 lookDir) { float angle = FastMath.Acos(TGCVector3.Dot(defaultLookDir, lookDir)); TGCVector3 rotVector = TGCVector3.Cross(defaultLookDir, lookDir); rotVector.Z = 0; rotation = TGCQuaternion.RotationAxis(rotVector, angle); }
public void giro_en_plano() { timer += ElapsedTime; var posicion = new TGCVector3(radio * FastMath.Sin(timer), 0f, radio * FastMath.Cos(timer)); var posicionNormalizada = posicion; posicionNormalizada.Normalize(); var quaternion = TGCQuaternion.RotationAxis(posicionNormalizada, -timer * 5f); sphereOne.Transform = TGCMatrix.Scaling(spheresScale) * TGCMatrix.RotationTGCQuaternion(quaternion) * TGCMatrix.Translation(posicion + new TGCVector3(0f, 4f, 0f)); }
/// <summary> /// Crear una malla instancia de una original /// </summary> private TgcMesh crearMeshInstance(TgcMeshData meshData, List <TgcMesh> meshes) { var originalMesh = meshes[meshData.originalMesh]; var translation = new TGCVector3(meshData.position[0], meshData.position[1], meshData.position[2]); var rotationQuat = new TGCQuaternion(meshData.rotation[0], meshData.rotation[1], meshData.rotation[2], meshData.rotation[3]); var rotation = quaternionToEuler(rotationQuat); var scale = new TGCVector3(meshData.scale[0], meshData.scale[1], meshData.scale[2]); var tgcMesh = new TgcMesh(meshData.name, originalMesh, translation, rotation, scale); return(tgcMesh); }
/// <summary> /// Actualiza la posicion de cada hueso del esqueleto segun sus KeyFrames de la animacion /// </summary> protected void updateSkeleton() { for (var i = 0; i < bones.Length; i++) { var bone = bones[i]; //Tomar el frame actual para este hueso var boneFrames = currentAnimation.BoneFrames[i]; //Solo hay un frame, no hacer nada, ya se hizo en el Init de la animacion if (boneFrames.Count == 1) { continue; } //Obtener cuadro actual segun el tiempo transcurrido var currentFrameF = currentTime * frameRate; //Ve a que KeyFrame le corresponde var keyFrameIdx = getCurrentFrameBone(boneFrames, currentFrameF); currentFrame = keyFrameIdx; //Armar un intervalo entre el proximo KeyFrame y el anterior var frame1 = boneFrames[keyFrameIdx - 1]; var frame2 = boneFrames[keyFrameIdx]; //Calcular la cantidad que hay interpolar en base al la diferencia entre cuadros float framesDiff = frame2.Frame - frame1.Frame; var interpolationValue = (currentFrameF - frame1.Frame) / framesDiff; //Interpolar traslacion var frameTranslation = (frame2.Position - frame1.Position) * interpolationValue + frame1.Position; //Interpolar rotacion con SLERP var quatFrameRotation = TGCQuaternion.Slerp(frame1.Rotation, frame2.Rotation, interpolationValue); //Unir ambas transformaciones de este frame var frameMatrix = TGCMatrix.RotationTGCQuaternion(quatFrameRotation) * TGCMatrix.Translation(frameTranslation); //Multiplicar por la matriz del padre, si tiene if (bone.ParentBone != null) { bone.MatFinal = frameMatrix * bone.ParentBone.MatFinal; } else { bone.MatFinal = frameMatrix; } } }
public override void Init() { stacked = TGCQuaternion.Identity; fromDrag = TGCVector3.Empty; dragging = false; currentPick = TGCVector3.Empty; InitializeSphere(); InitializeShark(); InitializeArrows(); pickingRay = new TgcPickingRay(Input); Camera.SetCamera(new TGCVector3(20f, 0f, 20f), new TGCVector3(-1f, 0f, -1f)); }
public void CambiarRotacion(TGCVector3 nuevaRotacion) { TGCQuaternion rotationX = TGCQuaternion.RotationAxis(new TGCVector3(1.0f, 0.0f, 0.0f), nuevaRotacion.X); TGCQuaternion rotationY = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 1.0f, 0.0f), nuevaRotacion.Y); TGCQuaternion rotationZ = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 0.0f, 1.0f), nuevaRotacion.Z); TGCQuaternion rotation = rotationX * rotationY * rotationZ; baseScaleRotation = TGCMatrix.Scaling(new TGCVector3(0.15f, 0.15f, 0.15f)) * TGCMatrix.RotationY(FastMath.PI_HALF); TGCMatrix.RotationTGCQuaternion(rotation); //TransformarModelo(delegate (TgcMesh unMesh) { unMesh.Rotation = nuevaRotacion; }); //TransformarModelo(delegate (TgcMesh unMesh) { unMesh.Transform = TGCMatrix.RotationTGCQuaternion(rotation); }); rotacionMesh = TGCMatrix.RotationTGCQuaternion(rotation); }
/// <summary> /// Asigna target con offsets. /// </summary> public void setTargetOffset(TGCVector3 target, float offsetY, float offsetZ) { Eye = new TGCVector3(target.X, target.Y + offsetY, target.Z + offsetZ); Target = target; m_viewMatrix = TGCMatrix.LookAtLH(Eye, Target, m_targetYAxis); m_orientation = TGCQuaternion.RotationMatrix(m_viewMatrix); var offset = Target - Eye; m_offsetDistance = offset.Length(); m_headingDegrees = 0.0f; m_pitchDegrees = 0.0f; }
private void Chase() { LookDirection = TGCVector3.Normalize(GameInstance.Player.Position - Position); TGCVector3 rotationAxis = TGCVector3.Cross(InitialLookDirection, LookDirection); // Ojo el orden - no es conmutativo TGCQuaternion rotationQuat = TGCQuaternion.RotationAxis(rotationAxis, MathExtended.AngleBetween(InitialLookDirection, LookDirection)); TGCVector3 nextPosition = Position + LookDirection * chasingSpeed * GameInstance.ElapsedTime; TGCMatrix translationMatrix = TGCMatrix.Translation(nextPosition); TGCMatrix rotationMatrix = TGCMatrix.RotationTGCQuaternion(rotationQuat); TGCMatrix nextTransform = rotationMatrix * translationMatrix; SimulateAndSetTransformation(nextPosition, nextTransform); }
public override void Update(float elapsedTime) { TGCQuaternion rotation = TGCQuaternion.RotationAxis(new TGCVector3(1.0f, 0.0f, 0.0f), Geometry.DegreeToRadian(90f)); TGCVector3 direccionDisparo = direccion; direccionDisparo.Normalize(); TGCQuaternion giro = QuaternionDireccion(direccionDisparo); TGCVector3 movement = direccionDisparo * 60f * elapsedTime; mainMesh.Position += movement; TGCMatrix matrizTransformacion = baseScaleRotation * TGCMatrix.RotationTGCQuaternion(rotation * giro) * TGCMatrix.Translation(mainMesh.Position); mainMesh.Transform = matrizTransformacion; mainMesh.BoundingBox.transform(matrizTransformacion); }
private void UpdateRotation(float ElapsedTime) { cam_angles += new TGCVector2(Input.YposRelative, Input.XposRelative) * sensitivity * ElapsedTime; cam_angles.X = FastMath.Clamp(cam_angles.X, -CAMERA_MAX_X_ANGLE, CAMERA_MAX_X_ANGLE); cam_angles.Y = cam_angles.Y > 2 * FastMath.PI || cam_angles.Y < -2 * FastMath.PI ? 0 : cam_angles.Y; TGCQuaternion rotationY = TGCQuaternion.RotationAxis(new TGCVector3(0f, 1f, 0f), cam_angles.Y); TGCQuaternion rotationX = TGCQuaternion.RotationAxis(new TGCVector3(1f, 0f, 0f), -cam_angles.X); rotation = rotationX * rotationY; var init_offset = new TGCVector3(0f, 0f, 1f); TGCMatrix camera_m = TGCMatrix.Translation(init_offset) * TGCMatrix.RotationTGCQuaternion(rotation) * TGCMatrix.Translation(Player.Position()); TGCVector3 pos = new TGCVector3(camera_m.M41, camera_m.M42, camera_m.M43); Camara.SetCamera(pos, Player.Position()); }
public TGCVector3 calulateNextPosition(float headingDegrees, float elapsedTimeSec) { var heading = FastMath.ToRad(-headingDegrees * elapsedTimeSec); var quatOrientation = m_orientation; if (heading != 0.0f) { var rot = TGCQuaternion.RotationAxis(m_targetYAxis, heading); quatOrientation = TGCQuaternion.Multiply(rot, quatOrientation); } quatOrientation.Normalize(); var viewMatrix = TGCMatrix.RotationTGCQuaternion(quatOrientation); var m_zAxis = new TGCVector3(viewMatrix.M13, viewMatrix.M23, viewMatrix.M33); var idealPosition = Target + m_zAxis * -m_offsetDistance; return(idealPosition); }
public Arco(TgcMesh mesh, float meshRotationAngle) : base(mesh) { this.meshRotationAngle = meshRotationAngle; cuerpo = BulletRigidBodyFactory.Instance.CreateRigidBodyFromTgcMesh(mesh); TGCQuaternion rot = new TGCQuaternion(); rot.RotateAxis(new TGCVector3(0, 1, 0), meshRotationAngle); cuerpo.WorldTransform = TGCMatrix.RotationTGCQuaternion(rot).ToBulletMatrix(); UpdateAABB(); AABBGol = new TgcBoundingAxisAlignBox(AABB.PMin + new TGCVector3(10, 10, 10), AABB.PMax - new TGCVector3(10, 10, 10)); AABBGol.transform(Mesh.Transform); Ka = 0.4f; Kd = 0.55f; Ks = 0.05f; shininess = 0; reflection = 0; }
/// <summary> /// Carga los valores default de la camara y limpia todos los cálculos intermedios /// </summary> public void resetValues() { EnableSpringSystem = true; Spring = DEFAULT_SPRING_CONSTANT; Damping = DEFAULT_DAMPING_CONSTANT; m_offsetDistance = 0.0f; m_headingDegrees = 0.0f; m_pitchDegrees = 0.0f; Eye = new TGCVector3(0.0f, 0.0f, 0.0f); Target = new TGCVector3(0.0f, 0.0f, 0.0f); m_targetYAxis = TGCVector3.Up; m_velocity = new TGCVector3(0.0f, 0.0f, 0.0f); m_viewMatrix = TGCMatrix.Identity; m_orientation = TGCQuaternion.Identity; SetCamera(Eye, Target); }
public override void Update() { PreUpdate(); var rot = rotacionModifier.Value; rot.X = Geometry.DegreeToRadian(rot.X); rot.Y = Geometry.DegreeToRadian(rot.Y); rot.Z = Geometry.DegreeToRadian(rot.Z); //Rotacion Euler boxEuler.Transform = TGCMatrix.RotationYawPitchRoll(rot.Y, rot.X, rot.Z) * TGCMatrix.Translation(boxEuler.Position); //Rotacion TGCQuaternion var q = TGCQuaternion.RotationYawPitchRoll(rot.Y, rot.X, rot.Z); boxTGCQuaternion.Transform = TGCMatrix.RotationTGCQuaternion(q) * TGCMatrix.Translation(boxTGCQuaternion.Position); PostUpdate(); }
private void updateOrientation(float elapsedTimeSec) { m_headingDegrees *= elapsedTimeSec; m_pitchDegrees *= elapsedTimeSec; var heading = FastMath.ToRad(m_headingDegrees); var pitch = FastMath.ToRad(m_pitchDegrees); TGCQuaternion rot; if (heading != 0.0f) { rot = TGCQuaternion.RotationAxis(m_targetYAxis, heading); m_orientation = TGCQuaternion.Multiply(rot, m_orientation); } if (pitch != 0.0f) { rot = TGCQuaternion.RotationAxis(WORLD_XAXIS, pitch); m_orientation = TGCQuaternion.Multiply(m_orientation, rot); } }
public override void Update() { var rot = rotacionModifier.Value; rot.X = Geometry.DegreeToRadian(rot.X); rot.Y = Geometry.DegreeToRadian(rot.Y); rot.Z = Geometry.DegreeToRadian(rot.Z); //Rotacion Euler eulerMesh.Transform = baseScaleRotation * TGCMatrix.RotationYawPitchRoll(rot.Y, rot.X, rot.Z) * baseEulerTranslation; //Rotacion TGCQuaternion TGCQuaternion rotationX = TGCQuaternion.RotationAxis(new TGCVector3(1.0f, 0.0f, 0.0f), rot.X); TGCQuaternion rotationY = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 1.0f, 0.0f), rot.Y); TGCQuaternion rotationZ = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 0.0f, 1.0f), rot.Z); TGCQuaternion rotation = rotationX * rotationY * rotationZ; quaternionMesh.Transform = baseScaleRotation * TGCMatrix.RotationTGCQuaternion(rotation) * baseQuaternionTranslation; }
public TgcSkeletalAnimationFrame(int frame, TGCVector3 position, TGCQuaternion rotation) { Frame = frame; Position = position; Rotation = rotation; }
public void Update() { TGCQuaternion camRot = cam.GetRotation(); TGCMatrix plrTransform = TGCMatrix.Translation(Player.Instance().Position()); mesh.Transform = TGCMatrix.Scaling(mesh.Scale) * TGCMatrix.RotationTGCQuaternion(rotOffset) * TGCMatrix.Translation(posOffset) * TGCMatrix.RotationTGCQuaternion(camRot) * plrTransform; }
/// <summary> /// Permite configurar la orientacion de la cámara respecto del Target /// </summary> /// <param name="cameraRotation">Rotación absoluta a aplicar</param> public void setOrientation(TGCVector3 cameraRotation) { m_orientation = TGCQuaternion.RotationYawPitchRoll(cameraRotation.Y, cameraRotation.X, cameraRotation.Z); m_headingDegrees = 0; m_pitchDegrees = 0; }