How to multiply vectors as in mono

Hello. Sorry for the stupid question, but how to do arithmetic operations with vectors as in mono. Perhaps you can somehow expand the list of standard vector methods or something.

let vec1: BABYLON.Vector3 = new BABYLON.Vector3(1, 2, 3);
let vec2: BABYLON.Vector3 = new BABYLON.Vector3(3, 2, 1);
let vec3: BABYLON.Vector3 = new BABYLON.Vector3(2, 3, 1);

let result = vec1 * vec2 * vec3 + 1; //mono

let result = vec1.multiply(vec2).multiply(vec3).add(1 /*?*/); //babylon

Hi @alex2006 and welcome to the forum. There are a number of vector operations you can carry out. The format may different that you are use to.

https://doc.babylonjs.com/api/classes/babylon.vector3

For multiplication

let result  = vec1.multiply(vec2);
vec1.multiplyInPlace(vec2);
vec1.multiplyToRef(vec2, result);

This thread might be useful as well What are the "...toRef" Methods? How Should We Use Them?

2 Likes

Thank you answer. Could you show how such a code would look using the babylon’s vectors?

I think I don’t understand something, because I get something that looks awful.

private BezierTo4Point(t: number, p0: BABYLON.Vector3, p1: BABYLON.Vector3, p2: BABYLON.Vector3, p3: BABYLON.Vector3): BABYLON.Vector3 {
    	let a = BABYLON.Vector3.Zero;
    	let d = 1 - t;

            // how?
    	a += p0 * d * d * d;
    	a += p1 * t * 3 * d * d;
    	a += p2 * 3 * t * t * d;
    	return a + p3 * t * t * t;
    }

Thanks, I seem to have done.

let result = new BABYLON.Vector3();
let d = 1 - t;
p0.scaleAndAddToRef(d * d * d, result);
p1.scaleAndAddToRef(t * 3 * d * d, result);
p2.scaleAndAddToRef(3 * t * t * d, result);
p3.scaleAndAddToRef(t * t * t, result);
return result;

This is exactly what @JohnK showed you and yes it looks awfull but as there are not operator overload in JS you need to call functions instead.

1 Like

For scalar multiplication you need Vector3 - Babylon.js Documentation.

let result  = vec1.scale(d);
vec1.scaleInPlace(d);
vec1.scaleToRef(d, result);
2 Likes

Since you are looking at Bezier curves you might be interested in this Draw Curves - Babylon.js Documentation

2 Likes

Here is a way to make it more mono-like, it takes the simple code and turns it into BJS code, which you can then eval:

https://playground.babylonjs.com/#HNJEE0#2

vec1 += vec1 * vec2 * vec3 + 55

becomes

vec1.addInPlace(vec1.multiply(vec2).multiplyInPlace(vec3).addInPlaceFromFloats(55,55,55))

Not great for production builds though, but you can use it as a standalone tool or make a script post-processor that replaces the eval lines with the expanded code.

2 Likes

I have this code in mono

 this._smoothR = Quaternion.RotateTowards(
 				this._smoothR, 
 				this._rotation, 
 				Quaternion.Angle(this._rotation, this._smoothR) * delta
 			);

I tried to translate it, but did not find the Quaternion.Angle and RotateTowards functions in the babylonjs library. Perhaps they are called somehow differently?

public static AngleTo(a: BABYLON.Quaternion, b: BABYLON.Quaternion): number {
    return ??
}

public static RotateTowards(from: BABYLON.Quaternion, to: BABYLON.Quaternion, maxDegreesDelta: number): BABYLON.Quaternion {
    let num: number = AngleTo(from, to);
    if (num == 0.0) {
        return to;
    }

    let amount: number = Math.min(1.0, maxDegreesDelta / num);
    return BABYLON.Quaternion.Slerp(from, to, amount);
}

Be careful that as you don’t handle operator priority, some expressions won’t be evaluated correctly.

For eg:

  • vec1 += vec1 * vec2 + vec3 = (6, 9, 7) ==> correct
  • vec1 += vec3 + vec1 * vec2 = (10, 12, 7) ==> incorrect
2 Likes