/// <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 Render() { // Renderizar la malla del auto, en este caso solo el Chasis Mesh.Transform = TGCMatrix.Translation(new TGCVector3(0, meshAxisRadius.Y - (meshRealHeight / 2f), 0)) * new TGCMatrix(rigidBody.InterpolationWorldTransform); Mesh.Render(); TGCMatrix wheelTransform; // Como las ruedas no son cuerpos rigidos (aún) se procede a realizar las transformaciones de las ruedas para renderizar wheelTransform = TGCMatrix.RotationY(vehicle.GetSteeringValue(0)) * TGCMatrix.RotationTGCQuaternion(new TGCQuaternion(RigidBody.Orientation.X, RigidBody.Orientation.Y, RigidBody.Orientation.Z, RigidBody.Orientation.W)) * TGCMatrix.Translation(new TGCVector3(vehicle.GetWheelInfo(0).WorldTransform.Origin)); wheel.Transform = wheelTransform; wheel.Render(); wheelTransform = TGCMatrix.RotationY(vehicle.GetSteeringValue(1) + FastMath.PI) * TGCMatrix.RotationTGCQuaternion(new TGCQuaternion(RigidBody.Orientation.X, RigidBody.Orientation.Y, RigidBody.Orientation.Z, RigidBody.Orientation.W)) * TGCMatrix.Translation(new TGCVector3(vehicle.GetWheelInfo(1).WorldTransform.Origin)); wheel.Transform = wheelTransform; wheel.Render(); wheelTransform = TGCMatrix.RotationY(-vehicle.GetSteeringValue(2)) * TGCMatrix.RotationTGCQuaternion(new TGCQuaternion(RigidBody.Orientation.X, RigidBody.Orientation.Y, RigidBody.Orientation.Z, RigidBody.Orientation.W)) * TGCMatrix.Translation(new TGCVector3(vehicle.GetWheelInfo(2).WorldTransform.Origin)); wheel.Transform = wheelTransform; wheel.Render(); wheelTransform = TGCMatrix.RotationY(-vehicle.GetSteeringValue(3) + FastMath.PI) * TGCMatrix.RotationTGCQuaternion(new TGCQuaternion(RigidBody.Orientation.X, RigidBody.Orientation.Y, RigidBody.Orientation.Z, RigidBody.Orientation.W)) * TGCMatrix.Translation(new TGCVector3(vehicle.GetWheelInfo(3).WorldTransform.Origin)); wheel.Transform = wheelTransform; wheel.Render(); }
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(); }
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; } //-------- }
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> /// Prepara una nueva animacion para ser ejecutada /// </summary> protected void initAnimationSettings(string animationName, bool playLoop, float userFrameRate) { isAnimating = true; currentAnimation = animations[animationName]; this.playLoop = playLoop; currentTime = 0; currentFrame = 0; //Cambiar BoundingBox boundingBox = currentAnimation.BoundingBox; updateBoundingBox(); //Si el usuario no especifico un FrameRate, tomar el default de la animacion if (userFrameRate == -1f) { frameRate = currentAnimation.FrameRate; } else { frameRate = userFrameRate; } //La duracion de la animacion. animationTimeLenght = ((float)currentAnimation.FramesCount - 1) / frameRate; //Configurar postura inicial de los huesos for (var i = 0; i < bones.Length; i++) { var bone = bones[i]; if (!currentAnimation.hasFrames(i)) { throw new Exception("El hueso " + bone.Name + " no posee KeyFrames"); } //Determinar matriz local inicial var firstFrame = currentAnimation.BoneFrames[i][0]; bone.MatLocal = TGCMatrix.RotationTGCQuaternion(firstFrame.Rotation) * TGCMatrix.Translation(firstFrame.Position); //Multiplicar por matriz del padre, si tiene if (bone.ParentBone != null) { bone.MatFinal = bone.MatLocal * bone.ParentBone.MatFinal; } else { bone.MatFinal = bone.MatLocal; } } //Ajustar vertices a posicion inicial del esqueleto updateMeshVertices(); }
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> /// 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; } } }
private void updateViewMatrix() { m_orientation.Normalize(); m_viewMatrix = TGCMatrix.RotationTGCQuaternion(m_orientation); var m_xAxis = new TGCVector3(m_viewMatrix.M11, m_viewMatrix.M21, m_viewMatrix.M31); var m_yAxis = new TGCVector3(m_viewMatrix.M12, m_viewMatrix.M22, m_viewMatrix.M32); var m_zAxis = new TGCVector3(m_viewMatrix.M13, m_viewMatrix.M23, m_viewMatrix.M33); Eye = Target + m_zAxis * -m_offsetDistance; m_viewMatrix.M41 = -TGCVector3.Dot(m_xAxis, Eye); m_viewMatrix.M42 = -TGCVector3.Dot(m_yAxis, Eye); m_viewMatrix.M43 = -TGCVector3.Dot(m_zAxis, Eye); }
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); }
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); }
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 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); }
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; }
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(); }
//Common functinos /// <param name="goalPos">Posicion en el mundo a la que se quiere llegar</param> /// <param name="speed">Velocidad a la que la entidad se mueve</param> protected void Move(TGCVector3 goalPos, float speed, float ElapsedTime) { TGCVector3 dir = TGCVector3.Normalize(goalPos - mesh.Position); LookAt(dir); TGCVector3 movement = dir * speed * ElapsedTime; mesh.Position += movement; //Check out for collisions foreach (var naveMesh in Nave.Instance().obtenerMeshes()) { bool col = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, naveMesh.BoundingBox); if (col) { mesh.Position -= movement; ResetMove(); break; } } mesh.Transform = TGCMatrix.Scaling(mesh.Scale) * TGCMatrix.RotationTGCQuaternion(rotation) * TGCMatrix.Translation(mesh.Position); }
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; }
private void InitializeShark() { var loader = new TgcSceneLoader(); var scene = loader.loadSceneFromFile(MediaDir + "Aquatic\\Meshes\\shark-TgcScene.xml"); shark = scene.Meshes[0]; TGCVector3 rotationDirection = TGCVector3.Up; rotationDirection.Normalize(); float angle = FastMath.QUARTER_PI / 2.0f; baseSharkTransform = TGCMatrix.Scaling(new TGCVector3(0.08f, 0.08f, 0.08f)) * TGCMatrix.RotationTGCQuaternion(new TGCQuaternion(0.0f, FastMath.Sin(angle), 0.0f, FastMath.Cos(angle))); shark.Transform = baseSharkTransform; }
private void updateViewMatrix(float elapsedTimeSec) { m_orientation.Normalize(); m_viewMatrix = TGCMatrix.RotationTGCQuaternion(m_orientation); var m_xAxis = new TGCVector3(m_viewMatrix.M11, m_viewMatrix.M21, m_viewMatrix.M31); var m_yAxis = new TGCVector3(m_viewMatrix.M12, m_viewMatrix.M22, m_viewMatrix.M32); var m_zAxis = new TGCVector3(m_viewMatrix.M13, m_viewMatrix.M23, m_viewMatrix.M33); // Calculate the new camera position. The 'idealPosition' is where the // camera should be position. The camera should be positioned directly // behind the target at the required offset distance. What we're doing here // is rather than have the camera immediately snap to the 'idealPosition' // we slowly move the camera towards the 'idealPosition' using a spring // system. // // References: // Stone, Jonathan, "Third-Person Camera Navigation," Game Programming // Gems 4, Andrew Kirmse, Editor, Charles River Media, Inc., 2004. var idealPosition = Target + m_zAxis * -m_offsetDistance; var displacement = Eye - idealPosition; var springAcceleration = -Spring * displacement - Damping * m_velocity; m_velocity += springAcceleration * elapsedTimeSec; Eye += m_velocity * elapsedTimeSec; // The view matrix is always relative to the camera's current position // 'm_eye'. Since a spring system is being used here 'm_eye' will be // relative to 'desiredPosition'. When the camera is no longer being moved // 'm_eye' will become the same as 'desiredPosition'. The local x, y, and // z axes that were extracted from the camera's orientation 'm_orienation' // is correct for the 'desiredPosition' only. We need to recompute these // axes so that they're relative to 'm_eye'. Once that's done we can use // those axes to reconstruct the view matrix. m_zAxis = Target - Eye; m_zAxis.Normalize(); m_xAxis = TGCVector3.Cross(m_targetYAxis, m_zAxis); m_xAxis.Normalize(); m_yAxis = TGCVector3.Cross(m_zAxis, m_xAxis); m_yAxis.Normalize(); m_viewMatrix = TGCMatrix.Identity; m_viewMatrix.M11 = m_xAxis.X; m_viewMatrix.M21 = m_xAxis.Y; m_viewMatrix.M31 = m_xAxis.Z; m_viewMatrix.M41 = -TGCVector3.Dot(m_xAxis, Eye); m_viewMatrix.M12 = m_yAxis.X; m_viewMatrix.M22 = m_yAxis.Y; m_viewMatrix.M32 = m_yAxis.Z; m_viewMatrix.M42 = -TGCVector3.Dot(m_yAxis, Eye); m_viewMatrix.M13 = m_zAxis.X; m_viewMatrix.M23 = m_zAxis.Y; m_viewMatrix.M33 = m_zAxis.Z; m_viewMatrix.M43 = -TGCVector3.Dot(m_zAxis, Eye); }
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; }