Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperLicht
Bug in Vect3d.js

svas
Registered User
Quote
2016-02-16 21:38:47

Hi

I found a bug in the CL3D.Vect3d.rotateXYBy (and XZ/YZ) functions. The easiest way to expose the bug is to compare the output with the same function in irrlicht.

Happy to submit a patch if you can tell me how. The problem is in the last line of the function. It uses this.X but this.X gets overwritten by the rotation in the line before. The fix is to use a temporary variable that is assigned to this.X before the rotation is applied.

Current code

CL3D.Vect3d.prototype.rotateXYBy = function(degrees, center)
{
degrees *= CL3D.DEGTORAD;
var cs = Math.cos(degrees);
var sn = Math.sin(degrees);

this.X = this.X*cs - this.Y*sn;
this.Y = this.X*sn + this.Y*cs; // <---- Problem is in this line.
}



Fixed code:

CL3D.Vect3d.prototype.rotateXYBy = function(degrees, center)
{
degrees *= CL3D.DEGTORAD;
var cs = Math.cos(degrees);
var sn = Math.sin(degrees);

var oldX = this.X
var oldY = this.Y

this.X = oldX*cs - oldY*sn;
this.Y = oldX*sn + oldY*cs;
}


The reason irrlicht does not have this bug is because irrlicht sets it directly like so:

set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z);


One more thing: it would be great to also support rotating around a center like irrlicht has. Happy to send a patch for this as well.

Thanks


niko
Moderator
Quote
2016-02-17 07:58:49

Oh, thanks! Nice to see a bug report including even the bug fix :) Will be added in the upcoming update. About the rotation: Of course, if you like, post it. The less work I have, the more likely it is that it is going to be included, of course. :)


svas
Registered User
Quote
2016-02-17 23:37:48

Here is the code to handle rotating about a center (copied from irrlicht).


/**
* Rotates the vector around XY by a specic angle and center
* @public
*/
CL3D.Vect3d.prototype.rotateXYBy = function(degrees, center)
{
degrees *= CL3D.DEGTORAD;
var cs = Math.cos(degrees);
var sn = Math.sin(degrees);

if (center) {
this.X -= center.X;
this.Y -= center.Y;
}

var oldX = this.X
var oldY = this.Y

this.X = oldX*cs - oldY*sn;
this.Y = oldX*sn + oldY*cs;

if (center) {
this.X += center.X;
this.Y += center.Y;
}
}

/**
* Rotates the vector around YZ by a specic angle and center
* @public
*/
CL3D.Vect3d.prototype.rotateYZBy = function(degrees, center)
{
degrees *= CL3D.DEGTORAD;
var cs = Math.cos(degrees);
var sn = Math.sin(degrees);

if (center) {
this.Z -= center.Z;
this.Y -= center.Y;
}

var oldY = this.Y
var oldZ = this.Z

this.Y = oldY*cs - oldZ*sn;
this.Z = oldY*sn + oldZ*cs;

if (center) {
this.Z += center.Z;
this.Y += center.Y;
}
}

/**
* Rotates the vector around XZ by a specic angle and center
* @public
*/
CL3D.Vect3d.prototype.rotateXZBy = function(degrees, center)
{
degrees *= CL3D.DEGTORAD;
var cs = Math.cos(degrees);
var sn = Math.sin(degrees);

if (center) {
this.X -= center.X;
this.Z -= center.Z;
}

var oldX = this.X
var oldZ = this.Z

this.X = oldX*cs - oldZ*sn;
this.Z = oldX*sn + oldZ*cs;

if (center) {
this.X += center.X;
this.Z += center.Z;
}
}



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "?nternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact