From 5b0944b5567afab6e75a20001f6a4263981b6608 Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Sat, 5 Sep 2020 17:38:24 +0200 Subject: [PATCH] Move raymath to raylib package, issue #58 --- Makefile | 2 +- physics/physics.go | 181 ++++++++++++------------ {raymath => raylib}/raymath.go | 251 ++++++++++++++++----------------- raymath/README.md | 3 - 4 files changed, 215 insertions(+), 222 deletions(-) rename {raymath => raylib}/raymath.go (79%) delete mode 100644 raymath/README.md diff --git a/Makefile b/Makefile index 964f4d9..0e4a951 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PACKAGES= raylib raygui raymath easings physics rres +PACKAGES= raylib raygui easings physics rres GO?= go diff --git a/physics/physics.go b/physics/physics.go index 0d09a22..fc61a7f 100644 --- a/physics/physics.go +++ b/physics/physics.go @@ -7,7 +7,6 @@ import ( "math" "github.com/gen2brain/raylib-go/raylib" - "github.com/gen2brain/raylib-go/raymath" ) // ShapeType type @@ -196,7 +195,7 @@ func NewBodyRectangle(pos rl.Vector2, width, height, density float32) *Body { } p2 := newBody.Shape.VertexData.Vertices[nextIndex] - D := raymath.Vector2CrossProduct(p1, p2) + D := rl.Vector2CrossProduct(p1, p2) triangleArea := D / 2 area += triangleArea @@ -276,7 +275,7 @@ func NewBodyPolygon(pos rl.Vector2, radius float32, sides int, density float32) } position2 := newBody.Shape.VertexData.Vertices[nextIndex] - cross := raymath.Vector2CrossProduct(position1, position2) + cross := rl.Vector2CrossProduct(position1, position2) triangleArea := cross / 2 area += triangleArea @@ -502,7 +501,7 @@ func Close() { // AddForce - Adds a force to a physics body func (b *Body) AddForce(force rl.Vector2) { - b.Force = raymath.Vector2Add(b.Force, force) + b.Force = rl.Vector2Add(b.Force, force) } // AddTorque - Adds an angular force to a physics body @@ -521,14 +520,14 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { for i := 0; i < vertexData.VertexCount; i++ { positionA := b.Position - positionB := raymath.Mat2MultiplyVector2(vertexData.Transform, raymath.Vector2Add(b.Position, vertexData.Vertices[i])) + positionB := rl.Mat2MultiplyVector2(vertexData.Transform, rl.Vector2Add(b.Position, vertexData.Vertices[i])) nextIndex := 0 if i+1 < vertexData.VertexCount { nextIndex = i + 1 } - positionC := raymath.Mat2MultiplyVector2(vertexData.Transform, raymath.Vector2Add(b.Position, vertexData.Vertices[nextIndex])) + positionC := rl.Mat2MultiplyVector2(vertexData.Transform, rl.Vector2Add(b.Position, vertexData.Vertices[nextIndex])) // Check collision between each triangle alpha := ((positionB.Y-positionC.Y)*(position.X-positionC.X) + (positionC.X-positionB.X)*(position.Y-positionC.Y)) / @@ -565,8 +564,8 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { } center := triangleBarycenter(vertices[i], vertices[nextIndex], rl.NewVector2(0, 0)) - center = raymath.Vector2Add(bodyPos, center) - offset := raymath.Vector2Subtract(center, bodyPos) + center = rl.Vector2Add(bodyPos, center) + offset := rl.Vector2Subtract(center, bodyPos) newBody := NewBodyPolygon(center, 10, 3, 10) // Create polygon physics body with relevant values @@ -574,9 +573,9 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { newData.VertexCount = 3 newData.Transform = trans - newData.Vertices[0] = raymath.Vector2Subtract(vertices[i], offset) - newData.Vertices[1] = raymath.Vector2Subtract(vertices[nextIndex], offset) - newData.Vertices[2] = raymath.Vector2Subtract(position, center) + newData.Vertices[0] = rl.Vector2Subtract(vertices[i], offset) + newData.Vertices[1] = rl.Vector2Subtract(vertices[nextIndex], offset) + newData.Vertices[2] = rl.Vector2Subtract(position, center) // Separate vertices to avoid unnecessary physics collisions newData.Vertices[0].X *= 0.95 @@ -593,7 +592,7 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { nextVertex = j + 1 } - face := raymath.Vector2Subtract(newData.Vertices[nextVertex], newData.Vertices[j]) + face := rl.Vector2Subtract(newData.Vertices[nextVertex], newData.Vertices[j]) newData.Normals[j] = rl.NewVector2(face.Y, -face.X) normalize(&newData.Normals[j]) @@ -617,7 +616,7 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { } p2 := newBody.Shape.VertexData.Vertices[nextVertex] - D := raymath.Vector2CrossProduct(p1, p2) + D := rl.Vector2CrossProduct(p1, p2) triangleArea := D / 2 area += triangleArea @@ -647,10 +646,10 @@ func (b *Body) Shatter(position rl.Vector2, force float32) { // Calculate explosion force direction pointA := newBody.Position - pointB := raymath.Vector2Subtract(newData.Vertices[1], newData.Vertices[0]) + pointB := rl.Vector2Subtract(newData.Vertices[1], newData.Vertices[0]) pointB.X /= 2 pointB.Y /= 2 - forceDirection := raymath.Vector2Subtract(raymath.Vector2Add(pointA, raymath.Vector2Add(newData.Vertices[0], pointB)), newBody.Position) + forceDirection := rl.Vector2Subtract(rl.Vector2Add(pointA, rl.Vector2Add(newData.Vertices[0], pointB)), newBody.Position) normalize(&forceDirection) forceDirection.X *= force forceDirection.Y *= force @@ -671,7 +670,7 @@ func (b *Body) GetShapeVertex(vertex int) rl.Vector2 { position.Y = b.Position.Y + float32(math.Sin(360/float64(circleVertices)*float64(vertex)*rl.Deg2rad))*b.Shape.Radius break case PolygonShape: - position = raymath.Vector2Add(b.Position, raymath.Mat2MultiplyVector2(b.Shape.VertexData.Transform, b.Shape.VertexData.Vertices[vertex])) + position = rl.Vector2Add(b.Position, rl.Mat2MultiplyVector2(b.Shape.VertexData.Transform, b.Shape.VertexData.Vertices[vertex])) break } @@ -683,7 +682,7 @@ func (b *Body) SetRotation(radians float32) { b.Orient = radians if b.Shape.Type == PolygonShape { - b.Shape.VertexData.Transform = raymath.Mat2Radians(radians) + b.Shape.VertexData.Transform = rl.Mat2Radians(radians) } } @@ -700,7 +699,7 @@ func (b *Body) integrateVelocity() { b.Orient += b.AngularVelocity * deltaTime } - raymath.Mat2Set(&b.Shape.VertexData.Transform, b.Orient) + rl.Mat2Set(&b.Shape.VertexData.Transform, b.Orient) b.integrateForces() } @@ -730,7 +729,7 @@ func newRandomPolygon(radius float32, sides int) Polygon { data.VertexCount = sides orient := rl.GetRandomValue(0, 360) - data.Transform = raymath.Mat2Radians(float32(orient) * rl.Deg2rad) + data.Transform = rl.Mat2Radians(float32(orient) * rl.Deg2rad) // Calculate polygon vertices positions for i := 0; i < data.VertexCount; i++ { @@ -745,7 +744,7 @@ func newRandomPolygon(radius float32, sides int) Polygon { nextIndex = i + 1 } - face := raymath.Vector2Subtract(data.Vertices[nextIndex], data.Vertices[i]) + face := rl.Vector2Subtract(data.Vertices[nextIndex], data.Vertices[i]) data.Normals[i] = rl.NewVector2(face.Y, -face.X) normalize(&data.Normals[i]) @@ -759,7 +758,7 @@ func newRectanglePolygon(pos, size rl.Vector2) Polygon { data := Polygon{} data.VertexCount = 4 - data.Transform = raymath.Mat2Radians(0) + data.Transform = rl.Mat2Radians(0) // Calculate polygon vertices positions data.Vertices[0] = rl.NewVector2(pos.X+size.X/2, pos.Y-size.Y/2) @@ -773,7 +772,7 @@ func newRectanglePolygon(pos, size rl.Vector2) Polygon { if i+1 < data.VertexCount { nextIndex = i + 1 } - face := raymath.Vector2Subtract(data.Vertices[nextIndex], data.Vertices[i]) + face := rl.Vector2Subtract(data.Vertices[nextIndex], data.Vertices[i]) data.Normals[i] = rl.NewVector2(face.Y, -face.X) normalize(&data.Normals[i]) @@ -852,9 +851,9 @@ func (m *manifold) solveCircleToCircle() { bodyB := m.BodyB // Calculate translational vector, which is normal - normal := raymath.Vector2Subtract(bodyB.Position, bodyA.Position) + normal := rl.Vector2Subtract(bodyB.Position, bodyA.Position) - distSqr := raymath.Vector2LenSqr(normal) + distSqr := rl.Vector2LenSqr(normal) radius := bodyA.Shape.Radius + bodyB.Shape.Radius // Check if circles are not in contact @@ -888,7 +887,7 @@ func (m *manifold) solveCircleToPolygon() { // Transform circle center to polygon transform space center := m.BodyA.Position - center = raymath.Mat2MultiplyVector2(raymath.Mat2Transpose(m.BodyB.Shape.VertexData.Transform), raymath.Vector2Subtract(center, m.BodyB.Position)) + center = rl.Mat2MultiplyVector2(rl.Mat2Transpose(m.BodyB.Shape.VertexData.Transform), rl.Vector2Subtract(center, m.BodyB.Position)) // Find edge with minimum penetration // It is the same concept as using support points in solvePolygonToPolygon @@ -897,7 +896,7 @@ func (m *manifold) solveCircleToPolygon() { vertexData := m.BodyB.Shape.VertexData for i := 0; i < vertexData.VertexCount; i++ { - currentSeparation := raymath.Vector2DotProduct(vertexData.Normals[i], raymath.Vector2Subtract(center, vertexData.Vertices[i])) + currentSeparation := rl.Vector2DotProduct(vertexData.Normals[i], rl.Vector2Subtract(center, vertexData.Vertices[i])) if currentSeparation > m.BodyA.Shape.Radius { return @@ -920,7 +919,7 @@ func (m *manifold) solveCircleToPolygon() { // Check to see if center is within polygon if separation < epsilon { m.ContactsCount = 1 - normal := raymath.Mat2MultiplyVector2(vertexData.Transform, vertexData.Normals[faceNormal]) + normal := rl.Mat2MultiplyVector2(vertexData.Transform, vertexData.Normals[faceNormal]) m.Normal = rl.NewVector2(-normal.X, -normal.Y) m.Contacts[0] = rl.NewVector2(m.Normal.X*m.BodyA.Shape.Radius+m.BodyA.Position.X, m.Normal.Y*m.BodyA.Shape.Radius+m.BodyA.Position.Y) m.Penetration = m.BodyA.Shape.Radius @@ -928,44 +927,44 @@ func (m *manifold) solveCircleToPolygon() { } // Determine which voronoi region of the edge center of circle lies within - dot1 := raymath.Vector2DotProduct(raymath.Vector2Subtract(center, v1), raymath.Vector2Subtract(v2, v1)) - dot2 := raymath.Vector2DotProduct(raymath.Vector2Subtract(center, v2), raymath.Vector2Subtract(v1, v2)) + dot1 := rl.Vector2DotProduct(rl.Vector2Subtract(center, v1), rl.Vector2Subtract(v2, v1)) + dot2 := rl.Vector2DotProduct(rl.Vector2Subtract(center, v2), rl.Vector2Subtract(v1, v2)) m.Penetration = m.BodyA.Shape.Radius - separation if dot1 <= 0 { // Closest to v1 - if raymath.Vector2Distance(center, v1) > m.BodyA.Shape.Radius*m.BodyA.Shape.Radius { + if rl.Vector2Distance(center, v1) > m.BodyA.Shape.Radius*m.BodyA.Shape.Radius { return } m.ContactsCount = 1 - normal := raymath.Vector2Subtract(v1, center) - normal = raymath.Mat2MultiplyVector2(vertexData.Transform, normal) + normal := rl.Vector2Subtract(v1, center) + normal = rl.Mat2MultiplyVector2(vertexData.Transform, normal) normalize(&normal) m.Normal = normal - v1 = raymath.Mat2MultiplyVector2(vertexData.Transform, v1) - v1 = raymath.Vector2Add(v1, m.BodyB.Position) + v1 = rl.Mat2MultiplyVector2(vertexData.Transform, v1) + v1 = rl.Vector2Add(v1, m.BodyB.Position) m.Contacts[0] = v1 } else if dot2 <= 0 { // Closest to v2 - if raymath.Vector2Distance(center, v2) > m.BodyA.Shape.Radius*m.BodyA.Shape.Radius { + if rl.Vector2Distance(center, v2) > m.BodyA.Shape.Radius*m.BodyA.Shape.Radius { return } m.ContactsCount = 1 - normal := raymath.Vector2Subtract(v2, center) - v2 = raymath.Mat2MultiplyVector2(vertexData.Transform, v2) - v2 = raymath.Vector2Add(v2, m.BodyB.Position) + normal := rl.Vector2Subtract(v2, center) + v2 = rl.Mat2MultiplyVector2(vertexData.Transform, v2) + v2 = rl.Vector2Add(v2, m.BodyB.Position) m.Contacts[0] = v2 - normal = raymath.Mat2MultiplyVector2(vertexData.Transform, normal) + normal = rl.Mat2MultiplyVector2(vertexData.Transform, normal) normalize(&normal) m.Normal = normal } else { // Closest to face normal := vertexData.Normals[faceNormal] - if raymath.Vector2DotProduct(raymath.Vector2Subtract(center, v1), normal) > m.BodyA.Shape.Radius { + if rl.Vector2DotProduct(rl.Vector2Subtract(center, v1), normal) > m.BodyA.Shape.Radius { return } - normal = raymath.Mat2MultiplyVector2(vertexData.Transform, normal) + normal = rl.Mat2MultiplyVector2(vertexData.Transform, normal) m.Normal = rl.NewVector2(-normal.X, -normal.Y) m.Contacts[0] = rl.NewVector2(m.Normal.X*m.BodyA.Shape.Radius+m.BodyA.Position.X, m.Normal.Y*m.BodyA.Shape.Radius+m.BodyA.Position.Y) m.ContactsCount = 1 @@ -1038,20 +1037,20 @@ func (m *manifold) solvePolygonToPolygon() { v2 := refData.Vertices[referenceIndex] // Transform vertices to world space - v1 = raymath.Mat2MultiplyVector2(refData.Transform, v1) - v1 = raymath.Vector2Add(v1, refPoly.Body.Position) - v2 = raymath.Mat2MultiplyVector2(refData.Transform, v2) - v2 = raymath.Vector2Add(v2, refPoly.Body.Position) + v1 = rl.Mat2MultiplyVector2(refData.Transform, v1) + v1 = rl.Vector2Add(v1, refPoly.Body.Position) + v2 = rl.Mat2MultiplyVector2(refData.Transform, v2) + v2 = rl.Vector2Add(v2, refPoly.Body.Position) // Calculate reference face side normal in world space - sidePlaneNormal := raymath.Vector2Subtract(v2, v1) + sidePlaneNormal := rl.Vector2Subtract(v2, v1) normalize(&sidePlaneNormal) // Orthogonalize refFaceNormal := rl.NewVector2(sidePlaneNormal.Y, -sidePlaneNormal.X) - refC := raymath.Vector2DotProduct(refFaceNormal, v1) - negSide := raymath.Vector2DotProduct(sidePlaneNormal, v1) * -1 - posSide := raymath.Vector2DotProduct(sidePlaneNormal, v2) + refC := rl.Vector2DotProduct(refFaceNormal, v1) + negSide := rl.Vector2DotProduct(sidePlaneNormal, v1) * -1 + posSide := rl.Vector2DotProduct(sidePlaneNormal, v2) // clip incident face to reference face side planes (due to floating point error, possible to not have required points if clip(rl.NewVector2(-sidePlaneNormal.X, -sidePlaneNormal.Y), negSide, &incidentFace0, &incidentFace1) < 2 { @@ -1070,7 +1069,7 @@ func (m *manifold) solvePolygonToPolygon() { // Keep points behind reference face currentPoint := 0 // clipped points behind reference face - separation := raymath.Vector2DotProduct(refFaceNormal, incidentFace0) - refC + separation := rl.Vector2DotProduct(refFaceNormal, incidentFace0) - refC if separation <= 0 { m.Contacts[currentPoint] = incidentFace0 m.Penetration = -separation @@ -1079,7 +1078,7 @@ func (m *manifold) solvePolygonToPolygon() { m.Penetration = 0 } - separation = raymath.Vector2DotProduct(refFaceNormal, incidentFace1) - refC + separation = rl.Vector2DotProduct(refFaceNormal, incidentFace1) - refC if separation <= 0 { m.Contacts[currentPoint] = incidentFace1 @@ -1105,11 +1104,11 @@ func (m *manifold) initializeManifolds() { for i := 0; i < 2; i++ { // Caculate radius from center of mass to contact - radiusA := raymath.Vector2Subtract(m.Contacts[i], bodyA.Position) - radiusB := raymath.Vector2Subtract(m.Contacts[i], bodyB.Position) + radiusA := rl.Vector2Subtract(m.Contacts[i], bodyA.Position) + radiusB := rl.Vector2Subtract(m.Contacts[i], bodyB.Position) - crossA := raymath.Vector2Cross(bodyA.AngularVelocity, radiusA) - crossB := raymath.Vector2Cross(bodyB.AngularVelocity, radiusB) + crossA := rl.Vector2Cross(bodyA.AngularVelocity, radiusA) + crossB := rl.Vector2Cross(bodyB.AngularVelocity, radiusB) radiusV := rl.Vector2{} radiusV.X = bodyB.Velocity.X + crossB.X - bodyA.Velocity.X - crossA.X @@ -1117,7 +1116,7 @@ func (m *manifold) initializeManifolds() { // Determine if we should perform a resting collision or not; // The idea is if the only thing moving this object is gravity, then the collision should be performed without any restitution - if raymath.Vector2LenSqr(radiusV) < (raymath.Vector2LenSqr(rl.NewVector2(gravityForce.X*deltaTime/1000, gravityForce.Y*deltaTime/1000)) + epsilon) { + if rl.Vector2LenSqr(radiusV) < (rl.Vector2LenSqr(rl.NewVector2(gravityForce.X*deltaTime/1000, gravityForce.Y*deltaTime/1000)) + epsilon) { m.Restitution = 0 } } @@ -1137,24 +1136,24 @@ func (m *manifold) integrateImpulses() { for i := 0; i < m.ContactsCount; i++ { // Calculate radius from center of mass to contact - radiusA := raymath.Vector2Subtract(m.Contacts[i], bodyA.Position) - radiusB := raymath.Vector2Subtract(m.Contacts[i], bodyB.Position) + radiusA := rl.Vector2Subtract(m.Contacts[i], bodyA.Position) + radiusB := rl.Vector2Subtract(m.Contacts[i], bodyB.Position) // Calculate relative velocity radiusV := rl.Vector2{} - radiusV.X = bodyB.Velocity.X + raymath.Vector2Cross(bodyB.AngularVelocity, radiusB).X - bodyA.Velocity.X - raymath.Vector2Cross(bodyA.AngularVelocity, radiusA).X - radiusV.Y = bodyB.Velocity.Y + raymath.Vector2Cross(bodyB.AngularVelocity, radiusB).Y - bodyA.Velocity.Y - raymath.Vector2Cross(bodyA.AngularVelocity, radiusA).Y + radiusV.X = bodyB.Velocity.X + rl.Vector2Cross(bodyB.AngularVelocity, radiusB).X - bodyA.Velocity.X - rl.Vector2Cross(bodyA.AngularVelocity, radiusA).X + radiusV.Y = bodyB.Velocity.Y + rl.Vector2Cross(bodyB.AngularVelocity, radiusB).Y - bodyA.Velocity.Y - rl.Vector2Cross(bodyA.AngularVelocity, radiusA).Y // Relative velocity along the normal - contactVelocity := raymath.Vector2DotProduct(radiusV, m.Normal) + contactVelocity := rl.Vector2DotProduct(radiusV, m.Normal) // Do not resolve if velocities are separating if contactVelocity > 0 { return } - raCrossN := raymath.Vector2CrossProduct(radiusA, m.Normal) - rbCrossN := raymath.Vector2CrossProduct(radiusB, m.Normal) + raCrossN := rl.Vector2CrossProduct(radiusA, m.Normal) + rbCrossN := rl.Vector2CrossProduct(radiusB, m.Normal) inverseMassSum := bodyA.InverseMass + bodyB.InverseMass + (raCrossN*raCrossN)*bodyA.InverseInertia + (rbCrossN*rbCrossN)*bodyB.InverseInertia @@ -1170,7 +1169,7 @@ func (m *manifold) integrateImpulses() { bodyA.Velocity.X += bodyA.InverseMass * (-impulseV.X) bodyA.Velocity.Y += bodyA.InverseMass * (-impulseV.Y) if !bodyA.FreezeOrient { - bodyA.AngularVelocity += bodyA.InverseInertia * raymath.Vector2CrossProduct(radiusA, rl.NewVector2(-impulseV.X, -impulseV.Y)) + bodyA.AngularVelocity += bodyA.InverseInertia * rl.Vector2CrossProduct(radiusA, rl.NewVector2(-impulseV.X, -impulseV.Y)) } } @@ -1178,19 +1177,19 @@ func (m *manifold) integrateImpulses() { bodyB.Velocity.X += bodyB.InverseMass * (impulseV.X) bodyB.Velocity.Y += bodyB.InverseMass * (impulseV.Y) if !bodyB.FreezeOrient { - bodyB.AngularVelocity += bodyB.InverseInertia * raymath.Vector2CrossProduct(radiusB, impulseV) + bodyB.AngularVelocity += bodyB.InverseInertia * rl.Vector2CrossProduct(radiusB, impulseV) } } // Apply friction impulse to each physics body - radiusV.X = bodyB.Velocity.X + raymath.Vector2Cross(bodyB.AngularVelocity, radiusB).X - bodyA.Velocity.X - raymath.Vector2Cross(bodyA.AngularVelocity, radiusA).X - radiusV.Y = bodyB.Velocity.Y + raymath.Vector2Cross(bodyB.AngularVelocity, radiusB).Y - bodyA.Velocity.Y - raymath.Vector2Cross(bodyA.AngularVelocity, radiusA).Y + radiusV.X = bodyB.Velocity.X + rl.Vector2Cross(bodyB.AngularVelocity, radiusB).X - bodyA.Velocity.X - rl.Vector2Cross(bodyA.AngularVelocity, radiusA).X + radiusV.Y = bodyB.Velocity.Y + rl.Vector2Cross(bodyB.AngularVelocity, radiusB).Y - bodyA.Velocity.Y - rl.Vector2Cross(bodyA.AngularVelocity, radiusA).Y - tangent := rl.NewVector2(radiusV.X-(m.Normal.X*raymath.Vector2DotProduct(radiusV, m.Normal)), radiusV.Y-(m.Normal.Y*raymath.Vector2DotProduct(radiusV, m.Normal))) + tangent := rl.NewVector2(radiusV.X-(m.Normal.X*rl.Vector2DotProduct(radiusV, m.Normal)), radiusV.Y-(m.Normal.Y*rl.Vector2DotProduct(radiusV, m.Normal))) normalize(&tangent) // Calculate impulse tangent magnitude - impulseTangent := -(raymath.Vector2DotProduct(radiusV, tangent)) + impulseTangent := -(rl.Vector2DotProduct(radiusV, tangent)) impulseTangent /= inverseMassSum impulseTangent /= float32(m.ContactsCount) @@ -1215,7 +1214,7 @@ func (m *manifold) integrateImpulses() { bodyA.Velocity.Y += bodyA.InverseMass * (-tangentImpulse.Y) if !bodyA.FreezeOrient { - bodyA.AngularVelocity += bodyA.InverseInertia * raymath.Vector2CrossProduct(radiusA, rl.NewVector2(-tangentImpulse.X, -tangentImpulse.Y)) + bodyA.AngularVelocity += bodyA.InverseInertia * rl.Vector2CrossProduct(radiusA, rl.NewVector2(-tangentImpulse.X, -tangentImpulse.Y)) } } @@ -1224,7 +1223,7 @@ func (m *manifold) integrateImpulses() { bodyB.Velocity.Y += bodyB.InverseMass * (tangentImpulse.Y) if !bodyB.FreezeOrient { - bodyB.AngularVelocity += bodyB.InverseInertia * raymath.Vector2CrossProduct(radiusB, tangentImpulse) + bodyB.AngularVelocity += bodyB.InverseInertia * rl.Vector2CrossProduct(radiusB, tangentImpulse) } } } @@ -1257,7 +1256,7 @@ func getSupport(shape Shape, dir rl.Vector2) rl.Vector2 { for i := 0; i < shape.VertexData.VertexCount; i++ { vertex := shape.VertexData.Vertices[i] - projection := raymath.Vector2DotProduct(vertex, dir) + projection := rl.Vector2DotProduct(vertex, dir) if projection > bestProjection { bestVertex = vertex @@ -1279,24 +1278,24 @@ func findAxisLeastPenetration(shapeA, shapeB Shape) (int, float32) { for i := 0; i < dataA.VertexCount; i++ { // Retrieve a face normal from A shape normal := dataA.Normals[i] - transNormal := raymath.Mat2MultiplyVector2(dataA.Transform, normal) + transNormal := rl.Mat2MultiplyVector2(dataA.Transform, normal) // Transform face normal into B shape's model space - buT := raymath.Mat2Transpose(dataB.Transform) - normal = raymath.Mat2MultiplyVector2(buT, transNormal) + buT := rl.Mat2Transpose(dataB.Transform) + normal = rl.Mat2MultiplyVector2(buT, transNormal) // Retrieve support point from B shape along -n support := getSupport(shapeB, rl.NewVector2(-normal.X, -normal.Y)) // Retrieve vertex on face from A shape, transform into B shape's model space vertex := dataA.Vertices[i] - vertex = raymath.Mat2MultiplyVector2(dataA.Transform, vertex) - vertex = raymath.Vector2Add(vertex, shapeA.Body.Position) - vertex = raymath.Vector2Subtract(vertex, shapeB.Body.Position) - vertex = raymath.Mat2MultiplyVector2(buT, vertex) + vertex = rl.Mat2MultiplyVector2(dataA.Transform, vertex) + vertex = rl.Vector2Add(vertex, shapeA.Body.Position) + vertex = rl.Vector2Subtract(vertex, shapeB.Body.Position) + vertex = rl.Mat2MultiplyVector2(buT, vertex) // Compute penetration distance in B shape's model space - distance := raymath.Vector2DotProduct(normal, raymath.Vector2Subtract(support, vertex)) + distance := rl.Vector2DotProduct(normal, rl.Vector2Subtract(support, vertex)) // Store greatest distance if distance > bestDistance { @@ -1316,15 +1315,15 @@ func findIncidentFace(v0, v1 *rl.Vector2, ref, inc Shape, index int) { referenceNormal := refData.Normals[index] // Calculate normal in incident's frame of reference - referenceNormal = raymath.Mat2MultiplyVector2(refData.Transform, referenceNormal) // To world space - referenceNormal = raymath.Mat2MultiplyVector2(raymath.Mat2Transpose(incData.Transform), referenceNormal) // To incident's model space + referenceNormal = rl.Mat2MultiplyVector2(refData.Transform, referenceNormal) // To world space + referenceNormal = rl.Mat2MultiplyVector2(rl.Mat2Transpose(incData.Transform), referenceNormal) // To incident's model space // Find most anti-normal face on polygon incidentFace := 0 minDot := float32(fltMax) for i := 0; i < incData.VertexCount; i++ { - dot := raymath.Vector2DotProduct(referenceNormal, incData.Normals[i]) + dot := rl.Vector2DotProduct(referenceNormal, incData.Normals[i]) if dot < minDot { minDot = dot @@ -1333,8 +1332,8 @@ func findIncidentFace(v0, v1 *rl.Vector2, ref, inc Shape, index int) { } // Assign face vertices for incident face - *v0 = raymath.Mat2MultiplyVector2(incData.Transform, incData.Vertices[incidentFace]) - *v0 = raymath.Vector2Add(*v0, inc.Body.Position) + *v0 = rl.Mat2MultiplyVector2(incData.Transform, incData.Vertices[incidentFace]) + *v0 = rl.Vector2Add(*v0, inc.Body.Position) if incidentFace+1 < incData.VertexCount { incidentFace = incidentFace + 1 @@ -1342,8 +1341,8 @@ func findIncidentFace(v0, v1 *rl.Vector2, ref, inc Shape, index int) { incidentFace = 0 } - *v1 = raymath.Mat2MultiplyVector2(incData.Transform, incData.Vertices[incidentFace]) - *v1 = raymath.Vector2Add(*v1, inc.Body.Position) + *v1 = rl.Mat2MultiplyVector2(incData.Transform, incData.Vertices[incidentFace]) + *v1 = rl.Vector2Add(*v1, inc.Body.Position) } // clip - Calculates clipping based on a normal and two faces @@ -1355,8 +1354,8 @@ func clip(normal rl.Vector2, clip float32, faceA, faceB *rl.Vector2) int { out[1] = *faceB // Retrieve distances from each endpoint to the line - distanceA := raymath.Vector2DotProduct(normal, *faceA) - clip - distanceB := raymath.Vector2DotProduct(normal, *faceB) - clip + distanceA := rl.Vector2DotProduct(normal, *faceA) - clip + distanceB := rl.Vector2DotProduct(normal, *faceB) - clip // If negative (behind plane) if distanceA <= 0 { @@ -1373,10 +1372,10 @@ func clip(normal rl.Vector2, clip float32, faceA, faceB *rl.Vector2) int { // Push intersection point alpha := distanceA / (distanceA - distanceB) out[sp] = *faceA - delta := raymath.Vector2Subtract(*faceB, *faceA) + delta := rl.Vector2Subtract(*faceB, *faceA) delta.X *= alpha delta.Y *= alpha - out[sp] = raymath.Vector2Add(out[sp], delta) + out[sp] = rl.Vector2Add(out[sp], delta) sp++ } diff --git a/raymath/raymath.go b/raylib/raymath.go similarity index 79% rename from raymath/raymath.go rename to raylib/raymath.go index c08c4fd..b211063 100644 --- a/raymath/raymath.go +++ b/raylib/raymath.go @@ -1,50 +1,47 @@ -// Package raymath - Some useful functions to work with Vector2, Vector3, Matrix and Quaternions -package raymath +package rl import ( "math" - - "github.com/gen2brain/raylib-go/raylib" ) // Vector2Zero - Vector with components value 0.0 -func Vector2Zero() rl.Vector2 { - return rl.NewVector2(0.0, 0.0) +func Vector2Zero() Vector2 { + return NewVector2(0.0, 0.0) } // Vector2One - Vector with components value 1.0 -func Vector2One() rl.Vector2 { - return rl.NewVector2(1.0, 1.0) +func Vector2One() Vector2 { + return NewVector2(1.0, 1.0) } // Vector2Add - Add two vectors (v1 + v2) -func Vector2Add(v1, v2 rl.Vector2) rl.Vector2 { - return rl.NewVector2(v1.X+v2.X, v1.Y+v2.Y) +func Vector2Add(v1, v2 Vector2) Vector2 { + return NewVector2(v1.X+v2.X, v1.Y+v2.Y) } // Vector2Subtract - Subtract two vectors (v1 - v2) -func Vector2Subtract(v1, v2 rl.Vector2) rl.Vector2 { - return rl.NewVector2(v1.X-v2.X, v1.Y-v2.Y) +func Vector2Subtract(v1, v2 Vector2) Vector2 { + return NewVector2(v1.X-v2.X, v1.Y-v2.Y) } // Vector2Length - Calculate vector length -func Vector2Length(v rl.Vector2) float32 { +func Vector2Length(v Vector2) float32 { return float32(math.Sqrt(float64((v.X * v.X) + (v.Y * v.Y)))) } // Vector2DotProduct - Calculate two vectors dot product -func Vector2DotProduct(v1, v2 rl.Vector2) float32 { +func Vector2DotProduct(v1, v2 Vector2) float32 { return v1.X*v2.X + v1.Y*v2.Y } // Vector2Distance - Calculate distance between two vectors -func Vector2Distance(v1, v2 rl.Vector2) float32 { +func Vector2Distance(v1, v2 Vector2) float32 { return float32(math.Sqrt(float64((v1.X-v2.X)*(v1.X-v2.X) + (v1.Y-v2.Y)*(v1.Y-v2.Y)))) } // Vector2Angle - Calculate angle between two vectors in X-axis -func Vector2Angle(v1, v2 rl.Vector2) float32 { - angle := float32(math.Atan2(float64(v2.Y-v1.Y), float64(v2.X-v1.X)) * (180.0 / float64(rl.Pi))) +func Vector2Angle(v1, v2 Vector2) float32 { + angle := float32(math.Atan2(float64(v2.Y-v1.Y), float64(v2.X-v1.X)) * (180.0 / float64(Pi))) if angle < 0 { angle += 360.0 @@ -54,60 +51,60 @@ func Vector2Angle(v1, v2 rl.Vector2) float32 { } // Vector2Scale - Scale vector (multiply by value) -func Vector2Scale(v rl.Vector2, scale float32) rl.Vector2 { - return rl.NewVector2(v.X*scale, v.Y*scale) +func Vector2Scale(v Vector2, scale float32) Vector2 { + return NewVector2(v.X*scale, v.Y*scale) } // Vector2Multiply - Multiply vector by vector -func Vector2Multiply(v1, v2 rl.Vector2) rl.Vector2 { - return rl.NewVector2(v1.X*v2.X, v1.Y*v2.Y) +func Vector2Multiply(v1, v2 Vector2) Vector2 { + return NewVector2(v1.X*v2.X, v1.Y*v2.Y) } // Vector2Negate - Negate vector -func Vector2Negate(v rl.Vector2) rl.Vector2 { - return rl.NewVector2(-v.X, -v.Y) +func Vector2Negate(v Vector2) Vector2 { + return NewVector2(-v.X, -v.Y) } // Vector2Divide - Divide vector by vector -func Vector2DivideV(v1, v2 rl.Vector2) rl.Vector2 { - return rl.NewVector2(v1.X/v2.X, v1.Y/v2.Y) +func Vector2DivideV(v1, v2 Vector2) Vector2 { + return NewVector2(v1.X/v2.X, v1.Y/v2.Y) } // Vector2Normalize - Normalize provided vector -func Vector2Normalize(v rl.Vector2) rl.Vector2 { +func Vector2Normalize(v Vector2) Vector2 { return Vector2Scale(v, 1/Vector2Length(v)) } // Vector2Lerp - Calculate linear interpolation between two vectors -func Vector2Lerp(v1, v2 rl.Vector2, amount float32) rl.Vector2 { - return rl.NewVector2(v1.X+amount*(v2.X-v1.X), v1.Y+amount*(v2.Y-v1.Y)) +func Vector2Lerp(v1, v2 Vector2, amount float32) Vector2 { + return NewVector2(v1.X+amount*(v2.X-v1.X), v1.Y+amount*(v2.Y-v1.Y)) } // Vector2CrossProduct - Calculate two vectors cross product -func Vector2CrossProduct(v1, v2 rl.Vector2) float32 { +func Vector2CrossProduct(v1, v2 Vector2) float32 { return v1.X*v2.Y - v1.Y*v2.X } // Vector2Cross - Calculate the cross product of a vector and a value -func Vector2Cross(value float32, vector rl.Vector2) rl.Vector2 { - return rl.NewVector2(-value*vector.Y, value*vector.X) +func Vector2Cross(value float32, vector Vector2) Vector2 { + return NewVector2(-value*vector.Y, value*vector.X) } // Vector2LenSqr - Returns the len square root of a vector -func Vector2LenSqr(vector rl.Vector2) float32 { +func Vector2LenSqr(vector Vector2) float32 { return vector.X*vector.X + vector.Y*vector.Y } // Mat2Radians - Creates a matrix 2x2 from a given radians value -func Mat2Radians(radians float32) rl.Mat2 { +func Mat2Radians(radians float32) Mat2 { c := float32(math.Cos(float64(radians))) s := float32(math.Sin(float64(radians))) - return rl.NewMat2(c, -s, s, c) + return NewMat2(c, -s, s, c) } // Mat2Set - Set values from radians to a created matrix 2x2 -func Mat2Set(matrix *rl.Mat2, radians float32) { +func Mat2Set(matrix *Mat2, radians float32) { cos := float32(math.Cos(float64(radians))) sin := float32(math.Sin(float64(radians))) @@ -118,33 +115,33 @@ func Mat2Set(matrix *rl.Mat2, radians float32) { } // Mat2Transpose - Returns the transpose of a given matrix 2x2 -func Mat2Transpose(matrix rl.Mat2) rl.Mat2 { - return rl.NewMat2(matrix.M00, matrix.M10, matrix.M01, matrix.M11) +func Mat2Transpose(matrix Mat2) Mat2 { + return NewMat2(matrix.M00, matrix.M10, matrix.M01, matrix.M11) } // Mat2MultiplyVector2 - Multiplies a vector by a matrix 2x2 -func Mat2MultiplyVector2(matrix rl.Mat2, vector rl.Vector2) rl.Vector2 { - return rl.NewVector2(matrix.M00*vector.X+matrix.M01*vector.Y, matrix.M10*vector.X+matrix.M11*vector.Y) +func Mat2MultiplyVector2(matrix Mat2, vector Vector2) Vector2 { + return NewVector2(matrix.M00*vector.X+matrix.M01*vector.Y, matrix.M10*vector.X+matrix.M11*vector.Y) } // Vector3Zero - Vector with components value 0.0 -func Vector3Zero() rl.Vector3 { - return rl.NewVector3(0.0, 0.0, 0.0) +func Vector3Zero() Vector3 { + return NewVector3(0.0, 0.0, 0.0) } // Vector3One - Vector with components value 1.0 -func Vector3One() rl.Vector3 { - return rl.NewVector3(1.0, 1.0, 1.0) +func Vector3One() Vector3 { + return NewVector3(1.0, 1.0, 1.0) } // Vector3Add - Add two vectors -func Vector3Add(v1, v2 rl.Vector3) rl.Vector3 { - return rl.NewVector3(v1.X+v2.X, v1.Y+v2.Y, v1.Z+v2.Z) +func Vector3Add(v1, v2 Vector3) Vector3 { + return NewVector3(v1.X+v2.X, v1.Y+v2.Y, v1.Z+v2.Z) } // Vector3Multiply - Multiply vector by scalar -func Vector3Multiply(v rl.Vector3, scalar float32) rl.Vector3 { - result := rl.Vector3{} +func Vector3Multiply(v Vector3, scalar float32) Vector3 { + result := Vector3{} result.X = v.X * scalar result.Y = v.Y * scalar @@ -154,8 +151,8 @@ func Vector3Multiply(v rl.Vector3, scalar float32) rl.Vector3 { } // Vector3MultiplyV - Multiply vector by vector -func Vector3MultiplyV(v1, v2 rl.Vector3) rl.Vector3 { - result := rl.Vector3{} +func Vector3MultiplyV(v1, v2 Vector3) Vector3 { + result := Vector3{} result.X = v1.X * v2.X result.Y = v1.Y * v2.Y @@ -165,13 +162,13 @@ func Vector3MultiplyV(v1, v2 rl.Vector3) rl.Vector3 { } // Vector3Subtract - Subtract two vectors -func Vector3Subtract(v1, v2 rl.Vector3) rl.Vector3 { - return rl.NewVector3(v1.X-v2.X, v1.Y-v2.Y, v1.Z-v2.Z) +func Vector3Subtract(v1, v2 Vector3) Vector3 { + return NewVector3(v1.X-v2.X, v1.Y-v2.Y, v1.Z-v2.Z) } // Vector3CrossProduct - Calculate two vectors cross product -func Vector3CrossProduct(v1, v2 rl.Vector3) rl.Vector3 { - result := rl.Vector3{} +func Vector3CrossProduct(v1, v2 Vector3) Vector3 { + result := Vector3{} result.X = v1.Y*v2.Z - v1.Z*v2.Y result.Y = v1.Z*v2.X - v1.X*v2.Z @@ -181,19 +178,19 @@ func Vector3CrossProduct(v1, v2 rl.Vector3) rl.Vector3 { } // Vector3Perpendicular - Calculate one vector perpendicular vector -func Vector3Perpendicular(v rl.Vector3) rl.Vector3 { - result := rl.Vector3{} +func Vector3Perpendicular(v Vector3) Vector3 { + result := Vector3{} min := math.Abs(float64(v.X)) - cardinalAxis := rl.NewVector3(1.0, 0.0, 0.0) + cardinalAxis := NewVector3(1.0, 0.0, 0.0) if math.Abs(float64(v.Y)) < min { min = math.Abs(float64(v.Y)) - cardinalAxis = rl.NewVector3(0.0, 1.0, 0.0) + cardinalAxis = NewVector3(0.0, 1.0, 0.0) } if math.Abs(float64(v.Z)) < min { - cardinalAxis = rl.NewVector3(0.0, 0.0, 1.0) + cardinalAxis = NewVector3(0.0, 0.0, 1.0) } result = Vector3CrossProduct(v, cardinalAxis) @@ -202,17 +199,17 @@ func Vector3Perpendicular(v rl.Vector3) rl.Vector3 { } // Vector3Length - Calculate vector length -func Vector3Length(v rl.Vector3) float32 { +func Vector3Length(v Vector3) float32 { return float32(math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z))) } // Vector3DotProduct - Calculate two vectors dot product -func Vector3DotProduct(v1, v2 rl.Vector3) float32 { +func Vector3DotProduct(v1, v2 Vector3) float32 { return v1.X*v2.X + v1.Y*v2.Y + v1.Z*v2.Z } // Vector3Distance - Calculate distance between two vectors -func Vector3Distance(v1, v2 rl.Vector3) float32 { +func Vector3Distance(v1, v2 Vector3) float32 { dx := v2.X - v1.X dy := v2.Y - v1.Y dz := v2.Z - v1.Z @@ -221,17 +218,17 @@ func Vector3Distance(v1, v2 rl.Vector3) float32 { } // Vector3Scale - Scale provided vector -func Vector3Scale(v rl.Vector3, scale float32) rl.Vector3 { - return rl.NewVector3(v.X*scale, v.Y*scale, v.Z*scale) +func Vector3Scale(v Vector3, scale float32) Vector3 { + return NewVector3(v.X*scale, v.Y*scale, v.Z*scale) } // Vector3Negate - Negate provided vector (invert direction) -func Vector3Negate(v rl.Vector3) rl.Vector3 { - return rl.NewVector3(-v.X, -v.Y, -v.Z) +func Vector3Negate(v Vector3) Vector3 { + return NewVector3(-v.X, -v.Y, -v.Z) } // Vector3Normalize - Normalize provided vector -func Vector3Normalize(v rl.Vector3) rl.Vector3 { +func Vector3Normalize(v Vector3) Vector3 { result := v var length, ilength float32 @@ -252,8 +249,8 @@ func Vector3Normalize(v rl.Vector3) rl.Vector3 { } // Vector3Transform - Transforms a Vector3 by a given Matrix -func Vector3Transform(v rl.Vector3, mat rl.Matrix) rl.Vector3 { - result := rl.Vector3{} +func Vector3Transform(v Vector3, mat Matrix) Vector3 { + result := Vector3{} x := v.X y := v.Y @@ -267,8 +264,8 @@ func Vector3Transform(v rl.Vector3, mat rl.Matrix) rl.Vector3 { } // Vector3Lerp - Calculate linear interpolation between two vectors -func Vector3Lerp(v1, v2 rl.Vector3, amount float32) rl.Vector3 { - result := rl.Vector3{} +func Vector3Lerp(v1, v2 Vector3, amount float32) Vector3 { + result := Vector3{} result.X = v1.X + amount*(v2.X-v1.X) result.Y = v1.Y + amount*(v2.Y-v1.Y) @@ -278,12 +275,12 @@ func Vector3Lerp(v1, v2 rl.Vector3, amount float32) rl.Vector3 { } // Vector3Reflect - Calculate reflected vector to normal -func Vector3Reflect(vector, normal rl.Vector3) rl.Vector3 { +func Vector3Reflect(vector, normal Vector3) Vector3 { // I is the original vector // N is the normal of the incident plane // R = I - (2*N*( DotProduct[ I,N] )) - result := rl.Vector3{} + result := Vector3{} dotProduct := Vector3DotProduct(vector, normal) @@ -295,8 +292,8 @@ func Vector3Reflect(vector, normal rl.Vector3) rl.Vector3 { } // Vector3Min - Return min value for each pair of components -func Vector3Min(vec1, vec2 rl.Vector3) rl.Vector3 { - result := rl.Vector3{} +func Vector3Min(vec1, vec2 Vector3) Vector3 { + result := Vector3{} result.X = float32(math.Min(float64(vec1.X), float64(vec2.X))) result.Y = float32(math.Min(float64(vec1.Y), float64(vec2.Y))) @@ -306,8 +303,8 @@ func Vector3Min(vec1, vec2 rl.Vector3) rl.Vector3 { } // Vector3Max - Return max value for each pair of components -func Vector3Max(vec1, vec2 rl.Vector3) rl.Vector3 { - result := rl.Vector3{} +func Vector3Max(vec1, vec2 Vector3) Vector3 { + result := Vector3{} result.X = float32(math.Max(float64(vec1.X), float64(vec2.X))) result.Y = float32(math.Max(float64(vec1.Y), float64(vec2.Y))) @@ -317,7 +314,7 @@ func Vector3Max(vec1, vec2 rl.Vector3) rl.Vector3 { } // Vector3Barycenter - Barycenter coords for p in triangle abc -func Vector3Barycenter(p, a, b, c rl.Vector3) rl.Vector3 { +func Vector3Barycenter(p, a, b, c Vector3) Vector3 { v0 := Vector3Subtract(b, a) v1 := Vector3Subtract(c, a) v2 := Vector3Subtract(p, a) @@ -329,7 +326,7 @@ func Vector3Barycenter(p, a, b, c rl.Vector3) rl.Vector3 { denom := d00*d11 - d01*d01 - result := rl.Vector3{} + result := Vector3{} result.Y = (d11*d20 - d01*d21) / denom result.Z = (d00*d21 - d01*d20) / denom @@ -339,7 +336,7 @@ func Vector3Barycenter(p, a, b, c rl.Vector3) rl.Vector3 { } // MatrixDeterminant - Compute matrix determinant -func MatrixDeterminant(mat rl.Matrix) float32 { +func MatrixDeterminant(mat Matrix) float32 { var result float32 a00 := mat.M0 @@ -370,13 +367,13 @@ func MatrixDeterminant(mat rl.Matrix) float32 { } // MatrixTrace - Returns the trace of the matrix (sum of the values along the diagonal) -func MatrixTrace(mat rl.Matrix) float32 { +func MatrixTrace(mat Matrix) float32 { return mat.M0 + mat.M5 + mat.M10 + mat.M15 } // MatrixTranspose - Transposes provided matrix -func MatrixTranspose(mat rl.Matrix) rl.Matrix { - var result rl.Matrix +func MatrixTranspose(mat Matrix) Matrix { + var result Matrix result.M0 = mat.M0 result.M1 = mat.M4 @@ -399,8 +396,8 @@ func MatrixTranspose(mat rl.Matrix) rl.Matrix { } // MatrixInvert - Invert provided matrix -func MatrixInvert(mat rl.Matrix) rl.Matrix { - var result rl.Matrix +func MatrixInvert(mat Matrix) Matrix { + var result Matrix a00 := mat.M0 a01 := mat.M1 @@ -456,8 +453,8 @@ func MatrixInvert(mat rl.Matrix) rl.Matrix { } // MatrixNormalize - Normalize provided matrix -func MatrixNormalize(mat rl.Matrix) rl.Matrix { - var result rl.Matrix +func MatrixNormalize(mat Matrix) Matrix { + var result Matrix det := MatrixDeterminant(mat) @@ -482,8 +479,8 @@ func MatrixNormalize(mat rl.Matrix) rl.Matrix { } // MatrixIdentity - Returns identity matrix -func MatrixIdentity() rl.Matrix { - return rl.NewMatrix( +func MatrixIdentity() Matrix { + return NewMatrix( 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, @@ -491,7 +488,7 @@ func MatrixIdentity() rl.Matrix { } // MatrixAdd - Add two matrices -func MatrixAdd(left, right rl.Matrix) rl.Matrix { +func MatrixAdd(left, right Matrix) Matrix { result := MatrixIdentity() result.M0 = left.M0 + right.M0 @@ -515,7 +512,7 @@ func MatrixAdd(left, right rl.Matrix) rl.Matrix { } // MatrixSubtract - Subtract two matrices (left - right) -func MatrixSubtract(left, right rl.Matrix) rl.Matrix { +func MatrixSubtract(left, right Matrix) Matrix { result := MatrixIdentity() result.M0 = left.M0 - right.M0 @@ -539,8 +536,8 @@ func MatrixSubtract(left, right rl.Matrix) rl.Matrix { } // MatrixTranslate - Returns translation matrix -func MatrixTranslate(x, y, z float32) rl.Matrix { - return rl.NewMatrix( +func MatrixTranslate(x, y, z float32) Matrix { + return NewMatrix( 1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, @@ -548,8 +545,8 @@ func MatrixTranslate(x, y, z float32) rl.Matrix { } // MatrixRotate - Returns rotation matrix for an angle around an specified axis (angle in radians) -func MatrixRotate(axis rl.Vector3, angle float32) rl.Matrix { - var result rl.Matrix +func MatrixRotate(axis Vector3, angle float32) Matrix { + var result Matrix mat := MatrixIdentity() @@ -617,7 +614,7 @@ func MatrixRotate(axis rl.Vector3, angle float32) rl.Matrix { } // MatrixRotateX - Returns x-rotation matrix (angle in radians) -func MatrixRotateX(angle float32) rl.Matrix { +func MatrixRotateX(angle float32) Matrix { result := MatrixIdentity() cosres := float32(math.Cos(float64(angle))) @@ -632,7 +629,7 @@ func MatrixRotateX(angle float32) rl.Matrix { } // MatrixRotateY - Returns y-rotation matrix (angle in radians) -func MatrixRotateY(angle float32) rl.Matrix { +func MatrixRotateY(angle float32) Matrix { result := MatrixIdentity() cosres := float32(math.Cos(float64(angle))) @@ -647,7 +644,7 @@ func MatrixRotateY(angle float32) rl.Matrix { } // MatrixRotateZ - Returns z-rotation matrix (angle in radians) -func MatrixRotateZ(angle float32) rl.Matrix { +func MatrixRotateZ(angle float32) Matrix { result := MatrixIdentity() cosres := float32(math.Cos(float64(angle))) @@ -662,8 +659,8 @@ func MatrixRotateZ(angle float32) rl.Matrix { } // MatrixScale - Returns scaling matrix -func MatrixScale(x, y, z float32) rl.Matrix { - result := rl.NewMatrix( +func MatrixScale(x, y, z float32) Matrix { + result := NewMatrix( x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, @@ -673,8 +670,8 @@ func MatrixScale(x, y, z float32) rl.Matrix { } // MatrixMultiply - Returns two matrix multiplication -func MatrixMultiply(left, right rl.Matrix) rl.Matrix { - var result rl.Matrix +func MatrixMultiply(left, right Matrix) Matrix { + var result Matrix result.M0 = right.M0*left.M0 + right.M1*left.M4 + right.M2*left.M8 + right.M3*left.M12 result.M1 = right.M0*left.M1 + right.M1*left.M5 + right.M2*left.M9 + right.M3*left.M13 @@ -697,8 +694,8 @@ func MatrixMultiply(left, right rl.Matrix) rl.Matrix { } // MatrixFrustum - Returns perspective projection matrix -func MatrixFrustum(left, right, bottom, top, near, far float32) rl.Matrix { - var result rl.Matrix +func MatrixFrustum(left, right, bottom, top, near, far float32) Matrix { + var result Matrix rl := right - left tb := top - bottom @@ -728,16 +725,16 @@ func MatrixFrustum(left, right, bottom, top, near, far float32) rl.Matrix { } // MatrixPerspective - Returns perspective projection matrix -func MatrixPerspective(fovy, aspect, near, far float32) rl.Matrix { - top := near * float32(math.Tan(float64(fovy*rl.Pi)/360.0)) +func MatrixPerspective(fovy, aspect, near, far float32) Matrix { + top := near * float32(math.Tan(float64(fovy*Pi)/360.0)) right := top * aspect return MatrixFrustum(-right, right, -top, top, near, far) } // MatrixOrtho - Returns orthographic projection matrix -func MatrixOrtho(left, right, bottom, top, near, far float32) rl.Matrix { - var result rl.Matrix +func MatrixOrtho(left, right, bottom, top, near, far float32) Matrix { + var result Matrix rl := right - left tb := top - bottom @@ -764,8 +761,8 @@ func MatrixOrtho(left, right, bottom, top, near, far float32) rl.Matrix { } // MatrixLookAt - Returns camera look-at matrix (view matrix) -func MatrixLookAt(eye, target, up rl.Vector3) rl.Matrix { - var result rl.Matrix +func MatrixLookAt(eye, target, up Vector3) Matrix { + var result Matrix z := Vector3Subtract(eye, target) z = Vector3Normalize(z) @@ -795,13 +792,13 @@ func MatrixLookAt(eye, target, up rl.Vector3) rl.Matrix { } // QuaternionLength - Compute the length of a quaternion -func QuaternionLength(quat rl.Quaternion) float32 { +func QuaternionLength(quat Quaternion) float32 { return float32(math.Sqrt(float64(quat.X*quat.X + quat.Y*quat.Y + quat.Z*quat.Z + quat.W*quat.W))) } // QuaternionNormalize - Normalize provided quaternion -func QuaternionNormalize(q rl.Quaternion) rl.Quaternion { - var result rl.Quaternion +func QuaternionNormalize(q Quaternion) Quaternion { + var result Quaternion var length, ilength float32 @@ -822,7 +819,7 @@ func QuaternionNormalize(q rl.Quaternion) rl.Quaternion { } // QuaternionInvert - Invert provided quaternion -func QuaternionInvert(quat rl.Quaternion) rl.Quaternion { +func QuaternionInvert(quat Quaternion) Quaternion { result := quat length := QuaternionLength(quat) @@ -841,8 +838,8 @@ func QuaternionInvert(quat rl.Quaternion) rl.Quaternion { } // QuaternionMultiply - Calculate two quaternion multiplication -func QuaternionMultiply(q1, q2 rl.Quaternion) rl.Quaternion { - var result rl.Quaternion +func QuaternionMultiply(q1, q2 Quaternion) Quaternion { + var result Quaternion qax := q1.X qay := q1.Y @@ -862,8 +859,8 @@ func QuaternionMultiply(q1, q2 rl.Quaternion) rl.Quaternion { } // QuaternionSlerp - Calculates spherical linear interpolation between two quaternions -func QuaternionSlerp(q1, q2 rl.Quaternion, amount float32) rl.Quaternion { - var result rl.Quaternion +func QuaternionSlerp(q1, q2 Quaternion, amount float32) Quaternion { + var result Quaternion cosHalfTheta := q1.X*q2.X + q1.Y*q2.Y + q1.Z*q2.Z + q1.W*q2.W @@ -893,8 +890,8 @@ func QuaternionSlerp(q1, q2 rl.Quaternion, amount float32) rl.Quaternion { } // QuaternionFromMatrix - Returns a quaternion for a given rotation matrix -func QuaternionFromMatrix(matrix rl.Matrix) rl.Quaternion { - var result rl.Quaternion +func QuaternionFromMatrix(matrix Matrix) Quaternion { + var result Quaternion trace := MatrixTrace(matrix) @@ -942,8 +939,8 @@ func QuaternionFromMatrix(matrix rl.Matrix) rl.Quaternion { } // QuaternionToMatrix - Returns a matrix for a given quaternion -func QuaternionToMatrix(q rl.Quaternion) rl.Matrix { - var result rl.Matrix +func QuaternionToMatrix(q Quaternion) Matrix { + var result Matrix x := q.X y := q.Y @@ -987,8 +984,8 @@ func QuaternionToMatrix(q rl.Quaternion) rl.Matrix { } // QuaternionFromAxisAngle - Returns rotation quaternion for an angle and axis -func QuaternionFromAxisAngle(axis rl.Vector3, angle float32) rl.Quaternion { - result := rl.NewQuaternion(0.0, 0.0, 0.0, 1.0) +func QuaternionFromAxisAngle(axis Vector3, angle float32) Quaternion { + result := NewQuaternion(0.0, 0.0, 0.0, 1.0) if Vector3Length(axis) != 0.0 { angle *= 0.5 @@ -1010,12 +1007,12 @@ func QuaternionFromAxisAngle(axis rl.Vector3, angle float32) rl.Quaternion { } // QuaternionToAxisAngle - Returns the rotation angle and axis for a given quaternion -func QuaternionToAxisAngle(q rl.Quaternion, outAxis *rl.Vector3, outAngle *float32) { +func QuaternionToAxisAngle(q Quaternion, outAxis *Vector3, outAngle *float32) { if math.Abs(float64(q.W)) > 1.0 { q = QuaternionNormalize(q) } - resAxis := rl.NewVector3(0.0, 0.0, 0.0) + resAxis := NewVector3(0.0, 0.0, 0.0) resAngle := 2.0 * float32(math.Acos(float64(q.W))) den := float32(math.Sqrt(float64(1.0 - q.W*q.W))) @@ -1035,8 +1032,8 @@ func QuaternionToAxisAngle(q rl.Quaternion, outAxis *rl.Vector3, outAngle *float } // QuaternionTransform - Transform a quaternion given a transformation matrix -func QuaternionTransform(q rl.Quaternion, mat rl.Matrix) rl.Quaternion { - var result rl.Quaternion +func QuaternionTransform(q Quaternion, mat Matrix) Quaternion { + var result Quaternion x := q.X y := q.Y diff --git a/raymath/README.md b/raymath/README.md deleted file mode 100644 index f8099cc..0000000 --- a/raymath/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## raymath [![GoDoc](https://godoc.org/github.com/gen2brain/raylib-go/raymath?status.svg)](https://godoc.org/github.com/gen2brain/raylib-go/raymath) - -Some useful functions to work with Vector2, Vector3, Matrix and Quaternions.