Пример #1
0
func newBox(width, height float32) *box {
	var err error

	box := new(box)

	// Sound player

	box.player, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Chipmunk body

	box.physicsShape = chipmunk.NewBox(
		vect.Vect{0, 0},
		vect.Float(width),
		vect.Float(height),
	)

	box.physicsShape.SetElasticity(BoxElasticity)
	box.physicsBody = chipmunk.NewBody(vect.Float(BoxMass), box.physicsShape.Moment(float32(BoxMass)))
	box.physicsBody.AddShape(box.physicsShape)
	box.physicsBody.CallbackHandler = callbacks{}

	// OpenGL shape

	box.openglShape = shapes.NewBox(width, height)

	return box
}
func addRoom(pos vect.Vect, w vect.Float, h vect.Float) {
	box := chipmunk.NewBox(vect.Vector_Zero, w+0.5, h+0.5)
	box.SetElasticity(0.5)
	body := chipmunk.NewBody(1.0, box.Moment(float32(1.0)))
	body.SetPosition(pos)
	body.AddShape(box)
	room := Room{
		Color: mgl32.Vec4{.3, .3, 1, .2},
		Box:   box,
	}
	rooms = append(rooms, &room)
}
// add a pipe box to the space
func addOnePipeBox(pos vect.Vect) {
	pipeBox := chipmunk.NewBox(vect.Vector_Zero, vect.Float(pipeSide), vect.Float(pipeSide))
	pipeBox.SetElasticity(0.6)

	body := chipmunk.NewBody(chipmunk.Inf, chipmunk.Inf)
	body.SetPosition(pos)
	body.SetVelocity(pipeVelX, pipeVelY)
	body.IgnoreGravity = true

	body.AddShape(pipeBox)
	space.AddBody(body)
	pipe = append(pipe, pipeBox)
}
Пример #4
0
func NewPhysics(static bool) *Physics {
	var body *chipmunk.Body

	box := chipmunk.NewBox(vect.Vect{0, 0}, vect.Float(1), vect.Float(1))

	if static {
		body = chipmunk.NewBodyStatic()
	} else {
		body = chipmunk.NewBody(1, box.Moment(1))
	}

	p := &Physics{BaseComponent: NewComponent(), Body: body, Box: box.GetAsBox(), Shape: box}

	body.AddShape(box)
	return p
}
func addFlappy() {
	flappyBird := chipmunk.NewBox(vect.Vector_Zero, vect.Float(pipeSide), vect.Float(pipeSide))
	flappyBird.SetElasticity(0.95)

	body := chipmunk.NewBody(vect.Float(flappyMass), vect.Float(flappyMoment))
	body.SetPosition(vect.Vect{100, vect.Float(winHeight)})
	body.SetAngularVelocity(float32(10 * deg2rad))

	// hook collision events
	handlers := collisionHandlers{}
	body.CallbackHandler = handlers

	body.AddShape(flappyBird)
	space.AddBody(body)
	flappyBirds = append(flappyBirds, flappyBird)
}
Пример #6
0
func newBox(world *World, width, height float32) *Box {
	box := new(Box)

	// Chipmunk body

	box.physicsShape = chipmunk.NewBox(
		vect.Vect{0, 0},
		vect.Float(width),
		vect.Float(height),
	)

	box.physicsShape.SetElasticity(BoxElasticity)
	box.physicsBody = chipmunk.NewBody(vect.Float(BoxMass), box.physicsShape.Moment(float32(BoxMass)))
	box.physicsBody.AddShape(box.physicsShape)
	box.physicsBody.CallbackHandler = callbacks{}

	// OpenGL shape

	box.openglShape = shapes.NewBox(world.boxProgramShader, width, height)

	return box
}
Пример #7
0
func (m *Map) GenerateCollision() {

	var shapes []*chipmunk.Shape

	for x := 0; x < m.Width; x++ {
		for y := 0; y < m.Height; y++ {
			if t, e := m.GetTile(x, y); e {
				if t.Collision() != CollisionNone {
					p, _ := m.GetTilePos(x, y)
					shape := chipmunk.NewBox(vect.Vect{vect.Float(p.X), vect.Float(p.Y)},
						vect.Float(m.TileSize), vect.Float(m.TileSize))

					shape.Layer = chipmunk.Layer(m.Layer)
					shape.SetFriction(0)
					shapes = append(shapes, shape)
				}
			}
		}
	}

	m.GameObject().AddComponent(engine.NewPhysicsShapes(true, shapes))

}