Jump to content

Math appears correct in algorithm, but it's not functioning as intended

Hello,

 

So, I've been toying with some code in Snap! (An awesome Scratch mod that allows you to code directly in JS) trying to create a tileset generator for a game I'm working on, however the issue I'm having is that tiles are not rotating as intended. The math for my calculations appears correct however rotation is not occurring as intended. 

 

rot refers to the rotation around the viewpoint in degrees.

this.tWidth refers to the width of a tile rotated by an angle.

this.tHeight refers to the height of a tile rotated by an angle.

// Ensure we're not drawing.
this.up();

// Dimensions of our tilemap.
wide = 2;
high = 2;

//convert our angle in degrees to radians.
ang = (rot) * (3.1415 / 180);

// Lists containing all valid points along the X and Y axis.
cX = [];
cY = [];

// Generate list of all valid locations along the X axis.
clock = 0;
while (clock < wide)
{
  cX.push((Math.sin(ang) * (this.tWidth[rot] * clock)) - (Math.cos(ang) * (this.tHeight[rot] * clock)));
  clock = clock + 1;
};

// Generate list of all valid locations along the Y axis.                 
clock = 0;
while (clock < high)
{
  cY.push((Math.cos(ang) * (this.tWidth[rot] * clock)) + (Math.sin(ang) * (this.tHeight[rot] * clock)));
  clock = clock + 1;
};

//Draw the grid, rotated by X amount of degrees.
rendX = 0;
while (rendX < cX.length)
{ 
  rendY = 0;
  while (rendY < cY.length)
  {
    this.gotoXY(cX[rendX], cY[rendY]);
    this.doStamp();
    rendY = rendY + 1;
  };
  rendX = rendX + 1;
}

If anyone can spot what's wrong with my math here, it would be very much appreciated.

 

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to post
Share on other sites

Is this the whole script? If so when does (rot) get a value? Looking at it from a very basic understanding right now (rot) will be empty so the result for sin will be 0. And 0 times something will always result in 0. So cX gets filled by 0.

Link to post
Share on other sites

7 minutes ago, Dujith said:

Is this the whole script? If so when does (rot) get a value? Looking at it from a very basic understanding right now (rot) will be empty so the result for sin will be 0. And 0 times something will always result in 0. So cX gets filled by 0.

it plugs into a javascript block on Snap! that has a parameter labeled rot.

 

So it would look more like this if you include the code for that:

 

function anonymous (rot)
{
  // Ensure we're not drawing.
  this.up();

  // Dimensions of our tilemap.
  wide = 2;
  high = 2;

  //convert our angle in degrees to radians.
  ang = (rot) * (3.1415 / 180);

  // Lists containing all valid points along the X and Y axis.
  cX = [];
  cY = [];

  // Generate list of all valid locations along the X axis.
  clock = 0;
  while (clock < wide)
  {
    cX.push((Math.sin(ang) * (this.tWidth[rot] * clock)) - (Math.cos(ang) * (this.tHeight[rot] * clock)));
    clock = clock + 1;
  };

  // Generate list of all valid locations along the Y axis.                 
  clock = 0;
  while (clock < high)
  {
    cY.push((Math.cos(ang) * (this.tWidth[rot] * clock)) + (Math.sin(ang) * (this.tHeight[rot] * clock)));
    clock = clock + 1;
  };

  //Draw the grid, rotated by X amount of degrees.
  rendX = 0;
  while (rendX < cX.length)
  { 
    rendY = 0;
    while (rendY < cY.length)
    {
      this.gotoXY(cX[rendX], cY[rendY]);
      this.doStamp();
      rendY = rendY + 1;
    };
    rendX = rendX + 1;
  }
};

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×