Problem calling a function

Discuss issues specific the Javascript port of Box2D
Kott
Posts: 23
Joined: Sun Jun 08, 2014 11:24 am

Problem calling a function

Postby Kott » Mon Jul 07, 2014 4:08 am

Hello I'm experimenting with Box2dWeb, and working with top-down car game.
My problem arises when I try to control the car, so it will move, at first only forwards. For simplicity I don't want to use wheels, and just apply the force to the car (a box). For the controls I made a function for but for a reason it's not getting called... That's where i need a pointer or advice. (Creation and placement of objects works just fine)
here's part of the code:

Code: Select all

var GlobalVar={   }
var KEY = {
        UP: 87,//W
        DOWN: 83,//s
   LEFT: 65,//A
   RIGHT: 68//D
}        
GlobalVar.pressedKeys = [];//an array to remember which key is pressed or not

$(function(){
   $(document).keydown(function(e){
      GlobalVar.pressedKeys[e.keyCode] = true;
   });
   $(document).keyup(function(e){
      GlobalVar.pressedKeys[e.keyCode] = false;
   });

   Rendering();
   PlaceStuff(GlobalVar.currentLevel);//placing stuff, like car and boundaries/walls
   moveCar();

});
function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      GlobalVar.car.ApplyForce(force, GlobalVar.car.GetWolrdCenter());
   }
}

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Problem calling a function

Postby irresistible force » Mon Jul 07, 2014 2:58 pm

I think you would be better off asking this on Stack Overflow and tagging with javascript, jQuery etc. It's not really a Box2D issue.

If you want the moveCar function to be called repeatedly, you would need to have it inside a loop of some kind.
You could look into using requestAnimationFrame for that: https://developer.mozilla.org/en/docs/W ... ationFrame

Kott
Posts: 23
Joined: Sun Jun 08, 2014 11:24 am

Re: Problem calling a function

Postby Kott » Tue Jul 08, 2014 8:32 am

True the topic is not really Box2d issue.
But there is an error that i stumbled upon that is Box2d related:
(I'm trying to apply the force at the center of mass)

Code: Select all

function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      GlobalVar.car.ApplyForce(force, GlobalVar.car.GetWolrdCenter());
   }
}

From the above i get: TypeError: GlobalVar.car.GetWolrdCenter is not a function
I modified the above code

Code: Select all

function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      var posi = GetWolrdCenter(GlobalVar.car);
      GlobalVar.car.ApplyForce(force, posi);
   }
}

and got:ReferenceError: GetWolrdCenter is not defined. I guess I also have to define var posi with b2Vec2 or something...
Now, sorry if what I ask is supposed to be simple, but understand that I'm trying to learn and don't know some stuff yet...

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Problem calling a function

Postby irresistible force » Tue Jul 08, 2014 7:39 pm

Probably a typo: 'Wolrd' should be 'World' :D

Kott
Posts: 23
Joined: Sun Jun 08, 2014 11:24 am

Re: Problem calling a function

Postby Kott » Wed Jul 09, 2014 10:53 am

That's kinda embarassing... :oops:

Kott
Posts: 23
Joined: Sun Jun 08, 2014 11:24 am

Re: Problem calling a function

Postby Kott » Wed Jul 09, 2014 11:13 am

But wait a minute.
In the 1st example (of my second post), i understand why the error was:
TypeError: GlobalVar.car.GetWolrdCenter is not a function. There is not such a thing as GetWolrdCenter, Wolrd :o

But in the 2nd example there where no typos... :

Code: Select all

function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      GlobalVar.car.ApplyForce(force, GetWorldCenter(GlobalVar.car));
   }
}

or

Code: Select all

function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      var posi = new b2Vec2(0,0);
      var posi = GetWorldCenter(GlobalVar.car);
      GlobalVar.car.ApplyForce(force, posi);
   }
}

I get the same error: ReferenceError: GetWorldCenter is not defined.
I also tried GetWorldCenter(car), perhaps my global object/variable is not needed there, but still got the same reference error...
Can you explain me why GetWorldCenter is not defined correctly?

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Problem calling a function

Postby irresistible force » Wed Jul 09, 2014 4:45 pm

The GetWorldCenter function belongs to the b2Body class, so you need to call it like
mybody.GetWorldCenter()

You can't just call it with no body like you are doing.

Kott
Posts: 23
Joined: Sun Jun 08, 2014 11:24 am

Re: Problem calling a function

Postby Kott » Thu Jul 10, 2014 7:05 am

... well i knew that GetWorldCenter is from b2Body class (although apparently i cannot implement it correctly yet).
I thought that it's enough for a variable to have in it b2Body(e.g has body definitions), like the car variable and then you could use it everywhere

Straight to the point:
(I'm trying to use deferent functions for deferent tasks )
mine part of code:

Code: Select all

function CreateCarAndPlacement(x, y){
      //creating car: body, fixture and Shape
      var car_bodyDef = new b2BodyDef;
      car_bodyDef.type = b2Body.b2_dynamicBody;
      car_bodyDef.UserData = "car";
      car_bodyDef.position.Set(x,y);

      var car_fixtrDef = new b2FixtureDef;
      car_fixtrDef.density = 10;
      car_fixtrDef.friction = 1.5;
      car_fixtrDef.restitution = 0.2;
      car_fixtrDef.shape = new b2PolygonShape;
      car_fixtrDef.shape.SetAsBox(.8,1.5);//takes half-width, half-height values

var car = world.CreateBody(car_bodyDef);
car.CreateFixture(car_fixtrDef);

      return car;
}
function moveCar(){
   if (GlobalVar.pressedKeys[KEY.UP]){
      var force = new b2Vec2(0, -10000000);
      GlobalVar.car.ApplyForce(force, car.GetWorldCenter());
   }
}

i get: ReferenceError: car is not defined GlobalVar.car.ApplyForce(force, car.GetWorldCenter());
But isn't 'return car' from CreateCarAndPlacement function counts?
I'm missing something essential... Maybe the problem lies in the way moveCar() is called(not shown here...)?
~~~~~~~~~~
the below part of code is from codinowl, just to show an example of GetWorldCenter:

Code: Select all

var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
var fixDef = new b2FixtureDef;
fixDef.density = 30;
fixDef.friction = .8;
fixDef.restitution = 0.1;
fixDef.shape = new b2CircleShape(0.3);
// Car body
bodyDef.position.Set(10,8);
// just change the fixDef.shape and keep other properties
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(.5,1.5);
var car = world.CreateBody(bodyDef);
car.CreateFixture(fixDef);

var xx = car.GetWorldCenter().x;
var yy = car.GetWorldCenter().y;

and it works without error(of course)

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Problem calling a function

Postby irresistible force » Thu Jul 10, 2014 7:29 am

If the 'car' variable belongs to 'GlobalVar', then you would need to use it like:
GlobalVar.car

You couldn't use it as just 'car' only like you are doing.
On the other hand, if 'car' is a global variable then it is not part of 'GlobalVar' (whatever that is) and prefixing it would cause a problem.

It's unclear from the code you have shown which of these cases is happening since we don't know what 'GlobalVar' is or what it contains, and again, none of this is related to Box2D :D



Return to “Javascript”



Who is online

Users browsing this forum: No registered users and 1 guest