INTERACTIVITY

21
Jun
2012

Here we will see how to interact with the Box2D World. However these are only some of the interactions that can be performed. For all the interactions click here. Then select b2Body from the menu.

## Applying Force to a Body

```box1.ApplyForce(new b2Vec2(10000,0), box1.GetWorldPoint(new b2Vec2(0,-3)));
```
A Force of Magnitude 10000 in +ve X direction will act at the point 3 units above the box1 center point.

GetWorldPoint gives the local point with respect the body center. A local point is fixed in the frame of the body. It fits the definition of a local coordinate system. The local point always the same regardless of the orientation of the body i.e. is point(1,0) always the same point in the body (i.e. relative to the bodies own internal x,y co-ordinate system) regardless of its orientation. The point doesn't have to be on the object.

## Applying Impulse to a Body

```box1.ApplyImpulse(new b2Vec2(110,0), box1.GetWorldCenter());
```
Same as Apply Force. It applies an Impulse at the center of the box1. Of course you can use GetWorldPoint() here too. Infact you can use it wherever you feel like. :)

## Setting Velocity to a Body

Although already discussed let's discuss it once more.
```box1.SetAwake(true);
box1.SetLinearVelocity(new b2Vec2(10,0));```
The first line is there to wake the body in case it is sleeping. The body won't move even if its given a speed if the body is sleeping.

## Teleporting a Body

```box1.SetPositionAndAngle(new b2Vec2(10,0), Math.PI/3);
```
New position will be set at x = 10 & y = 0 and at an Angle of 60 Degrees in clockwise direction.

## Interacting with Keyboard

I'll use jQuery for this example. Make sure you import jQuery javascript file.
```var c=0;
\$(window).keydown(function(key) {
c++;
var code = key.keyCode;
if(code == 116 || code == 84)	teleport(c); //Key 't' or 'T'
if(code == 118 || code == 86)	speed(c);    //Key 'v' or 'V'
if(code == 102 || code == 70)	push_(c);    //Key 'f' or 'F'
if(code == 105 || code == 73)	hit(c);      //Key 'i' or 'I'
});
\$(window).keyup(function(e) {
c=0;
});

//force
function push_(c) {
if(c==1)	box1.ApplyForce(new b2Vec2(0,-10000), box1.GetWorldPoint(new b2Vec2(0,-3)));
}
//impulse
function hit(c) {
if(c==1)	box1.ApplyImpulse(new b2Vec2(110,0), box1.GetWorldCenter());
}
//velocity
function speed(c) {
if(c==1)	{
box1.SetAwake(true);
box1.SetLinearVelocity(new b2Vec2(4,0));
}
}
//teleportation
function teleport(c) {
if(c==1)	{
box1.SetAwake(true);
box1.SetPositionAndAngle(new b2Vec2(10,0),Math.PI/3);
}
}
```
Here I have declared variable 'c' as a counter to prevent multiple calling of any particular function if any key, let's say 'f' is pressed for a long time. The function will only be called when 'c' is equal to '1'. When key is 'unpressed' it's value is again set to '0'. I hope rest of the code is easily understandable.

Use t,v,f & i for Teleporting, Applying Velocity, Applying Force and Applying Impulse respectively.

By administrator at 11:03:17 PM 0 Comment(s)

 Please enter the email address.Invalid format. (Won't be Displayed) Notify me of followup comments Enter the displayed Code: 