This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Creator's profile picture

3D Engine transformation matrix help

Started by Creator, 14 December 2015 - 05:34 PM
Creator #1
Posted 14 December 2015 - 06:34 PM
Hi guys,

I have been working on a 3D engine for a week now, and I am having some questions.

When I rotate the object (using a left handed coordinate system), the points move in a weird manner. Instead of staying on the circle, they come inwards:



There are two "parallel" circles because there are tow points. The main concern is that the points come inwards.

This is the code I use for the matrix:


rotateZ = function(dergees)
  local radians = rad(dergees)
  local c = math.cos(radians)
  local s = math.sin(radians)
  --[[return {
    c, s, 0, 0,
   -s, c, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1,
  }]]--
  return {
   ["1_1"] = c,
   ["1_2"] = s,
   ["2_1"] = -s,
   ["2_2"] = c,
  }
end,

This is the code I use for multiplying the matrix with the position vector (x,y,z,w):


rotateZ = function(matrix)
   x = x*matrix["1_1"] + matrix["1_2"]*y
   y = x*matrix["2_1"] + matrix["2_2"]*y
  end,

I have striped the useless operations away to make it faster, much faster.

The questions:
  1. Why does the circle go inwards?
  2. Is this the correct transformation matrix for rotating around the Z axis on a left hand coordinate system?
  3. What exactly does w (in the position vector) represent and why is it useful?
  4. What is the translation matrix?

Thanks in advance,

Creator.

PS: Before anyone jumps on me saying: "This is not CC!"

I know, I am developing it for Love2D and then porting to CC 2.0
Lupus590 #2
Posted 14 December 2015 - 09:50 PM
  • Why does the circle go inwards? At a guess, rounding errors or a bug
  • Is this the correct transformation matrix for rotating around the Z axis on a left hand coordinate system? I'm not good at remembering which
  • What exactly does w (in the position vector) represent and why is it useful? uniform scaling
  • What is the translation matrix? A matrix which (if multiplied by a vector/point) will move the vector/point.
If you know C++ feel free to look at my matrix/maths classes. It uses the same coordinate system as openGL, which I believe is left handed.
Edited on 14 December 2015 - 08:55 PM
Creator #3
Posted 14 December 2015 - 11:28 PM
Thanks
SquidDev #4
Posted 15 December 2015 - 12:01 PM

rotateZ = function(matrix)
   x = x*matrix["1_1"] + matrix["1_2"]*y
   y = x*matrix["2_1"] + matrix["2_2"]*y
  end,

You overwrite the old x value with a new one, making the second calculation wrong.

rotateZ = function(matrix)
   local oldX = x
   x = x*matrix["1_1"] + matrix["1_2"]*y
   y = oldX*matrix["2_1"] + matrix["2_2"]*y
  end,
Edited on 15 December 2015 - 11:02 AM
Creator #5
Posted 15 December 2015 - 12:34 PM
Thanks SquidDev. I seriously don't know how I oversaw that. +1

It works! Just tested it.