|
| 26 Jul 2016 03:16 PM |
What would be a pure lua implementation of each the methods/constructors of the CFrame and Vector3 types?
I would prefer for the format for these implementations to be as a function that accepts the same arguments as the internal function it is depicting and returns the same values for the same inputs as the internal function it is depicting.
Finally, the reason I want to know these implementations is so that I can more efficiently use them.
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 02:05 PM |
| Also, to clarify what I mean by pure is to create the functions using (among other things) the lua math functions such as the trigonometry functions. |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 27 Jul 2016 02:22 PM |
| They're all written in C. If you want then written in Lua you could always write them yourself but it seems like a waste of time. Unless I'm misunderstanding. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 05:07 PM |
@ Kodran
Thank you so much for responding. The reason I don't just write them myself is because I don't know how to (I am currently learning trig), so I want to see what they look like in lua (as a function) to further my understanding of roblox mathematics.
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 05:09 PM |
Vector3 does not require math.
All it does is store 3 numbers and C and C++ will use those numbers for positioning.
CFrame.p is the same
CFrame.Angles() PLEASE don't try to understand this. Just use it when you need to.
http://wiki.roblox.com/index.php?title=CFrame#Rotation_Matrix |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 27 Jul 2016 05:36 PM |
the dot product and cross product wiki pages give great explanations on how they work. lerp uses a standard linear interpolation function (search Wikipedia for linear interpolation and find the programming part). the cframe lerp function is actually a slerp function which uses quaternions which will probably be the hardest one to totally grasp. the unit function I believe has a writeup on the wiki, but it just normalizes the vector (Google vector normalization).
just tell me if there are any in particular I forgot that you want to know about. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 05:41 PM |
If you wanna make your own methods, functions, etc involving vector3's you could lean towards object - oriented organization:
Vector3 = {}
Vector3.new = function (x,y,z) if type(x) == "Number" and type(y) == "Number" and type(z) == "Number" then return {x or 0 ,y or 0, z or 0} else error('Invalid argument(s) for ' .. '"Vector3.new(' .. tostring(x) .. ',' .. tostring(y) .. ',' .. tostring(z) .. ')' end end
Btw I'm trying to write this on a touchscreen so take everything I said with a grain of salt |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 06:08 PM |
@ thedailyblarg
Thank you for your response. What I mean is something to depict the CFrame's methods, constructors, and function-like properties like this:
local CFrame = setmetatable({},{ __index = function(self, index) if tostring(index)=='lookVector' then --what would go here else return rawget(self,tostring(index)) end end }) function CFrame.fromEulerAnglesXYZ( rx, ry, rz ) --what would go here end function CFrame.Angles( rx, ry, rz ) --what would go here end function CFrame.fromAxisAngle( v, r ) --what would go here end function CFrame:lerp( goal, alpha ) --what would go here end function CFrame:toWorldSpace( cf ) --what would go here end function CFrame:toObjectSpace( cf ) --what would go here end function CFrame:pointToWorldSpace( v3 ) --what would go here end function CFrame:pointToObjectSpace( v3 ) --what would go here end function CFrame:vectorToWorldSpace( v3 ) --what would go here end function CFrame:vectorToObjectSpace( v3 ) --what would go here end function CFrame:components( ) --what would go here end function CFrame:toEulerAngles( ) --what would go here end
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 06:19 PM |
Like I said before, that has to deal with the rotation matrix and some pretty heavy trigonometry and it wont really help you with anything ever since all that math serves a purpose to do is function as a spherical rotation equation which allows objects to be turned around the X Y and Z axis.
Also, most of roblox's code is in their files and if you look inside the right ones you can see most of the information. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 06:21 PM |
For all other equations, you can try to come up with solutions but it will take many tries and why you would want to do this even though functions exist to do that for you, I dont know.
Dont worry much about these functions, you just need to know when and how to use them.
Learning every aspect about them is not going to help you with math, only applying it to your code and learning it in school will assist you the most. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:10 PM |
@ thedailyblarg
Thank you very much for your 2 responses. I am fairly sure that roblox just doesn't have the source code for that on public display (let alone incorporated into the local files of each client). The reason for why I would like to understand the functions even though they already exist is so that I can more effectively use them in more complicated things.
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:22 PM |
@ LateralLace
Thank you for your reply, but I already know about that, and I already extremely experienced in the whole lua language + memorized much of the roblox API. So, something for you to think about: why not use userdata (for compatibility with roblox types and more metamethods) metatables for far simpler, easier use of your API so that (for example) you can replace calling a function with no input values with a simple member-access metamethod that calls that function for you.
|
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:43 PM |
NOTE: I wrote this up super quickly, and cannot guarantee everything works as intended. I was focusing on speed; I may have made some logic errors, if I did, do tell me, because I will want to fix it!
I'd assume it's something like this:
local Vector3 do Vector3 = {x=0,y=0,z=0}
local meta = { __index = Vector3, __newindex = function(t,k,v) if (k == 'x' or k == 'y' or k == 'z') and type(v) == 'number' then rawset(t,k,v) else error('k is either readonly, or it does not exist') end end, __add = function(a,b) local am, bm = getmetatable(a), getmetatable(b) if am and am.__Type == 'Vector3' and bm and bm.__Type == 'Vector3' then return Vector3.new(a.x+b.x,a.y+b.y,a.z+b.z) else error('Attempt to add non-Vector3 to a Vector3',2) end end, __sub = function(a,b) local am, bm = getmetatable(a), getmetatable(b) if am and am.__Type == 'Vector3' and bm and bm.__Type == 'Vector3' then return Vector3.new(a.x-b.x,a.y-b.y,a.z-b.z) else error('Attempt to subtract non-Vector3 to a Vector3',2) end end, __mul = function(a,b) local am, bm = getmetatable(a), getmetatable(b) if am and am.__Type == 'Vector3' and bm and bm.__Type == 'Vector3' then return Vector3.new(a.x*b.x,a.y*b.y,a.z*b.z) elseif tonumber(a) then return Vector3.new(b.x*a,b.y*a,b.z*a) elseif tonumber(b) then return Vector3.new(a.x*b,a.y*b,a.z*b) end end, __div = function(a,b) local am, bm = getmetatable(a), getmetatable(b) if am and am.__Type == 'Vector3' and bm and bm.__Type == 'Vector3' then return Vector3.new(a.x/b.x,a.y/b.y,a.z/b.z) elseif tonumber(a) then return Vector3.new(a/b.x,a/b.y,a/b.z) elseif tonumber(b) then return Vector3.new(a.x/b,a.y/b,a.z/b) end end, __tostring = function(t) return t.x..', '..t.y..', '..t.z end, __Type = 'Vector3' } function Vector3.new(x,y,z) return setmetatable({x=tonumber(x) or 0,y=tonumber(y) or 0,z=tonumber(z) or 0},meta) end
function Vector3.lerp(self, goal, alpha) local m = getmetatable(other) if m and m.__Type == 'Vector3' then return Vector3.new((self.x-other.x)*alpha+self.x,(self.y-other.y)*alpha+self.y,(self.z-other.z)*alpha+self.z) end end
function Vector3.dot(self,other) local m = getmetatable(other) if m and m.__Type == 'Vector3' then return self.x*other.x+self.y*other.y+self.z*other.z end end
function Vector3.cross(self, other) local m = getmetatable(other) if m and m.__Type == 'Vector3' then return Vector3.new(self.y*other.z-self.z*other.y, self.z*other.x-self.x*other.z, self.x*other.y-self.y*other.x) end end setmetatable(Vector3,{ __index = function(t,k) if k == 'magnitude' then return math.sqrt(t.x^2+t.y^2+t.z^2) elseif k == 'unit' then local m = math.sqrt(t.x^2+t.y^2+t.z^2) return Vector3.new(t.x/m,t.y/m,t.z/m) else return rawget(t,k) end end, __newindex = function() error('You cannot write to the Vector3 table', 2) end }) end |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:47 PM |
"Thank you for your reply, but I already know about that, and I already extremely experienced in the whole lua language + memorized much of the roblox API. So, something for you to think about: why not use userdata (for compatibility with roblox types and more metamethods)"
Pure Lua (well at least 5.2 and up) no longer have newproxy, so you can't use userdata. And in RBX.Lua, using a userdata will not be compatible with roblox types, they will recognize it as a userdata you made, because it will not have the same metatable as their userdatas. |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 27 Jul 2016 07:49 PM |
| >Implying roblox will EVER move on from Lua 5.1 rofl |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:52 PM |
'>Implying roblox will EVER move on from Lua 5.1 rofl'
Well first of all, that has been previously discussed as a very serious idea.
Secondly, I wasn't implying that, he just said 'pure lua', so I was making sure my code was up-to-date. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 07:56 PM |
@ warspyking
Thank you for your reply, but that's not what I meant. obviously, it would be impossible to use a forged userdata in place of a real one with the roblox API unless you have a 3d-party-program running on the computer which would be impractical. What I meant is that if you use userdata then the __ep, __lt, and __le metamethods will function properly when used with Roblox API types. Roblox Wiki: http://wiki.roblox.com/index.php?title=Metatable |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 27 Jul 2016 07:57 PM |
| It was more a commentary on the state of roblox's priorities. Lua 5.2 was released nearly 5 years ago, and 5.3 over a year ago. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 08:31 PM |
'obviously, it would be impossible to use a forged userdata in place of a real one with the roblox API unless you have a 3d-party-program running on the computer which would be impractical.'
No 3rd-party-program would do this.. Stop thinking 3rd party programs are godlike, you act like they can do anything *references geolocation thread where you assume a 3rd party software would solve your problem, despite having 0 knowledge on the subject*
'What I meant is that if you use userdata then the __ep, __lt, and __le metamethods will function properly when used with Roblox API types.'
Okay jack, I've been using metatables for years now, I know how they work lol. First of all, being a userdata has nothing to do with the __ep __lt and __le metamethods. They only run if the both tables being compared have the *same* metatable. In other words, you would be trying to 'use a forged userdata in place of a real one with the roblox API', which as previously stated by both of us, isn't possible, because you cannot set it to the same metatable.
'Roblox Wiki: http://wiki.roblox.com/index.php?title=Metatable '
I don't have much to say either, other then the fact you quoted the wiki about something obviously false, and the wiki doesn't even back it up. Why would you put this there lol.
I coded what (is hopefully a perfectly working) Vector3 table in pure Lua, something YOU were asking about. Why act like you are more knowledgeable on the subject? |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 08:35 PM |
| Not the same metatable, same __eq/__lt/__le functions (apart from the I read nothing, not even the other posts) |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 08:37 PM |
'First of all, being a userdata has nothing to do with the __ep __lt and __le metamethods. They only run if the both tables being compared have the *same* metatable.'
Simple correction:
First of all, being a userdata has nothing to do with the __eq __lt and __le metamethods. __eq only runs if the both tables being compared have the *same* metatable. As for __lt and __le I'm unsure why you were so confused. |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 08:38 PM |
@Flux Hold on.. I thought that as well but the wiki is saying otherwise??
wiki.roblox.com/index.php?title=Metamethods#eq
"Note that for Lua to rely on __eq the values must have the same metatable and basic type (table/userdata/etc.)"
And the other metamethods (__lt and __le) don't have this note on them at all? |
|
|
| Report Abuse |
|
|
|
| 27 Jul 2016 08:41 PM |
Alright since flux confirmed I'm just gonna go with my gut instinct anyway until someone proves the wiki wrong:
First of all, being a userdata has nothing to do with the __eq __lt and __le metamethods. They only run if the both tables being compared's metatable have the *same* metamethod, along with a common type. So even if they are a userdata, you're not gonna be able to get them to return true/false when comparing to a regular Vector3. |
|
|
| Report Abuse |
|
|
| |
|
|
| 27 Jul 2016 08:51 PM |
| @Flux Nothing new I suppose lol |
|
|
| Report Abuse |
|
|