3

I just started learning Javascript about a week ago so bear with me. I am attempting to make a basic physics engine using a quadtree and nodes as the backing structure for all objects. However in this snippet of code

102 Quadtree.prototype.insert= function(self, object){
103     if (object == null){
104         return;
105     }
106     var objectx,
107         objecty,
108         objectw,
109         objecth;
110     objectx = object.getDim()[0];
111     objecty = object.getDim()[1];
112     objectw = object.getDim()[2];
113     objecth = object.getDim()[3];
114     var cn = root;
115     if(cn == undefined){
116         console.log("asdgalkjh");
117     }

cn (current node) is returning undefined. Object is also null. I know that my problem is arising from not knowing how javascript objects work in detail. Could someone please give me a summary or point me where to read? Thanks.

here is my node and quadtree class for reference

94 function Quadtree(width,height){
95     this.width = width;
96     this.height = height;
97     this.MAX_LEVELS = 4;
98     this.MAX_OBJ = 2;
99     var root = new Node(0,0,width,height);
100     root.split();
101 }

13 function Node(x,y,width,height){
14     var x,
15         y,
16         width
17         height;
18     this.x = x;
19     this.y = y;
20     this.width = width;
21     this.height = height;
22     var hash = 0;
23     var objects = new Array();
24     var parent = null;
25     var child1 = null;
26     var child2 = null;
27     var child3 = null;
28     var child4 = null;
29 }

I realize I may be doing many things wrong, I come from a heavy java background and javascript objects are passed by 'call by reference' which is also confusing so far. Any help is much appreciated.

jknam
  • 93
  • 7
  • I'm confused by your 'call be reference' statement. Both Java and Javascript are pass-by-value languages, but (almost) all variables are references. Both Java and Javascript behave the same way in this respect. – Mashmagar Jun 12 '15 at 20:16
  • Ah you are right actually. Question still stands, why can't i set currentnode to the root? – jknam Jun 12 '15 at 20:31
  • Only curious: what are you building? is it opensource? – rpax Jun 13 '15 at 07:40
  • I'm building a physics engine as a side project. For now the main features I want it to have is gravity, user being able to throw objects, and efficient object collision. I just started working on it less than a week ago but it's been really fun so far. I guess it's opensource technically haha. If you'd like to help I would be enthused. – jknam Jun 13 '15 at 15:01

1 Answers1

1

You are trying to access a private variable root which is accessible only in the QuadTree constructor.

If you want to use it anywhere else you will have to either make a getter or put it in public members (i.e. this.root = new Node(0,0,width,height);) and then access it with this.root

axelduch
  • 10,769
  • 2
  • 31
  • 50
  • So by saying this.root and not saying var it becomes a public member? And var makes it private? – jknam Jun 12 '15 at 20:32
  • Well, yes, but this is all because javascript scope works this way. When you use `var` you define it for the first wrapping function (i.e. QuadTree), and all functions inside this function can access it too. – axelduch Jun 12 '15 at 20:51