Example #1
0
func safeNormalize(v mgl32.Vec3) mgl32.Vec3 {
	v = v.Normalize()
	if math.IsInf(float64(v[0]), 0) || math.IsNaN(float64(v[0])) {
		return mgl32.Vec3{}
	}
	return v
}
Example #2
0
// CreateBeam - creates a square prism oriented along the vector
func CreateBeam(width float32, vector mgl32.Vec3) *Geometry {
	direction := vector.Normalize()
	geo := CreateBoxWithOffset(width, width, -width*0.5, -width*0.5)
	geo2 := CreateBoxWithOffset(width, width, -width*0.5, -width*0.5)
	facingTx := util.Mat4From(mgl32.Vec3{1, 1, 1}, mgl32.Vec3{}, util.FacingOrientation(0, direction, mgl32.Vec3{0, 0, 1}, mgl32.Vec3{1, 0, 0}))
	geo.Transform(facingTx)
	facingTx = util.Mat4From(mgl32.Vec3{1, 1, 1}, vector, util.FacingOrientation(0, direction, mgl32.Vec3{0, 0, -1}, mgl32.Vec3{1, 0, 0}))
	geo2.Optimize(geo, facingTx)
	geo.Indicies = append(geo.Indicies, 0, 1, 4, 4, 5, 0) //top
	geo.Indicies = append(geo.Indicies, 1, 2, 7, 7, 4, 1) //side
	geo.Indicies = append(geo.Indicies, 2, 3, 6, 6, 7, 2) //bottom
	geo.Indicies = append(geo.Indicies, 3, 0, 5, 5, 6, 3) //side
	return geo
}
Example #3
0
func (c *Camera) Update(dt float32) {
	/* Handle keyboard input */
	move := false
	dir := mgl.Vec3{}
	if KeyDown(KeyW) && !KeyDown(KeyS) {
		dir[2] += 1
		move = true
	}
	if KeyDown(KeyS) && !KeyDown(KeyW) {
		dir[2] -= 1
		move = true
	}
	if KeyDown(KeyA) && !KeyDown(KeyD) {
		dir[0] -= 1
		move = true
	}
	if KeyDown(KeyD) && !KeyDown(KeyA) {
		dir[0] += 1
		move = true
	}
	if KeyDown(KeyE) && !KeyDown(KeyQ) {
		dir[1] += 1
		move = true
	}
	if KeyDown(KeyQ) && !KeyDown(KeyE) {
		dir[1] -= 1
		move = true
	}

	if move {
		/* Calculate normalized movement vector */
		dv := 5.0 * dt /* magic number: movement speed */
		dir = dir.Normalize().Mul(dv)

		right := c.Transform.Right.Mul(dir[0])
		up := mgl.Vec3{0, dir[1], 0}
		forward := c.Transform.Forward.Mul(dir[2])

		/* Translate camera */
		c.Transform.Translate(right.Add(up.Add(forward)))
	}

	/* Mouse look */
	if MouseDown(MouseButton1) {
		rx := c.Transform.Rotation[0] - Mouse.DY*0.08
		ry := c.Transform.Rotation[1] - Mouse.DX*0.09

		/* Camera angle limits */
		/* -90 < rx < 90 */
		rx = float32(math.Max(-90.0, math.Min(90.0, float64(rx))))

		/* -180 < ry < 180 */
		if ry > 180.0 {
			ry -= 360.0
		}
		if ry < -180.0 {
			ry += 360.0
		}
		c.Transform.Rotation[0] = rx
		c.Transform.Rotation[1] = ry
	}

	/* Update transform with new position & rotation */
	c.Transform.Update(dt)

	/* Calculate new view matrix based on forward vector */
	lookAt := c.Transform.Position.Add(c.Transform.Forward)
	c.View = mgl.LookAtV(c.Transform.Position, lookAt, mgl.Vec3{0, 1, 0})
}