From afbd1dfd68f3b485239fd24ec7a2c9840bcb12bd Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 16 Oct 2024 23:39:07 +0200 Subject: [PATCH] improved Vector stuff --- src/lib/BlockPos.hx | 54 +++++----------------------- src/lib/ScreenPos.hx | 28 +++++---------- src/lib/Vec.hx | 86 ++++++++++++++++++++++++++++++++++++++++++-- src/lib/WorldPos.hx | 62 +++++++------------------------- 4 files changed, 113 insertions(+), 117 deletions(-) diff --git a/src/lib/BlockPos.hx b/src/lib/BlockPos.hx index a82f3cd..691852a 100644 --- a/src/lib/BlockPos.hx +++ b/src/lib/BlockPos.hx @@ -7,46 +7,30 @@ import lib.Vec.Vec3; Basicly a wrapper for Vec3 with some extra functions. `Y` represents the height of the point. **/ -@:forward(x, y, z) +@:forward abstract BlockPos(Vec3) from Vec3 to Vec3 { public inline function new(x:Int, y:Int, z:Int) { this = new Vec3(x, y, z); } @:op(A + B) - public function add(rhs:BlockPos):BlockPos { - return { - y: this.y + rhs.y, - x: this.x + rhs.x, - z: this.z + rhs.z - }; + public inline function add(rhs:BlockPos):BlockPos { + return this.add(rhs); } @:op(A - B) - public function sub(rhs:BlockPos):BlockPos { - return { - y: this.y - rhs.y, - x: this.x - rhs.x, - z: this.z - rhs.z - }; + public inline function sub(rhs:BlockPos):BlockPos { + return this.sub(rhs); } @:op(A * B) - public function multiplyScalar(rhs:Int):BlockPos { - return { - y: this.y * rhs, - x: this.x * rhs, - z: this.z * rhs - }; + public inline function multiplyScalar(rhs:Int):BlockPos { + return this.multiplyScalar(rhs); } @:op(-A) - public function negate():BlockPos { - return { - y: -this.y, - x: -this.x, - z: -this.z - }; + public inline function negate():BlockPos { + return this.negate(); } @:op(A == B) @@ -59,22 +43,6 @@ abstract BlockPos(Vec3) from Vec3 to Vec3 { return !equals(rhs); } - public function dot(rhs:BlockPos):Float { - return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z; - } - - public function cross(rhs:BlockPos):BlockPos { - return { - x: this.x * rhs.y - this.y * rhs.x, - y: this.y * rhs.z - this.z * rhs.y, - z: this.z * rhs.x - this.x * rhs.z, - }; - } - - public function length():Float { - return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); - } - public function hashCode():Int { return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791); } @@ -83,10 +51,6 @@ abstract BlockPos(Vec3) from Vec3 to Vec3 { return 'BlockPos(${this.x}, ${this.y}, ${this.z})'; } - public function distance(rhs:BlockPos):Float { - return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2)); - } - /** Returns a list of positions neighboring the block. No Diagonal. **/ diff --git a/src/lib/ScreenPos.hx b/src/lib/ScreenPos.hx index f1cae0d..9adce2a 100644 --- a/src/lib/ScreenPos.hx +++ b/src/lib/ScreenPos.hx @@ -13,34 +13,22 @@ abstract ScreenPos(Vec2) from Vec2 to Vec2 { } @:op(A + B) - public function add(rhs:Vec2):ScreenPos { - return new ScreenPos({ - y: this.y + rhs.y, - x: this.x + rhs.x, - }); + public inline function add(rhs:Vec2):ScreenPos { + return this.add(rhs); } @:op(A - B) - public function sub(rhs:Vec2):ScreenPos { - return new ScreenPos({ - y: this.y - rhs.y, - x: this.x - rhs.x, - }); + public inline function sub(rhs:Vec2):ScreenPos { + return this.sub(rhs); } @:op(A * B) - public function multiply(rhs:Vec2):ScreenPos { - return new ScreenPos({ - y: this.y * rhs.y, - x: this.x * rhs.x, - }); + public inline function multiply(rhs:Vec2):ScreenPos { + return this.multiply(rhs); } @:op(-A) - public function negate():ScreenPos { - return new ScreenPos({ - y: -this.y, - x: -this.x, - }); + public inline function negate():ScreenPos { + return this.negate(); } } diff --git a/src/lib/Vec.hx b/src/lib/Vec.hx index 414d31f..6af541d 100644 --- a/src/lib/Vec.hx +++ b/src/lib/Vec.hx @@ -8,16 +8,96 @@ package lib; this.x = x; this.y = y; } + + public function add(rhs:Vec2):Vec2 { + return { + y: this.y + rhs.y, + x: this.x + rhs.x, + }; + } + + public function sub(rhs:Vec2):Vec2 { + return { + y: this.y - rhs.y, + x: this.x - rhs.x, + }; + } + + public function multiply(rhs:Vec2):Vec2 { + return { + y: this.y * rhs.y, + x: this.x * rhs.x, + }; + } + + public function negate():Vec2 { + return { + y: -this.y, + x: -this.x, + }; + } } @:structInit class Vec3 { - public var x:T; - public var y:T; - public var z:T; + public final x:T; + public final y:T; + public final z:T; public function new(x:T, y:T, z:T) { this.x = x; this.y = y; this.z = z; } + + public function add(rhs:Vec3):Vec3 { + return { + x: this.x + rhs.x, + y: this.y + rhs.y, + z: this.z + rhs.z + }; + } + + public function sub(rhs:Vec3):Vec3 { + return { + x: this.x - rhs.x, + y: this.y - rhs.y, + z: this.z - rhs.z + }; + } + + public function multiplyScalar(rhs:T):Vec3 { + return { + x: this.x * rhs, + y: this.y * rhs, + z: this.z * rhs + }; + } + + public function negate():Vec3 { + return { + x: -this.x, + y: -this.y, + z: -this.z + }; + } + + public function dot(rhs:Vec3):Float { + return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z; + } + + public function cross(rhs:Vec3):Vec3 { + return { + x: this.y * rhs.z - this.z * rhs.y, + y: this.z * rhs.x - this.x * rhs.z, + z: this.x * rhs.y - this.y * rhs.x + }; + } + + public function length():Float { + return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); + } + + public function distance(rhs:Vec3):Float { + return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2)); + } } diff --git a/src/lib/WorldPos.hx b/src/lib/WorldPos.hx index f5b1006..b99fe7a 100644 --- a/src/lib/WorldPos.hx +++ b/src/lib/WorldPos.hx @@ -7,78 +7,46 @@ import lib.Vec.Vec3; Basicly a wrapper for Vec3 with some extra functions. `Y` represents the height of the point. **/ -@:forward(x, y, z) +@:forward abstract WorldPos(Vec3) from Vec3 to Vec3 { - inline public function new(i:Vec3) { - this = i; + public inline function new(v:Vec3) { + this = v; } @:op(A + B) - public function add(rhs:Vec3):WorldPos { - return new WorldPos({ - y: this.y + rhs.y, - x: this.x + rhs.x, - z: this.z + rhs.z - }); + public inline function add(rhs:WorldPos):WorldPos { + return this.add(rhs); } @:op(A - B) - public function sub(rhs:Vec3):WorldPos { - return new WorldPos({ - y: this.y - rhs.y, - x: this.x - rhs.x, - z: this.z - rhs.z - }); + public inline function sub(rhs:Vec3):WorldPos { + return this.sub(rhs); } @:op(A * B) - public function multiplyScalar(rhs:Float):WorldPos { - return new WorldPos({ - y: this.y * rhs, - x: this.x * rhs, - z: this.z * rhs - }); + public inline function multiplyScalar(rhs:Float):WorldPos { + return this.multiplyScalar(rhs); } @:op(A / B) public function divideScalar(rhs:Float):WorldPos { - return new WorldPos({ + return { y: this.y / rhs, x: this.x / rhs, z: this.z / rhs - }); + }; } @:op(-A) public function negate():WorldPos { - return new WorldPos({ - y: -this.y, - x: -this.x, - z: -this.z - }); - } - - public function dot(rhs:Vec3):Float { - return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z; - } - - public function cross(rhs:Vec3):WorldPos { - return new WorldPos({ - x: this.y * rhs.z - this.z * rhs.y, - y: this.z * rhs.x - this.x * rhs.z, - z: this.x * rhs.y - this.y * rhs.x - }); + return this.negate(); } public function normalize():WorldPos { - var l = length(); + var l = this.length(); return multiplyScalar(1 / l); } - public function length():Float { - return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); - } - @:op(A == B) public function equals(rhs:WorldPos):Bool { return close(rhs); @@ -104,8 +72,4 @@ abstract WorldPos(Vec3) from Vec3 to Vec3 { z: Math.fround(this.z) }); } - - public function distance(rhs:WorldPos):Float { - return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2)); - } }