Fix examples

This commit is contained in:
Milan Nikolic 2018-10-09 12:36:54 +02:00
parent 70aa6f5da5
commit f61497c79d
7 changed files with 141 additions and 122 deletions

View file

@ -32,7 +32,6 @@ func main() {
rl.UpdateCamera(&camera) // Update camera
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
// NOTE: This function is NOT WORKING properly!
ray = rl.GetMouseRay(rl.GetMousePosition(), camera)
// Check collision between ray and box

View file

@ -35,10 +35,10 @@ func main() {
rl.DrawRectangle(0, int32(85+40*i), screenWidth, 40, rl.Fade(rl.LightGray, 0.3))
}
rl.DrawText(droppedFiles[i], 120, int32(100), 10, rl.Gray)
rl.DrawText(droppedFiles[i], 120, int32(100+i*40), 10, rl.Gray)
}
rl.DrawText("Drop new files...", 100, int32(150), 20, rl.DarkGray)
rl.DrawText("Drop new files...", 100, int32(150+count*40), 20, rl.DarkGray)
}
rl.EndDrawing()

View file

@ -26,7 +26,7 @@ func main() {
rl.DrawRectangle(screenWidth/2-40, boxPositionY, 80, 80, rl.Maroon)
rl.DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.Gray)
rl.DrawText("Use mouse wheel to move the square up and down!", 10, 10, 20, rl.Gray)
rl.DrawText(fmt.Sprintf("Box position Y: %d", boxPositionY), 10, 40, 20, rl.LightGray)
rl.EndDrawing()

View file

