Example #1
0
// see if any of the rigid bodys contact
func generateContacts(delta float64) (bool, []*cubez.Contact) {
	var returnFound bool

	// create the ground plane
	groundPlane := cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0)

	// see if we have a collision with the ground
	found, contacts := cubez.CheckForCollisions(cube.Collider, groundPlane, nil)
	if found {
		returnFound = true
	}
	// see if there's a collision against the backboard
	found, contacts = cubez.CheckForCollisions(cube.Collider, backboard.Collider, contacts)
	if found {
		returnFound = true
	}

	// run collision checks on bullets
	for _, bullet := range bullets {
		// check against the ground
		found, contacts = cubez.CheckForCollisions(bullet.Collider, groundPlane, contacts)
		if found {
			returnFound = true
		}

		// check against the cube
		found, contacts = cubez.CheckForCollisions(cube.Collider, bullet.Collider, contacts)
		if found {
			returnFound = true
		}

		// check against the backboard
		found, contacts = cubez.CheckForCollisions(backboard.Collider, bullet.Collider, contacts)
		if found {
			returnFound = true
		}

		// check against other bullets
		for _, bullet2 := range bullets {
			if bullet2 == bullet {
				continue
			}
			found, contacts = cubez.CheckForCollisions(bullet2.Collider, bullet.Collider, contacts)
			if found {
				returnFound = true
			}
		}
	}

	return returnFound, contacts
}
Example #2
0
func main() {
	app = NewApp()
	app.InitGraphics("Cube Drop", 800, 600)
	app.SetKeyCallback(keyCallback)
	app.OnRender = renderCallback
	app.OnUpdate = updateCallback
	defer app.Terminate()

	// compile the shaders
	var err error
	diffuseShader, err = LoadShaderProgram(DiffuseTextureVertShader, DiffuseTextureFragShader)
	if err != nil {
		panic("Failed to compile the diffuse shader! " + err.Error())
	}

	// setup the slice of cubes to render
	cubes = make([]*Entity, 0, 128)

	// load the grass texture for the ground
	grassTex, err := LoadImageToTexture(grassTexturePath)
	if err != nil {
		panic("Failed to load the grass texture! " + err.Error())
	}

	// load the crate texture for the cubes
	crateTexture, err = LoadImageToTexture(crateTexturePath)
	if err != nil {
		panic("Failed to load the crate texture! " + err.Error())
	}

	// create the ground plane
	groundPlane = cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0)

	// make a ground plane to draw
	ground = CreatePlaneXZ(-500.0, 500.0, 500.0, -500.0, 64.0)
	ground.Shader = diffuseShader
	ground.Color = mgl.Vec4{1.0, 1.0, 1.0, 1.0}
	ground.Tex0 = grassTex

	// setup the camera
	app.CameraPos = mgl.Vec3{0.0, 3.0, 10.0}
	app.CameraRotation = mgl.QuatLookAtV(
		mgl.Vec3{0.0, 5.0, 10.0},
		mgl.Vec3{0.0, 0.0, 0.0},
		mgl.Vec3{0.0, 1.0, 0.0})

	gl.Enable(gl.DEPTH_TEST)
	app.RenderLoop()
}
Example #3
0
func main() {
	app = NewApp()
	app.InitGraphics("Ballistic", 800, 600)
	app.SetKeyCallback(keyCallback)
	app.OnRender = renderCallback
	app.OnUpdate = updateCallback
	defer app.Terminate()

	// compile the shaders
	var err error
	colorShader, err = LoadShaderProgram(DiffuseColorVertShader, DiffuseColorFragShader)
	if err != nil {
		panic("Failed to compile the shader! " + err.Error())
	}

	// create the ground plane
	groundPlane = cubez.NewCollisionPlane(m.Vector3{0.0, 1.0, 0.0}, 0.0)

	// make a ground plane to draw
	ground = CreatePlaneXZ(-500.0, 500.0, 500.0, -500.0, 1.0)
	ground.Shader = colorShader
	ground.Color = mgl.Vec4{0.6, 0.6, 0.6, 1.0}

	// create a test cube to render
	cubeNode := CreateCube(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0)
	cubeNode.Shader = colorShader
	cubeNode.Color = mgl.Vec4{1.0, 0.0, 0.0, 1.0}

	// create the collision box for the the cube
	var cubeMass m.Real = 8.0
	var cubeInertia m.Matrix3
	cubeCollider := cubez.NewCollisionCube(nil, m.Vector3{1.0, 1.0, 1.0})
	cubeCollider.Body.Position = m.Vector3{0.0, 5.0, 0.0}
	cubeCollider.Body.SetMass(cubeMass)
	cubeInertia.SetBlockInertiaTensor(&cubeCollider.HalfSize, cubeMass)
	cubeCollider.Body.SetInertiaTensor(&cubeInertia)
	cubeCollider.Body.CalculateDerivedData()
	cubeCollider.CalculateDerivedData()

	// make the entity out of the renerable and collider
	cube = NewEntity(cubeNode, cubeCollider)

	// make a slice of entities for bullets
	bullets = make([]*Entity, 0, 16)

	// make the backboard to bound the bullets off of
	backboardNode := CreateCube(-0.5, -2.0, -0.25, 0.5, 2.0, 0.25)
	backboardNode.Shader = colorShader
	backboardNode.Color = mgl.Vec4{0.25, 0.2, 0.2, 1.0}
	backboardCollider := cubez.NewCollisionCube(nil, m.Vector3{0.5, 2.0, 0.25})
	backboardCollider.Body.Position = m.Vector3{0.0, 2.0, -10.0}
	backboardCollider.Body.SetInfiniteMass()
	backboardCollider.Body.CalculateDerivedData()
	backboardCollider.CalculateDerivedData()
	SetGlVector3(&backboardNode.Location, &backboardCollider.Body.Position)

	// make the backboard entity
	backboard = NewEntity(backboardNode, backboardCollider)

	// setup the camera
	app.CameraPos = mgl.Vec3{-3.0, 3.0, 15.0}
	app.CameraRotation = mgl.QuatLookAtV(
		mgl.Vec3{-3.0, 3.0, 15.0},
		mgl.Vec3{0.0, 1.0, 0.0},
		mgl.Vec3{0.0, 1.0, 0.0})

	gl.Enable(gl.DEPTH_TEST)
	app.RenderLoop()
}