Beispiel #1
0
func testbox3(position Vec3) *GameObj {

	body := NewGameObj(position, Vec3{1, 1, 1}, Vec3{0, 0, 1}, 84, 0, nil)
	//body2 := NewGameObj(Vec3{0,2,0},Vec3{1,1,1},Vec3{1,0,0},1,0,body)
	//	body.Constraints = []Constraint{ObjectConstraint{body,body2,Vec3{0,4,0},100,40}}
	glfw.AddListener(func(keyev glfw.KeyEvent) {

	})
	return body
}
Beispiel #2
0
func main() {
	//ABSPTest()
	//return
	glfw.Init(800, 600)
	InitPhysics()

	world := new(World)
	world.Init()
	world.GameObjects = new(list.List)
	player := MakeMan(Vec3{10, 20, 10})

	world.Add(player)
	world.Add(ropetest(Vec3{0, 40, 0}, 4, 4))
	world.Add(treeThing(Vec3{20, 20, 20}, 3))
	world.Add(MakePlayer(Vec3{-20, 20, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 50, 0}).Body)

	world.Add(MakePlayer(Vec3{-20, 70, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 90, 0}).Body)
	world.Add(MakePlayer(Vec3{-20, 110, 0}).Body)
	/*for i := 0; i < 100; i++ {
		world.Add(SetPlayerAnimation(MakePlayer(Vec3{float32(int(i%10))*50,0,float32(i*2) - 200})).Body)
	}*/
	world.Add(NewGameObj(Vec3{0, -20, 0}, Vec3{10000, 10, 10000}, Vec3{0, 0.5, 0.1}, float32(math.Inf(1)), 10, nil))

	//world.Add(SetPlayerAnimation(MakePlayer(Vec3{-20,120,0})).Body)

	qtn := new(ABSPNode)
	qtn.Root = qtn
	//qtn.Position = Vec3{-10000,-10000,-10000}
	//qtn.Size = Vec3{20000,20000,20000}
	for i := world.GameObjects.Front(); i != nil; i = i.Next() {
		gobj := i.Value.(*GameObj)
		all := gobj.GetSubs()
		for i := 0; i < len(all); i++ {
			qtn.Insert(all[i])
		}
	}
	world.GameObjectTree = qtn
	fmt.Println("Total:", len(qtn.Data))
	qtn.Divide()
	cols := 0
	qtn.cd(func(obj1, obj2 SPData) {
		cols += 1
	})
	fmt.Println(cols)
	//qtn.Traverse(0)
	//return
	gl.Init()
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	vs.Source(
		LoadFileToString("s1.vert"))
	vs.Compile()

	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	fs.Source(LoadFileToString("s1.frag"))
	fs.Compile()

	pg := gl.CreateProgram()
	pg.AttachShader(vs)
	pg.AttachShader(fs)
	pg.Link()
	pg.Validate()

	pg.Use()
	fmt.Println("**Shader log**")
	fmt.Println(fs.GetInfoLog())
	fmt.Println(vs.GetInfoLog())
	fmt.Println(pg.GetInfoLog())
	fmt.Println("******END*****")

	gl.ClearColor(0.5, 0.5, 1, 0)
	gl.Enable(gl.DEPTH_TEST)
	gl.Enable(gl.BLEND)
	gl.Enable(gl.POLYGON_SMOOTH)
	gl.Hint(gl.POLYGON_SMOOTH_HINT, gl.NICEST)

	var t float64
	t = float64(time.Nanoseconds()) / 1000000000
	cam1 := Camera{player, 100, Vec3{0, 0, 0}}
	glfw.AddListener(func(m glfw.MouseMoveEvent) {
		cam1.Angle.X = float32(m.X-400) / 400 * 3.14 * 2
		cam1.Angle.Y = float32(m.Y-300) / 300 * 3.14 * 2
		//player.Rotation = Vec3{cam1.Angle.X,cam1.Angle.Y,0}
	})
	glfw.AddListener(func(mw glfw.MouseWheelEvent) {
		cam1.Distance = 100 + float32(mw.Pos*mw.Pos*mw.Pos)
	})

	world.CompilePhysicsObjects()
	for it := 0; it < 10000; it += 1 {
		cam1.Setup()
		dt := float32(0.005)
		t = float64(float64(time.Nanoseconds()) / 1000000000)
		UpdatePositions(world.PhysicsObjects, dt)

		UpdateModelStates(world.PhysicsObjects)

		UpdateCollisions(world.GameObjectTree, dt)
		UpdateModelStates(world.PhysicsObjects)
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		DrawWorld(world.GameObjects, dt, pg)
		glfw.SwapBuffers()
		pt := float64(float64(time.Nanoseconds()) / 1000000000)
		sleeptime := (float64(dt) - (pt - t)) * 10e8
		fmt.Println("Sleep for:", sleeptime)
		time.Sleep(int64(sleeptime))
	}

}
Beispiel #3
0
func MakeMan(position Vec3) *GameObj {
	body := NewGameObj(position, Vec3{1, 2, 1}, Vec3{0.5, 0.5, 0.5}, 50, 0, nil)

	rleg := NewGameObj(position.Add(Vec3{2, 0, 0}), Vec3{0.5, 0.5, 0.5}, Vec3{0.1, 0.2, 0.3}, 1.1, 5, body)
	rlegc := ObjectConstraint{body, rleg, Vec3{2, -6, 0}, 3000, 200}

	lleg := NewGameObj(Vec3{-2, 0, 0}, Vec3{0.5, 0.5, 0.5}, Vec3{0.3, 0.2, 0.1}, 1.1, 5, body)
	llegc := ObjectConstraint{body, lleg, Vec3{-2, -6, 0}, 3000, 200}

	rarm := NewGameObj(Vec3{2, 10, 0}, Vec3{0.5, 0.5, 0.5}, Vec3{0.1, 0.2, 0.7}, 1, 0, body)
	rarmc := ObjectConstraint{body, rarm, Vec3{2, 4, 0}, 100, 10}

	larm := NewGameObj(Vec3{-2, 10, 0}, Vec3{0.5, 0.5, 0.5}, Vec3{0.3, 0.2, 0.1}, 1, 0, body)
	larmc := ObjectConstraint{body, larm, Vec3{-2, 4, 0}, 100, 10}

	head := NewGameObj(Vec3{0, 5, 0}, Vec3{1, 1, 1}, Vec3{0.5, 0.2, 0.2}, 5, 0, body)
	headc := ObjectConstraint{body, head, Vec3{0, 4, 0}, 1000, 40}

	body.Constraints = []Constraint{&rlegc, &llegc, &rarmc, &headc, &larmc}
	body.Anim = func(self *Box3D, t float32) {

	}
	var jumpBusy bool
	jumpBusy = false
	rleg_begin := rlegc.V
	lleg_begin := llegc.V
	/*larm_begin := larmc.V
		rarm_begin := rarmc.V
		var walkCycle float32 = 0
		var speed float32 = 1
		advAnim := func(self *Box3D,t float32){
			walkCycle += 0.01*speed


			rlegc.V = (rleg_begin.Add(Vec3{0,3,0}.Rotate(walkCycle + math.Pi,0))).Rotate(0, -body.Rotation.X)
			llegc.V = (lleg_begin.Add(Vec3{0,3,0}.Rotate(walkCycle, 0))).Rotate(0, -body.Rotation.X)
			rarmc.V = rarm_begin.Add(Vec3{0,-2,0}.Rotate(walkCycle,0)).Rotate(0,-body.Rotation.X)
			larmc.V = larm_begin.Rotate(0,-body.Rotation.X)


	}
		body.Anim = advAnim
	*/

	StartJump := func(self *Box3D, t float32) {
		jumpStart := t * 3
		if rlegc.V.Y < llegc.V.Y {
			rlegc.V = (rleg_begin.Sub(Vec3{0, 5, -7})).Rotate(0, -body.Rotation.X)
		} else {
			llegc.V = (lleg_begin.Sub(Vec3{0, 5, -7})).Rotate(0, -body.Rotation.X)
		}
		body.Anim = func(self *Box3D, t float32) {
			if t-jumpStart > 0.5 {
				rlegc.V = rleg_begin
				llegc.V = lleg_begin

			}
		}
	}
	var direction float32 = 1

	Walk := func(self *Box3D, t float32) {
		var tc float32 = 0

		body.Anim = func(self *Box3D, t float32) {

			tc = t * 3
			rlegc.V = (rleg_begin.Add(Vec3{0, 3, 0}.Rotate(direction*(tc+math.Pi), 0))).Rotate(0, -body.Rotation.X)
			llegc.V = (lleg_begin.Add(Vec3{0, 3, 0}.Rotate(direction*tc, 0))).Rotate(0, -body.Rotation.X)

		}

	}

	glfw.AddListener(
		func(keyev glfw.KeyEvent) {
			fmt.Println(keyev)
			switch keyev.Key {

			case glfw.KEY_RIGHT, glfw.KEY_D:
				{

				}
			case glfw.KEY_LEFT:
				{

				}
			case glfw.KEY_UP:
				{
					direction = 1
					body.Anim = Walk

				}
			case glfw.KEY_DOWN:
				{
					direction = -1
					body.Anim = Walk
				}
			case glfw.KEY_SPACE:
				{
					if !jumpBusy {
						body.Anim = StartJump
					}
				}
			}
		})

	return body

}