improved Vector stuff

This commit is contained in:
Niklas Kapelle 2024-10-16 23:39:07 +02:00
parent b305594ea4
commit afbd1dfd68
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
4 changed files with 113 additions and 117 deletions

View File

@ -7,46 +7,30 @@ import lib.Vec.Vec3;
Basicly a wrapper for Vec3<Int> with some extra functions.
`Y` represents the height of the point.
**/
@:forward(x, y, z)
@:forward
abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
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<Int>) from Vec3<Int> to Vec3<Int> {
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<Int>) from Vec3<Int> to Vec3<Int> {
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.
**/

View File

@ -13,34 +13,22 @@ abstract ScreenPos(Vec2<Int>) from Vec2<Int> to Vec2<Int> {
}
@:op(A + B)
public function add(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({
y: this.y + rhs.y,
x: this.x + rhs.x,
});
public inline function add(rhs:Vec2<Int>):ScreenPos {
return this.add(rhs);
}
@:op(A - B)
public function sub(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({
y: this.y - rhs.y,
x: this.x - rhs.x,
});
public inline function sub(rhs:Vec2<Int>):ScreenPos {
return this.sub(rhs);
}
@:op(A * B)
public function multiply(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({
y: this.y * rhs.y,
x: this.x * rhs.x,
});
public inline function multiply(rhs:Vec2<Int>):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();
}
}

View File

@ -8,16 +8,96 @@ package lib;
this.x = x;
this.y = y;
}
public function add(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y + rhs.y,
x: this.x + rhs.x,
};
}
public function sub(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y - rhs.y,
x: this.x - rhs.x,
};
}
public function multiply(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y * rhs.y,
x: this.x * rhs.x,
};
}
public function negate():Vec2<T> {
return {
y: -this.y,
x: -this.x,
};
}
}
@:structInit class Vec3<T:Float> {
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<T>):Vec3<T> {
return {
x: this.x + rhs.x,
y: this.y + rhs.y,
z: this.z + rhs.z
};
}
public function sub(rhs:Vec3<T>):Vec3<T> {
return {
x: this.x - rhs.x,
y: this.y - rhs.y,
z: this.z - rhs.z
};
}
public function multiplyScalar(rhs:T):Vec3<T> {
return {
x: this.x * rhs,
y: this.y * rhs,
z: this.z * rhs
};
}
public function negate():Vec3<T> {
return {
x: -this.x,
y: -this.y,
z: -this.z
};
}
public function dot(rhs:Vec3<T>):Float {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
public function cross(rhs:Vec3<T>):Vec3<T> {
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<T>):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));
}
}

View File

@ -7,78 +7,46 @@ import lib.Vec.Vec3;
Basicly a wrapper for Vec3<Float> with some extra functions.
`Y` represents the height of the point.
**/
@:forward(x, y, z)
@:forward
abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
inline public function new(i:Vec3<Float>) {
this = i;
public inline function new(v:Vec3<Float>) {
this = v;
}
@:op(A + B)
public function add(rhs:Vec3<Float>):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<Float>):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<Float>):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>):Float {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
public function cross(rhs:Vec3<Float>):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<Float>) from Vec3<Float> to Vec3<Float> {
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));
}
}