Jump to content

Vector3.lerp is concatenating instead of adding


ozRocker
 Share

Recommended Posts

I'm using babylon 2.4 here.

I've providing position vectors to the Vector3.lerp function.  The result seems to be a string concatenation instead of arithmetic addition.

Below I'm just showing x, z coordinates because y is always constant

from: 2.000, 0.000  to: 2.400, 0.000
lerp percentage: 0.33589999999996506
lerped position: 2.0000.134359999999986, 0.0000
lerp percentage: 0.9552999999999884
lerped position: 2.0000.38211999999999524, 0.0000

so this is going from (2.0, 0.0) to (2.4, 0.0)

The 1st resulting lerp should be 2.134359999999986 but instead its 2.0000.134359999999986

 

Link to comment
Share on other sites

yep, that was it.  The mesh position values were treated as strings because they were set from a JSON record.  I had to cast them to numbers.  The weird thing is that the scene renderer was treating that position vector as numbers and moving my avatar along normally, which is why I didn't pick this up before.

Link to comment
Share on other sites

  • 2 weeks later...
On 5/19/2016 at 3:48 PM, ozRocker said:

yep, that was it.  The mesh position values were treated as strings because they were set from a JSON record.  I had to cast them to numbers.  The weird thing is that the scene renderer was treating that position vector as numbers and moving my avatar along normally, which is why I didn't pick this up before.

This is exactly why you should learn Typescript :)

I'd really recommend pushing through the initial PITA of getting started. If you'd written that in Typescript you'd have a red line under it in your IDE.

Link to comment
Share on other sites

2 hours ago, Dal said:

This is exactly why you should learn Typescript :)

I'd really recommend pushing through the initial PITA of getting started. If you'd written that in Typescript you'd have a red line under it in your IDE.

I am using Typescript.  My whole project is in Visual Studio.  I didn't see any error or any red line.

Link to comment
Share on other sites

My code is kinda like this in Typescript:

        var vector: BABYLON.Vector3;
        var JSONstring: string;
        var instruction = JSON.parse(JSONstring);
        var message = instruction.message;

        vector = new BABYLON.Vector3(message.player.position.x, message.player.position.y, message.player.position.z);

I'm dealing with a JSON string that gets parsed into a combination of objects and strings so I can't give strict types for them.  Because of that I'm passing untyped data to the Vector3 constructor.  No red-line or error.  Also, the code runs even with the strings in there.  Babylon.js has no problem with it.  Its only when I do standard javascript arithmetic is when the problem arises.

Link to comment
Share on other sites

21 hours ago, ozRocker said:

My code is kinda like this in Typescript:


        var vector: BABYLON.Vector3;
        var JSONstring: string;
        var instruction = JSON.parse(JSONstring);
        var message = instruction.message;

        vector = new BABYLON.Vector3(message.player.position.x, message.player.position.y, message.player.position.z);

I'm dealing with a JSON string that gets parsed into a combination of objects and strings so I can't give strict types for them.  Because of that I'm passing untyped data to the Vector3 constructor.  No red-line or error.  Also, the code runs even with the strings in there.  Babylon.js has no problem with it.  Its only when I do standard javascript arithmetic is when the problem arises.

class Player {
   position:Vector3 = Vector3.Zero();
}

var message = instruction.message;
var player:Player = <Player>message.player;

vector = new BABYLON.Vector3(player.position.x, player.position.y, player.position.z);

That probably won't work as it is but something on those kind of lines would avoid passing untyped data around. If message.player is the right shape it should fit.

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...