nicklockwood / vectormath Goto Github PK
View Code? Open in Web Editor NEWA Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based graphics
License: MIT License
A Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based graphics
License: MIT License
(lldb) po Quaternion(rotationMatrix: Matrix4.identity)
โฟ Quaternion
- x : nan
- y : nan
- z : 0.0
- w : nan
Correct me if I'm wrong or midunderstangind something, but I think the order of matrix multiplication of VectorMath library seems reversed.
public static func *(lhs: Matrix3, rhs: Matrix3) -> Matrix3 {
return Matrix3(
lhs.m11 * rhs.m11 + lhs.m21 * rhs.m12 + lhs.m31 * rhs.m13,
.....
}
public static func *(lhs: Matrix4, rhs: Matrix4) -> Matrix4 {
var m = Matrix4.identity
m.m11 = lhs.m11 * rhs.m11 + lhs.m21 * rhs.m12
m.m11 += lhs.m31 * rhs.m13 + lhs.m41 * rhs.m14
....
}
According to this, algorithm of two matrices is something like this:
where I believe VectorMath's *
operator represents AB
in this equation. (meaning you can write A * B
in swift to do same calculation.) If so, the function's lhs
and rhs
is reversed and since Matrix multiplication is not commutative, it'll produce different results.
I did write a simple Matrix4 <-> CATransform3D conversion code and check something like this:
let m1 = Matrix4( /* init with some values */)
let m2 = Matrix4( /* init with some other values */)
let con1 = CATransform3DConcat(CATransform3D(m1), CATransform3D(m2) )
let con2 = m1 * m2
XCTAssertTrue(Matrix4(con1) == con2) // this should pass, but fails
let con3 = m2 * m1
XCTAssertFalse(Matrix4(con1) == con3) // this should pass, but fails
Here's what CATransform3DConcat
s document says:
Concatenate 'b' to 'a' and return the result: t' = a * b.
I think it would be better to have reversed order for those methods.
Hey, I'd love to see this lib available in Swift 3 so I can implement it in my project.
Thanks ๐
I'd be cool to also add Cocoapods support. I done this implementation myself last month for my library. Just follow this post and this checklist.
func normalized() -> Vector4 {
let lengthSquared = self.lengthSquared
if lengthSquared ~= 0 || lengthSquared ~= 1 {
return self
}
return self / sqrt(lengthSquared)
}
should be " lengthSquared == 0 || lengthSquared == 1 "
I ran tests on OSX 10.11.1 but couldn't compile
put the "public" keyword before the extension declarations, so that they export properly from bundle to bundle/app
example:
public extension SCNVector3 {
init(_ v: Vector3) {
self.init(x: SCNFloat(v.x), y: SCNFloat(v.y), z: SCNFloat(v.z))
}
}
Insert the following testing code to testEulerConversion
test case and observe it to fail
let quat2 = Quaternion(pitch: 0.12334412, yaw: 1.3521468, roll: -0.53435323)
let (pitch, yaw, roll) = quat2.toPitchYawRoll()
let quat2Ref = Quaternion(pitch: pitch, yaw: yaw, roll: roll)
XCTAssertTrue(quat2 ~= quat2Ref)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.