# Using impulses to shoot boxes

Hi,
It’s really great developing on play canvas. I now have HTC vive controllers looking like guns firing with muzzle flashes and hitting boxes (thanks to everyone for the tips). What I would love to know now is how you make the boxes react realistically. From the documentation it looks like you can identify where the raycast from the gun strikes the box and I am guessing that I can use that to figure out what linear and angular impulses i need to apply to the box when it is struck but I just don’t know how at the moment? and do I need to apply both linear and angular impulses or does the linear spin the box if it is coming in at an angle?

I will probably mess around with it for a bit but if anyone has an example for me to look at it would really help.

1 Like

So yes, to make the boxes react correctly to the strike of a bullet, you would use an impulse rather than a force. An impulse is a force applied in an instant (used for making a character jump, say) whereas a force is applied over time (to push a body over a surface, say).

So you know the position and direction of your gun. That’s the ray that you would perform a raycast with. The raycast result gives you the point of intersection in world space. So you would apply an impulse to the hit body that has the same direction as the ray. The only thing you need to do is also supply the relative point to apply the impulse in the local coordinate space of the hit entity. To do this, transform the world space intersection point supplied by pc.RayCastResult into local space of the entity.

I believe you would have to transform that point by the inverse world transformation matrix of the entity. So something like:

``````var from = gunEntity.getPosition();
var to = from.clone();

app.systems.rigidbody.raycastFirst(from, to, function (result) {
var hitEntity = result.entity;

var localOffset = new pc.Vec3();
var mat = hitEntity.getWorldTransform().clone();
mat.invert();
mat.transformPoint(result.point, localOffset);

hitEntity.applyImpulse(rayDir, localOffset);
});
``````

Warning: I have not tested this code!

@will I tried the code and had to make a couple changes but I still need to define local offset and raydir

HERE is my code

The code Will posted gives you the local offset:

``````    var localOffset = new pc.Vec3();
var mat = hitEntity.getWorldTransform().clone();
mat.invert();
mat.transformPoint(result.point, localOffset);
``````

To get the ray direction you can easily do so from your raycast:

``````var rayDir = to.sub(from).normalize();
hitEntity.applyImpulse(rayDir.scale(100), localOffset);
``````

ok thanks

@Leonidas I tried it and got this error
[POW%20POW.js?id=40420369&branchId=1303e9b3-0978-47b7-b1c7-7f286697e2fb:41]: Uncaught TypeError: Cannot read property ‘getWorldTransform’ of undefined

Yes, because you don’t have any variable named entity in your code.

Try using this.entity instead. Unless you want to apply the hit on the hit entity then use result.entity.

@Leonidas I tried that and told me applyImpulse is undefined

It seems you don’t have the right order of execution in your code:

Reverse the following lines:

``````entity.applyImpulse(rayDir.scale(100), localOffset);
var entity = result.entity;
``````

To:

``````var entity = result.entity;
entity.applyImpulse(rayDir.scale(100), localOffset);
``````

Still line 45 comes before 46, be careful and read through your code.

You try to apply impulse both on line 45 and on line 47.

thanks now its working great

1 Like

hey @Leonidas while i have your attention do you know a way to full screen the game

Sure, you can use the browser fullscreen API, just note that it isn’t supported everywhere (on mobile especially):

https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API

i used this code and it gave me an error

`Element.requestFullscreen()`

Also @Leonidas can you help with this Help With A Follow Me AI

Study the doc I sent, to enable fullscreen mode you need to do so as a response to a user interaction e.g. on the click of a button.

Here is a good Playcanvas tutorial on how to add a UI button and execute code on click:

https://developer.playcanvas.com/en/tutorials/ui-elements-buttons/

1 Like