func (r *Receiver) Simulate(time loop.GameTime) { dt := time.Delta.Seconds() r.Car.Simulate(r.UIState.Controls, dt) r.SetCarTransform(dt) c := r.Data.Car.WorldTransform().Mul4x1(glm.Vec4d{0, 0, 0, 1}) p := glm.Vec4d{0, 0, 1, 0}.Mul(r.UIState.CameraDistance) rotation := r.UIState.Orientation.Conjugate().Mat4() t := glm.Translate3Dd(-p[0], -p[1], -p[2]) r.Data.Cameraview = t.Mul4(rotation).Mul4(glm.Translate3Dd(-c[0], -c[1], -c[2])) }
func (r *Receiver) SetCarTransform(dt float64) { p := r.Car.Center up := glm.Vec3d{0, 0, 1} front := glm.Vec4d{0, 1, 0, 0} // u := glm.Vec3d{r.Car.Direction[0],r.Car.Direction[1],r.Car.Direction[2]} rot := RotationBetweenNormals(front, r.Car.Direction) m := glm.Translate3Dd(p[0], p[1], p[2]).Mul4(rot) r.Data.Car.Transform = m fwav := r.Car.FrontWheelAngularDeviation rwav := r.Car.RearWheelAngularDeviation fr := glm.HomogRotate3DXd(-fwav * 180 / math.Pi) rr := glm.HomogRotate3DXd(-rwav * 180 / math.Pi) wheel, ok := r.Data.Car.FindModelWithName("Wheel_BackLeft") if ok { wheel.Transform = wheel.BaseTransform.Mul4(rr) } wheel, ok = r.Data.Car.FindModelWithName("Wheel_BackRight") if ok { wheel.Transform = wheel.BaseTransform.Mul4(rr) } wheel, ok = r.Data.Car.FindModelWithName("Wheel_FrontLeft") ft := glm.HomogRotate3Dd(-r.Car.FrontWheelO, up) if ok { wheel.Transform = wheel.BaseTransform.Mul4(ft).Mul4(fr) } wheel, ok = r.Data.Car.FindModelWithName("Wheel_FrontRight") if ok { wheel.Transform = wheel.BaseTransform.Mul4(ft).Mul4(fr) } }
func NodeTransform(node *collada.Node) glm.Mat4d { transform := glm.Ident4d() for _, matrix := range node.Matrix { v := matrix.F() transform = transform.Mul4(glm.Mat4d{ v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11], v[12], v[13], v[14], v[15], }.Transpose()) } for _, translate := range node.Translate { v := translate.F() transform = transform.Mul4(glm.Translate3Dd(v[0], v[1], v[2])) } for _, rotation := range node.Rotate { v := rotation.F() transform = transform.Mul4(glm.HomogRotate3Dd(v[3]*math.Pi/180, glm.Vec3d{v[0], v[1], v[2]})) } for _, scale := range node.Scale { v := scale.F() transform = transform.Mul4(glm.Scale3Dd(v[0], v[1], v[2])) } return transform }
func (r *Receiver) Simulate(gameTime gameloop.GameTime) { r.SimulationTime = gameTime deltaT := gameTime.Delta.Seconds() if !r.UIState.Impulse.ApproxEqual(glm.Vec4d{}) { regulatedImpulse := r.UIState.Impulse.Normalize().Mul(r.Constants.PlayerImpulseMomentumLimit) viewAdjustedImpulse := gtk.ToHomogVec4D(r.Player.OrientationH.Rotate(gtk.ToVec3D(regulatedImpulse))) r.Player.Velocity = r.Player.Velocity.Add(viewAdjustedImpulse) r.UIState.Impulse = glm.Vec4d{} } aggregateVelocity := r.Player.Velocity if !r.UIState.Movement.ApproxEqual(glm.Vec4d{}) { regulatedMovement := r.UIState.Movement.Normalize().Mul(r.Constants.PlayerMovementLimit) viewAdjustedMovement := gtk.ToHomogVec4D(r.Player.OrientationH.Rotate(gtk.ToVec3D(regulatedMovement))) aggregateVelocity = aggregateVelocity.Add(viewAdjustedMovement) } dp := aggregateVelocity.Mul(deltaT) for _, p := range r.Portals { portalview := p.Portalview pos := portalview.Mul4x1(r.Player.Position) v := portalview.Mul4x1(dp) if pos[2] < 0 && v[2] > 0 { t := -pos[2] / v[2] hit := pos.Add(v.Mul(t)) if math.Abs(float64(hit[0])) <= 1 && math.Abs(float64(hit[1])) <= 1 && t > 0 && t <= 1 { // fmt.Println("crossed portal", i, pos) ti := p.Transform.Inv() r.Player.Transform(ti) dp = ti.Mul4x1(dp) r.Data.Inception = r.Data.Inception.Mul4(p.Transform) break } } } p0 := r.Player.Position r.Player.Position = p0.Add(dp) //apply gravity if player is off the ground (ys==0) if r.Player.Position[1] > 1 { r.Player.Velocity[1] = r.Player.Velocity[1] + r.Constants.Gravity*deltaT } else { r.Player.Velocity[1] = 0 if p0[1] >= 1 { r.Player.Position[1] = 1 } } p := r.Player.Position translate := glm.Translate3Dd(-p[0], -p[1], -p[2]) rotation := r.Player.Orientation.Conjugate().Mat4() r.Data.Cameraview = rotation.Mul4(translate) }
func PortalTransform(a, b Quad) (glm.Mat4d, glm.Mat4d, glm.Mat4d, glm.Mat4d) { zn := glm.Vec4d{0, 0, 1, 0} xn := glm.Vec4d{1, 0, 0, 0} translateAZ := glm.Translate3Dd(-a.Center[0], -a.Center[1], -a.Center[2]) rotationAZ := RotationBetweenNormals(a.Normal, zn) rotationAXZ := RotationBetweenNormals(rotationAZ.Mul4x1(a.PlaneV), xn) scaleAZ := glm.Scale3Dd(1.0/a.Scale[0], 1.0/a.Scale[1], 1.0/a.Scale[2]) AZ := scaleAZ.Mul4(rotationAXZ).Mul4(rotationAZ).Mul4(translateAZ) ZA := AZ.Inv() translateBZ := glm.Translate3Dd(-b.Center[0], -b.Center[1], -b.Center[2]) rotationBZ := RotationBetweenNormals(b.Normal, zn) rotationBXZ := RotationBetweenNormals(rotationBZ.Mul4x1(b.PlaneV), xn) scaleBZ := glm.Scale3Dd(1.0/b.Scale[0], 1.0/b.Scale[1], 1.0/b.Scale[2]) BZ := scaleBZ.Mul4(rotationBXZ).Mul4(rotationBZ).Mul4(translateBZ) ZB := BZ.Inv() AB := ZB.Mul4(AZ) BA := ZA.Mul4(BZ) return AB, BA, AZ, BZ }
func Translate(v glm.Vec4d) glm.Mat4d { return glm.Translate3Dd(v[0], v[1], v[2]) }