Exemple #1
0
//Creates examples for mater/collision.
func collisionExamples() {
	{
		circle := collision.NewCircle(vect.Vect{}, 1.0)

		saveToFile(circle, "shape_circle")
	}
	{
		segment := collision.NewSegment(vect.Vect{1, 1}, vect.Vect{-1, -1}, 0)

		saveToFile(segment, "shape_segment")
	}
	{
		verts := collision.Vertices{
			{-1, -1},
			{-1, 1},
			{1, 1},
			{1, -1},
		}

		poly := collision.NewPolygon(verts, vect.Vect{})

		saveToFile(poly, "shape_polygon")
	}
	{
		box := collision.NewBox(vect.Vect{}, 1, 1)

		saveToFile(box, "shape_box")
	}
	{
		body := collision.NewBody(collision.BodyType_Static)

		saveToFile(body, "body_static")
	}
	{
		body := collision.NewBody(collision.BodyType_Dynamic)

		saveToFile(body, "body_dynamic")
	}
	{
		space := collision.NewSpace()

		saveToFile(space, "space")
	}
}
Exemple #2
0
func main() {
	log.SetFlags(log.Lshortfile)

	//parse flags
	flag.Parse()

	if flags.cpuprofile != "" {
		f, err := os.Create(flags.cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	loadSettingsFile()
	Settings.Paused = flags.startPaused

	if flags.buildExamples {
		allExamples()
		return
	}

	wx, wy := 800, 600

	//initialize opengl & glfw
	{
		//init glfw
		if err := glfw.Init(); err != nil {
			log.Printf("Error initializing glfw: %v\n", err)
			return
		}
		defer glfw.Terminate()

		//set window hints
		glfw.OpenWindowHint(glfw.WindowNoResize, 1)

		//create the window
		if err := glfw.OpenWindow(wx, wy, 8, 8, 8, 8, 0, 8, glfw.Windowed); err != nil {
			log.Printf("Error opening Window: %v\n", err)
			return
		}
		defer glfw.CloseWindow()

		//init opengl
		if gl.Init() != 0 {
			panic("gl error")
		}

		//glfw config
		{
			glfw.SetSwapInterval(1)
			glfw.SetWindowTitle("mater test")
		}

		//set additional opengl stuff
		{
			gl.ClearColor(0, 0, 0, 0)
			gl.Enable(gl.BLEND)
			//gl.BlendFunc (gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
			gl.Enable(gl.TEXTURE_2D)
		}
	}

	//setup scene related stuff
	{
		//create empty space
		space = collision.NewSpace()
	}

	//reload settings so they take effect
	reloadSettings()
	saveSettingsFile()

	//set callbacks
	{
		glfw.SetWindowSizeCallback(OnResize)
		glfw.SetKeyCallback(OnKey)
	}

	//init debug console
	console.Init()

	//load savefile passed from the commandline if any
	if flags.file != "" {
		err := loadSpace(flags.file)
		Settings.Paused = true
		if err != nil {
			panic(err)
		}
	}

	//if set to true once a second
	printFPS := false

	//fix timestep to given fps
	const expectedFps = 30.0
	const expectedFrameTime = 1.0 / expectedFps

	//the time at the start of the last frame
	lastTime := 0.0

	acc := 0.0
	updateAcc := 0.0

	frameCount := 0
	updateFrameCount := 0

	fps := 0
	updateFps := 0

	Settings.Running = true

	for Settings.Running && glfw.WindowParam(glfw.Opened) == 1 {
		time := glfw.Time()
		//get the time elapsed since the last frame
		dt := time - lastTime
		lastTime = time

		//advance framecount and accumulators
		frameCount++
		acc += dt
		updateAcc += dt

		//execute console commands if any
		select {
		case command := <-console.Command:
			console.ExecuteCommand(command)
		default:
		}

		//update the scene at a fixed timestep
		for updateAcc >= expectedFrameTime {
			updateFrameCount++

			//if one second has passed update the fps and reset the framecount
			if acc > 1 {
				updateFps = updateFrameCount
				updateFrameCount = 0
			}

			//only update if not paused or if set to advance a single frame
			if !Settings.Paused || Settings.SingleStep {
				space.Step(expectedFrameTime)
				Settings.SingleStep = false
			}

			updateAcc -= expectedFrameTime
		}

		//draw debug data
		Draw()

		glfw.SwapBuffers()

		//if one second has passed update the fps and reset the framecount
		if acc > 1 {
			fps = frameCount
			frameCount = 0
			if printFPS {
				fmt.Printf("---\n")
				fmt.Printf("FPS: %v\n", fps)
				fmt.Printf("Update FPS: %v\n", updateFps)
				fmt.Printf("Average frametime: %v\n", acc/float64(fps))
				fmt.Printf("---\n")
			}
			acc -= 1
		}
	}
}
Exemple #3
0
func collisionTests() {
	//circle-segment collision
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			seg := collision.NewSegment(vect.Vect{5, 0}, vect.Vect{-5, 0}, 0.0)
			seg.Friction = 0.2
			body.AddShape(seg)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			seg := collision.NewSegment(vect.Vect{5, 0}, vect.Vect{-5, 0}, 1.0)
			seg.Friction = 0.2
			body.AddShape(seg)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{6, -7}
			body.Transform.SetAngle(0)

			circle := collision.NewCircle(vect.Vect{0, 0}, 1.0)
			circle.Friction = 0.2
			body.AddShape(circle)

			space.AddBody(body)
		}

		saveToFile(space, "circle-segment")
	}

	//circle-polygon collision
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{6, -7}
			body.Transform.SetAngle(0)

			circle := collision.NewCircle(vect.Vect{0, 0}, 1.0)
			circle.Friction = 0.2
			body.AddShape(circle)

			space.AddBody(body)
		}

		saveToFile(space, "circle-polygon")
	}

	//polygon-polygon collision
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{6, -7}
			body.Transform.SetAngle(0)

			box := collision.NewBox(vect.Vect{0, 0}, 1, 1)
			box.Friction = 0.2
			body.AddShape(box)

			space.AddBody(body)
		}

		saveToFile(space, "polygon-polygon")
	}

	//polygon-polygon collision 2
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{6, -7}
			body.Transform.SetAngle(0)

			{
				box := collision.NewBox(vect.Vect{-0.2, -0.5}, 1, 1)
				box.Friction = 0.2
				body.AddShape(box)
			}
			{
				box := collision.NewBox(vect.Vect{0.2, 0.5}, 1, 1)
				box.Friction = 0.2
				body.AddShape(box)
			}

			space.AddBody(body)
		}

		saveToFile(space, "polygon-polygon2")
	}

	//polygon-segment collision
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			seg := collision.NewSegment(vect.Vect{5, 0}, vect.Vect{-5, 0}, 0.0)
			seg.Friction = 0.2
			body.AddShape(seg)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			seg := collision.NewSegment(vect.Vect{5, 0}, vect.Vect{-5, 0}, 1.0)
			seg.Friction = 0.2
			body.AddShape(seg)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{6, -7}
			body.Transform.SetAngle(0)

			box := collision.NewBox(vect.Vect{0, 0}, 1, 1)
			box.Friction = 0.2
			body.AddShape(box)

			space.AddBody(body)
		}

		saveToFile(space, "polygon-segment")
	}

	//segment-polygon collision
	{
		space := collision.NewSpace()
		space.Gravity = vect.Vect{0, 10}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{-3, 0}
			body.Transform.SetAngle(0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Static)
			body.Transform.Position = vect.Vect{3, 0}
			body.Transform.SetAngle(-0.5)

			verts := collision.Vertices{
				{-5, -1},
				{-5, 1},
				{5, 1},
				{5, -1},
			}

			poly := collision.NewPolygon(verts, vect.Vect{})
			poly.Friction = 0.2
			body.AddShape(poly)

			space.AddBody(body)
		}

		{
			body := collision.NewBody(collision.BodyType_Dynamic)
			body.Transform.Position = vect.Vect{5, -7}
			body.Transform.SetAngle(0)

			{
				seg := collision.NewSegment(vect.Vect{-1, 0}, vect.Vect{1, 0}, 0.5)
				seg.Friction = 0.2
				body.AddShape(seg)
			}

			space.AddBody(body)
		}

		saveToFile(space, "segment-polygon")
	}
}