How to find directional velocity from x and y velocity

Hello, I am trying to find the forward velocity of an object. However, the extractor block only provides x and y velocity. I thought it would be no big deal, and that I could just use the pythagorean theorem. I used an expression block:
Math.sqrt((X^2) + (Y^2))
The total velocity was zero, so I thought it had to do with how the Y was negative, even though a negative squared is positive. I put Math.abs() around the (X^2) and (Y^2). It worked, but the velocity seemed to be almost random, other than that it fluctuated around the square root of the Y velocity (note that I was just moving up in order to test it, so there was no X). But it would fluctuate, even though the acceleration was constant. The eval input of the expression block was connecting to always. It also hardly updated.

Does anyone know why it is doing that, or even better, alternative ways to find the total velocity?

2 Likes

you would want to use distance formula
or this distance bundle that is used by pretty much anyone
next time just use the magnifying glass in the top right and search for distance formula.
here’s my speedometer

{"data":{"behavior":{"v":"2","nodes":[{"inputCount":1,"outputCount":1,"name":"Speedometer","behaviorType":"logic.NodeGroup","x":-450,"y":928,"group":"756f33eafe2a1f48","id":"771fcdf8bcf1694c","notes":"Modes:\n1 - Linear (Starting mode)\n2 - Vertical\n3 - Horizontal\n4 - Rotational","n_o":1,"isMenuItem":false},{"inputCount":0,"outputCount":1,"name":"Bundle Input","behaviorType":"logic.NodeGroupInput","x":-720,"y":64,"group":"771fcdf8bcf1694c","id":"771fcdf9ffb89c4a","portId":"771fcdf8bcf1694ci0","tag":"Mode","dataType":2},{"inputCount":1,"outputCount":0,"name":"Bundle Output","behaviorType":"logic.NodeGroupOutput","x":180,"y":96,"group":"771fcdf8bcf1694c","id":"771fcdf97da47446","portId":"771fcdf8bcf1694co0","tag":"Speed","dataType":2},{"gateVal":1,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":-450,"y":32,"group":"771fcdf8bcf1694c","id":"771fcdf918ec6f47"},{"inputCount":0,"outputCount":1,"name":"Always","behaviorType":"logic.triggers.Always","x":-720,"y":-224,"group":"771fcdf8bcf1694c","id":"771fcdf9206d564f"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-450,"y":-192,"group":"771fcdf8bcf1694c","id":"771fcdf9a95e3946","targetId":0,"prop":"y velocity"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-450,"y":-320,"group":"771fcdf8bcf1694c","id":"771fcdf9e8d8924a","targetId":0,"prop":"x velocity"},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":-90,"y":-96,"group":"771fcdf8bcf1694c","id":"771fcdf9e6cc774b","_startVal":1,"dataType":2},{"gateVal":4,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":-450,"y":320,"group":"771fcdf8bcf1694c","id":"771fcdfb06df7145"},{"gateVal":3,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":-450,"y":224,"group":"771fcdf8bcf1694c","id":"771fcdfb8dba5441"},{"gateVal":2,"mode":"equal to","inputCount":2,"outputCount":2,"name":"Filter","behaviorType":"logic.logic.Filter","x":-450,"y":128,"group":"771fcdf8bcf1694c","id":"771fcdfb64d98c42"},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":-90,"y":64,"group":"771fcdf8bcf1694c","id":"771fcdfc85d2f043","_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":-90,"y":160,"group":"771fcdf8bcf1694c","id":"771fcdfc5165a043","_startVal":0,"dataType":2},{"inputCount":3,"outputCount":1,"name":"Switch","behaviorType":"logic.logic.Switch","x":180,"y":224,"group":"771fcdf8bcf1694c","id":"771fcdfca2dc4d49","_startVal":0,"dataType":2},{"inputCount":0,"outputCount":1,"name":"Always","behaviorType":"logic.triggers.Always","x":-270,"y":448,"group":"771fcdf8bcf1694c","id":"771fcdfc7e80234e"},{"delay":0,"count":1,"version":2,"inputCount":3,"outputCount":1,"name":"Timer","behaviorType":"logic.triggers.Timer","x":-270,"y":352,"group":"771fcdf8bcf1694c","id":"771fcdfc02e8e048"},{"expression":"(b-a)","default0":0,"default1":0,"default2":0,"default3":0,"default4":0,"default5":0,"params":2,"version":2,"tag":"","inputCount":3,"outputCount":1,"name":"Expression","behaviorType":"logic.logic.Expression","x":90,"y":448,"group":"771fcdf8bcf1694c","id":"771fcdfccb584c40"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-90,"y":448,"group":"771fcdf8bcf1694c","id":"771fcdfc7c924045","targetId":0,"prop":"rotation"},{"inputCount":1,"outputCount":1,"name":"Extractor","behaviorType":"logic.properties.Extractor","x":-90,"y":352,"group":"771fcdf8bcf1694c","id":"771fcdfcbafbc74b","targetId":0,"prop":"rotation"},{"expression":"Math.sqrt(Math.pow(A,2) + Math.pow(B, 2))","default0":0,"default1":0,"default2":0,"default3":0,"default4":0,"default5":0,"params":2,"version":2,"tag":"","inputCount":3,"outputCount":1,"name":"Expression","behaviorType":"logic.logic.Expression","x":-180,"y":-256,"group":"771fcdf8bcf1694c","id":"7797b10ea5803a4d"},{"expression":"Math.round(A*10)/10","default0":0,"default1":0,"default2":0,"default3":0,"default4":0,"default5":0,"params":1,"version":2,"tag":"","inputCount":2,"outputCount":1,"name":"Expression","behaviorType":"logic.logic.Expression","x":0,"y":-256,"group":"771fcdf8bcf1694c","id":"7797b10ef648b84e"}],"links":[{"input_id":"771fcdf918ec6f47i0","output_id":"771fcdf9ffb89c4ao0"},{"input_id":"771fcdfb64d98c42i0","output_id":"771fcdf9ffb89c4ao0"},{"input_id":"771fcdfb8dba5441i0","output_id":"771fcdf9ffb89c4ao0"},{"input_id":"771fcdfb06df7145i0","output_id":"771fcdf9ffb89c4ao0"},{"input_id":"771fcdf9e6cc774bi1","output_id":"771fcdf918ec6f47o0"},{"input_id":"771fcdf9e6cc774bi0","output_id":"771fcdf918ec6f47o1"},{"input_id":"771fcdf9e8d8924ai0","output_id":"771fcdf9206d564fo0"},{"input_id":"771fcdf9a95e3946i0","output_id":"771fcdf9206d564fo0"},{"input_id":"7797b10ea5803a4di1","output_id":"771fcdf9a95e3946o0"},{"input_id":"7797b10ea5803a4di2","output_id":"771fcdf9a95e3946o0"},{"input_id":"771fcdfc85d2f043i2","output_id":"771fcdf9a95e3946o0"},{"input_id":"7797b10ea5803a4di0","output_id":"771fcdf9e8d8924ao0"},{"input_id":"771fcdfc5165a043i2","output_id":"771fcdf9e8d8924ao0"},{"input_id":"771fcdf97da47446i0","output_id":"771fcdf9e6cc774bo0"},{"input_id":"771fcdfca2dc4d49i1","output_id":"771fcdfb06df7145o0"},{"input_id":"771fcdfca2dc4d49i0","output_id":"771fcdfb06df7145o1"},{"input_id":"771fcdfc5165a043i1","output_id":"771fcdfb8dba5441o0"},{"input_id":"771fcdfc5165a043i0","output_id":"771fcdfb8dba5441o1"},{"input_id":"771fcdfc85d2f043i1","output_id":"771fcdfb64d98c42o0"},{"input_id":"771fcdfc85d2f043i0","output_id":"771fcdfb64d98c42o1"},{"input_id":"771fcdf97da47446i0","output_id":"771fcdfc85d2f043o0"},{"input_id":"771fcdf97da47446i0","output_id":"771fcdfc5165a043o0"},{"input_id":"771fcdf97da47446i0","output_id":"771fcdfca2dc4d49o0"},{"input_id":"771fcdfc02e8e048i0","output_id":"771fcdfc7e80234eo0"},{"input_id":"771fcdfc7c924045i0","output_id":"771fcdfc7e80234eo0"},{"input_id":"771fcdfcbafbc74bi0","output_id":"771fcdfc02e8e048o0"},{"input_id":"771fcdfca2dc4d49i2","output_id":"771fcdfccb584c40o0"},{"input_id":"771fcdfccb584c40i0","output_id":"771fcdfc7c924045o0"},{"input_id":"771fcdfccb584c40i1","output_id":"771fcdfcbafbc74bo0"},{"input_id":"771fcdfccb584c40i2","output_id":"771fcdfcbafbc74bo0"},{"input_id":"7797b10ef648b84ei0","output_id":"7797b10ea5803a4do0"},{"input_id":"7797b10ef648b84ei1","output_id":"7797b10ea5803a4do0"},{"input_id":"771fcdf9e6cc774bi2","output_id":"7797b10ef648b84eo0"}]}}}

linear is forwards velocity

2 Likes

You do have to use pythagorean theorem, you just have to get the right format or else itll output 0.
For a power, you have to do Math.pow(base,exp)

So, the correct thing to put in is

Math.sqrt(Math.pow(A,2)+Math.pow(B,2))

1 Like

If your trying to find the speed of the object regardless of direction, then here’s my speedometer example:
https://flowlab.io/game/play/1472966

5 Likes

Thanks so much, this worked!