// Create is the engine callback for initial asset creation. func (cr *crtag) Create(eng vu.Eng, s *vu.State) { cr.run = 10 // move so many cubes worth in one second. cr.spin = 270 // spin so many degrees in one second. cr.top = eng.Root().NewPov() sun := cr.top.NewPov().SetLocation(0, 10, 10) sun.NewLight().SetColour(0.8, 0.8, 0.8) cr.view = cr.top.NewView() cr.cam = cr.view.Cam() cr.cam.SetPerspective(60, float64(800)/float64(600), 0.1, 500) cr.cam.SetLocation(0, 10, 25) // load the static slab. slab := cr.top.NewPov().SetScale(50, 50, 50).SetLocation(0, -25, 0) slab.NewBody(vu.NewBox(25, 25, 25)) slab.SetSolid(0, 0.4) slab.NewModel("gouraud").LoadMesh("cube").LoadMat("floor") // create a single moving body. useBalls := true // Flip to use boxes instead of spheres. cr.striker = cr.top.NewPov() cr.striker.SetLocation(15, 15, 0) // -5, 15, -3 if useBalls { cr.getBall(cr.striker) } else { cr.getBox(cr.striker) cr.striker.SetRotation(&lin.Q{X: 0.1825742, Y: 0.3651484, Z: 0.5477226, W: 0.7302967}) } cr.striker.Model().SetColour(rand.Float64(), rand.Float64(), rand.Float64()) // create a block of physics bodies. cubeSize := 3 startX := -5 - cubeSize/2 startY := -5 startZ := -3 - cubeSize/2 for k := 0; k < cubeSize; k++ { for i := 0; i < cubeSize; i++ { for j := 0; j < cubeSize; j++ { bod := cr.top.NewPov() lx := float64(2*i + startX) ly := float64(20 + 2*k + startY) lz := float64(2*j + startZ) bod.SetLocation(lx, ly, lz) if useBalls { cr.getBall(bod) } else { cr.getBox(bod) } } } } // set non default engine state. eng.SetColor(0.15, 0.15, 0.15, 1) rand.Seed(time.Now().UTC().UnixNano()) }
// activate the current level. Add physics parts to the physics simulation. func (lvl *level) activate(hm healthMonitor) { lvl.player.monitorHealth("game", hm) lvl.player.resetEnergy() lvl.hd.setLevel(lvl) // reset the camera each time, so it is in a known position. lvl.cam.SetLocation(4, 0.5, 10) lvl.player.resetEnergy() // ensure the walls and floor are added to the physics simulation. for _, wall := range lvl.walls { // set the walls collision shape based on (hand copied from) the .obj file. wall.NewBody(vu.NewBox(1, 1, 1)) wall.SetSolid(0, 0) } lvl.floor.NewBody(vu.NewBox(100, 25, 100)) lvl.floor.SetSolid(0, 0.4) lvl.floor.SetLocation(0, -25, 0) // add a physics body for the camera. lvl.body.NewBody(vu.NewSphere(0.25)) lvl.body.SetSolid(1, 0) }
// getBox creates a visible box physics body. func (cr *crtag) getBox(p vu.Pov) { p.SetScale(2, 2, 2) p.NewBody(vu.NewBox(1, 1, 1)) p.SetSolid(1, 0) p.NewModel("gouraud").LoadMesh("cube").LoadMat("sphere") }