@ -5,15 +5,11 @@ import (
)
func main() {
screenWidth := int32(1080)
screenHeight := int32(600)
// NOTE: screenWidth/screenHeight should match VR device aspect ratio
rl.InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator")
hmd := rl.GetVrDeviceInfo(rl.HmdOculusRiftCv1) // Oculus Rift CV1
rl.InitWindow(int32(hmd.HScreenSize), int32(hmd.VScreenSize), "raylib [core] example - vr simulator")
// NOTE: default device (simulator)
rl.InitVrSimulator(rl.GetVrDeviceInfo(rl.HmdOculusRiftCv1)) // Init VR device (Oculus Rift CV1)
rl.InitVrSimulator(hmd) // Init VR device
camera := rl.Camera{}
camera.Position = rl.NewVector3(5.0, 2.0, 5.0) // Camera position

View file

@ -436,12 +436,12 @@ func (g *Game) Draw() {
}
} else {
// Draw text
rl.DrawText("Floppy Gopher", int32(rl.GetScreenWidth())/2-rl.MeasureText("Floppy Gopher", 40)/2, int32(rl.GetScreenHeight())/2-150, 40, rl.RayWhite)
rl.DrawText("Floppy Gopher", rl.GetScreenWidth()/2-rl.MeasureText("Floppy Gopher", 40)/2, rl.GetScreenHeight()/2-150, 40, rl.RayWhite)
if runtime.GOOS == "android" {
rl.DrawText("[TAP] TO PLAY", int32(rl.GetScreenWidth())/2-rl.MeasureText("[TAP] TO PLAY", 20)/2, int32(rl.GetScreenHeight())/2-50, 20, rl.Black)
rl.DrawText("[TAP] TO PLAY", rl.GetScreenWidth()/2-rl.MeasureText("[TAP] TO PLAY", 20)/2, rl.GetScreenHeight()/2-50, 20, rl.Black)
} else {
rl.DrawText("[ENTER] TO PLAY", int32(rl.GetScreenWidth())/2-rl.MeasureText("[ENTER] TO PLAY", 20)/2, int32(rl.GetScreenHeight())/2-50, 20, rl.Black)
rl.DrawText("[ENTER] TO PLAY", rl.GetScreenWidth()/2-rl.MeasureText("[ENTER] TO PLAY", 20)/2, rl.GetScreenHeight()/2-50, 20, rl.Black)
}
// Draw Gopher

View file

@ -103,7 +103,7 @@ func (g *Game) Init() {
// Update - Update game
func (g *Game) Update() {
if !g.GameOver {
if rl.IsKeyPressed('P') {
if rl.IsKeyPressed(rl.KeyP) {
g.Pause = !g.Pause
}
@ -231,7 +231,7 @@ func (g *Game) Draw() {
rl.DrawText("GAME PAUSED", g.ScreenWidth/2-rl.MeasureText("GAME PAUSED", 40)/2, g.ScreenHeight/2-40, 40, rl.Gray)
}
} else {
rl.DrawText("PRESS [ENTER] TO PLAY AGAIN", int32(rl.GetScreenWidth())/2-rl.MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, int32(rl.GetScreenHeight())/2-50, 20, rl.Gray)
rl.DrawText("PRESS [ENTER] TO PLAY AGAIN", rl.GetScreenWidth()/2-rl.MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, rl.GetScreenHeight()/2-50, 20, rl.Gray)
}
rl.EndDrawing()

View file

@ -1,139 +1,163 @@
package main
import (
"fmt"
"math"
"math/rand"
"github.com/gen2brain/raylib-go/raylib"
"github.com/vova616/chipmunk"
"github.com/vova616/chipmunk/vect"
"github.com/jakecoffman/cp"
)
const (
ballRadius = 25
ballMass = 1
)
// Game type
type Game struct {
Space *chipmunk.Space
Balls []*chipmunk.Shape
StaticLines []*chipmunk.Shape
ticksToNextBall int
var grabbableMaskBit uint = 1 << 31
var grabFilter = cp.ShapeFilter{
cp.NO_GROUP, grabbableMaskBit, grabbableMaskBit,
}
// NewGame - Start new game
func NewGame() (g Game) {
g.Init()
return
func randUnitCircle() cp.Vector {
v := cp.Vector{X: rand.Float64()*2.0 - 1.0, Y: rand.Float64()*2.0 - 1.0}
if v.LengthSq() < 1.0 {
return v
}
return randUnitCircle()
}
// Init - Initialize game
func (g *Game) Init() {
g.createBodies()
g.ticksToNextBall = 10
var simpleTerrainVerts = []cp.Vector{
{350.00, 425.07}, {336.00, 436.55}, {272.00, 435.39}, {258.00, 427.63}, {225.28, 420.00}, {202.82, 396.00},
{191.81, 388.00}, {189.00, 381.89}, {173.00, 380.39}, {162.59, 368.00}, {150.47, 319.00}, {128.00, 311.55},
{119.14, 286.00}, {126.84, 263.00}, {120.56, 227.00}, {141.14, 178.00}, {137.52, 162.00}, {146.51, 142.00},
{156.23, 136.00}, {158.00, 118.27}, {170.00, 100.77}, {208.43, 84.00}, {224.00, 69.65}, {249.30, 68.00},
{257.00, 54.77}, {363.00, 45.94}, {374.15, 54.00}, {386.00, 69.60}, {413.00, 70.73}, {456.00, 84.89},
{468.09, 99.00}, {467.09, 123.00}, {464.92, 135.00}, {469.00, 141.03}, {497.00, 148.67}, {513.85, 180.00},
{509.56, 223.00}, {523.51, 247.00}, {523.00, 277.00}, {497.79, 311.00}, {478.67, 348.00}, {467.90, 360.00},
{456.76, 382.00}, {432.95, 389.00}, {417.00, 411.32}, {373.00, 433.19}, {361.00, 430.02}, {350.00, 425.07},
}
// Update - Update game
func (g *Game) Update() {
g.ticksToNextBall--
if g.ticksToNextBall == 0 {
g.ticksToNextBall = rand.Intn(100) + 1
g.addBall()
}
// creates a circle with random placement
func addCircle(space *cp.Space, radius float64) {
mass := radius * radius / 25.0
body := space.AddBody(cp.NewBody(mass, cp.MomentForCircle(mass, 0, radius, cp.Vector{})))
body.SetPosition(randUnitCircle().Mult(180))
// Physics steps calculations
g.step(rl.GetFrameTime())
shape := space.AddShape(cp.NewCircle(body, radius, cp.Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0.9)
}
// Draw - Draw game
func (g *Game) Draw() {
for i := range g.StaticLines {
x := g.StaticLines[i].GetAsSegment().A.X
y := g.StaticLines[i].GetAsSegment().A.Y
// creates a simple terrain to contain bodies
func simpleTerrain() *cp.Space {
space := cp.NewSpace()
space.Iterations = 10
space.SetGravity(cp.Vector{0, -100})
space.SetCollisionSlop(0.5)
x2 := g.StaticLines[i].GetAsSegment().B.X
y2 := g.StaticLines[i].GetAsSegment().B.Y
rl.DrawLine(int32(x), int32(y), int32(x2), int32(y2), rl.DarkBlue)
offset := cp.Vector{X: -320, Y: -240}
for i := 0; i < len(simpleTerrainVerts)-1; i++ {
a := simpleTerrainVerts[i]
b := simpleTerrainVerts[i+1]
space.AddShape(cp.NewSegment(space.StaticBody, a.Add(offset), b.Add(offset), 0))
}
for _, b := range g.Balls {
pos := b.Body.Position()
rl.DrawCircleLines(int32(pos.X), int32(pos.Y), float32(ballRadius), rl.DarkBlue)
}
}
// createBodies sets up the chipmunk space and static bodies
func (g *Game) createBodies() {
g.Space = chipmunk.NewSpace()
g.Space.Gravity = vect.Vect{0, 900}
staticBody := chipmunk.NewBodyStatic()
g.StaticLines = []*chipmunk.Shape{
chipmunk.NewSegment(vect.Vect{250.0, 240.0}, vect.Vect{550.0, 280.0}, 0),
chipmunk.NewSegment(vect.Vect{550.0, 280.0}, vect.Vect{550.0, 180.0}, 0),
}
for _, segment := range g.StaticLines {
segment.SetElasticity(0.6)
staticBody.AddShape(segment)
}
g.Space.AddBody(staticBody)
}
// addBall adds ball to chipmunk space and body
func (g *Game) addBall() {
x := rand.Intn(600-200) + 200
ball := chipmunk.NewCircle(vect.Vector_Zero, float32(ballRadius))
ball.SetElasticity(0.95)
body := chipmunk.NewBody(vect.Float(ballMass), ball.Moment(float32(ballMass)))
body.SetPosition(vect.Vect{vect.Float(x), 0.0})
body.SetAngle(vect.Float(rand.Float32() * 2 * math.Pi))
body.AddShape(ball)
g.Space.AddBody(body)
g.Balls = append(g.Balls, ball)
}
// step advances the physics engine and cleans up any balls that are off-screen
func (g *Game) step(dt float32) {
g.Space.Step(vect.Float(dt))
for i := 0; i < len(g.Balls); i++ {
p := g.Balls[i].Body.Position()
if p.Y < -100 {
g.Space.RemoveBody(g.Balls[i].Body)
g.Balls[i] = nil
g.Balls = append(g.Balls[:i], g.Balls[i+1:]...)
i-- // consider same index again
}
}
return space
}
func main() {
rl.InitWindow(800, 450, "raylib [physics] example - chipmunk")
const width, height = 800, 450
const physicsTickrate = 1.0 / 60.0
rl.SetTargetFPS(60)
rl.SetConfigFlags(rl.FlagVsyncHint)
rl.InitWindow(width, height, "raylib [physics] example - chipmunk")
game := NewGame()
offset := rl.Vector2{X: width / 2, Y: height / 2}
// since the example ported from elsewhere, flip the camera 180 and offset to center it
camera := rl.NewCamera2D(offset, rl.Vector2{}, 180, 1)
space := simpleTerrain()
for i := 0; i < 1000; i++ {
addCircle(space, 5)
}
mouseBody := cp.NewKinematicBody()
var mouse cp.Vector
var mouseJoint *cp.Constraint
var accumulator, dt float32
lastTime := rl.GetTime()
for !rl.WindowShouldClose() {
// calculate dt
now := rl.GetTime()
dt = now - lastTime
lastTime = now
// update the mouse position
mousePos := rl.GetMousePosition()
// alter the mouse coordinates based on the camera position, rotation
mouse.X = float64(mousePos.X-camera.Offset.X) * -1
mouse.Y = float64(mousePos.Y-camera.Offset.Y) * -1
// smooth mouse movements to new position
newPoint := mouseBody.Position().Lerp(mouse, 0.25)
mouseBody.SetVelocityVector(newPoint.Sub(mouseBody.Position()).Mult(60.0))
mouseBody.SetPosition(newPoint)
// handle grabbing
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
result := space.PointQueryNearest(mouse, 5, grabFilter)
if result.Shape != nil && result.Shape.Body().Mass() < cp.INFINITY {
var nearest cp.Vector
if result.Distance > 0 {
nearest = result.Point
} else {
nearest = mouse
}
// create a new constraint where the mouse is to draw the body towards the mouse
body := result.Shape.Body()
mouseJoint = cp.NewPivotJoint2(mouseBody, body, cp.Vector{}, body.WorldToLocal(nearest))
mouseJoint.SetMaxForce(50000)
mouseJoint.SetErrorBias(math.Pow(1.0-0.15, 60.0))
space.AddConstraint(mouseJoint)
}
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) && mouseJoint != nil {
space.RemoveConstraint(mouseJoint)
mouseJoint = nil
}
// perform a fixed rate physics tick
accumulator += dt
for accumulator >= physicsTickrate {
space.Step(physicsTickrate)
accumulator -= physicsTickrate
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode2D(camera)
game.Update()
game.Draw()
// this is a generic way to iterate over the shapes in a space,
// to avoid the type switch just keep a pointer to the shapes when they've been created
space.EachShape(func(s *cp.Shape) {
switch s.Class.(type) {
case *cp.Segment:
segment := s.Class.(*cp.Segment)
a := segment.A()
b := segment.B()
rl.DrawLineV(v(a), v(b), rl.Black)
case *cp.Circle:
circle := s.Class.(*cp.Circle)
pos := circle.Body().Position()
rl.DrawCircleV(v(pos), float32(circle.Radius()), rl.Red)
default:
fmt.Println("unexpected shape", s.Class)
}
})
rl.EndMode2D()
rl.DrawFPS(0, 0)
rl.EndDrawing()
}
rl.CloseWindow()
}
func v(v cp.Vector) rl.Vector2 {
return rl.Vector2{X: float32(v.X), Y: float32(v.Y)}
}