Author Topic: camera.getPosition() a copy?  (Read 3558 times)

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
camera.getPosition() a copy?
« on: December 30, 2008, 11:26:19 am »
ok so i'm slowly hacking away at getting my core elements going  :P

I've come across a pecularity that came up. I am using LuaJ. It has the problem that it's internvalues don't refer back to floats naturaly in it's enviroment. So if I
Code: [Select]
camera:setPosition(1.0, 2.0, 3.0); // in lua. This will fail. Ok so I came up with a solution

SimplerVector v = camera:getPosition(); //get the simplevector
bridge:setSimpleVector(v, 1.0, 2.0, 3.0);
 
this doesn't work, no error though. this is my custom class that converts the lua values to floats in the java enviro.

I can however do this
Code: [Select]
camera:setPosition(bridge:createSimpleVector(1.0, 2.0, 3.0));  this however creates a new SimpleVector

It seems that geting the camera position get's a copy not the the actual vector that camera uses. Am I doing something wrong?
Also if there is no other solution will creating a new SimpleVector per camera update cause a memory problem?




Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: camera.getPosition() a copy?
« Reply #1 on: December 30, 2008, 11:59:53 am »
I'm really stupid sometimes. While I can't find an ideal solution. I at least have one to avoid any memory mis management problems.

Code: [Select]
local cameraTask = luajava.createProxy("sre.util.Task",
  {
   update = function()
     position = camera:getPosition(); -- get copy? of current camera position. updating purposes
     --when updating the camera, x&y match the player. eventually
     bridge:setSimpleVector(position, 150, -60, -150); -- set the new position into the vector
     camera:setPosition(position);  --apply the vector
     
     bridge:setSimpleVector(position, 0,0,0); -- repeat above
     camera:lookAt(position);
   end
  });

I would still like to know if getPosition is a copy or if i'm doing something wrong :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: camera.getPosition() a copy?
« Reply #2 on: December 30, 2008, 01:41:59 pm »
Yes, it's a copy. The reason for this is, that the camera itself doesn't store the value this way, so i have to create a new instance every time you call that method.

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: camera.getPosition() a copy?
« Reply #3 on: December 30, 2008, 06:05:15 pm »
thank you. It's helpful knowledge. I now know not to continue looking for a better solution.