Whenever I try to use Body.pos, it always says that it is a nil value. But it is assigned a value in the new function, so it should not be nil.
My code:
Vector={
x=nil,y=nil
,new=function (self,x,y)
o={}
setmetatable(o,self)
self.__index=self
o.x=x or 1
o.y=y or 1
return o
end
-- utility functions here
}
Body={
pos=nil
,vel=nil
,acc=nil
,mass=nil
,new=function (self,pos,vel,acc,mass)
o={}
setmetatable(o,self)
self.__index=self
o.pos=pos or Vector:new()
o.vel=vel or Vector:new()
o.acc=acc or Vector:new()
o.mass=mass or 1
return o
end
,applyForce=function (self,v)
self.acc:add(v:scale(1/self.mass))
end
,applyGravity=function (self)
self.acc:add(GRAVITY_VECTOR)
end
,step=function (self)
self.vel:add(self.acc)
self.pos:add(self.vel)
self.acc:scale(0)
end
}
Trial code:
b=Body:new()
print(b.pos.x) -- shows error that pos is nil
Vector:new() does not return nil, but still Body.pos is always nil. I don't know what I am doing wrong here.
EDIT: Added Vector implementation
b.posis notnil. ButBody.posis of coursenil.Bodyis a class.bis instance.Vectordeclared?