Sign in to follow this  

Applying Force/ Impulse to P2 Physics Body on collision

Recommended Posts

I have a p2 Object with the following properties, true);
			this.player.body.allowRotation = false;
			this.player.body.fixedRotation = true;
			this.player.body.immovable = true;
this.player.body.kinematic = true;[0].sensor = false;
			this.player.body.collideWorldBounds = true;

The above object can only move along its X axis and I want to trigger push/ hit force to its impacting object on collision;
			this.ball.body.collideWorldBounds = true;
			this.ball.body.allowRotation = true;
			this.ball.body.fixedRotation = false;

The above object is always the impacting object whose movement is controlled by gravity and impulse.

I'm trying to figure out how to calculate the following:-

  • The point of collision on the body of "this.player"
  • The impulse to apply to the impacting object after collision to simulate a proper/ accurate deflection setting the projected angle and speed/ velocity
  • what properties of the impacting object do I set? Force? AngularVelocity? Velocity?



Share this post

Link to post
Share on other sites



Been researching and tinkering around and came up with this

this.player.body.onBeginContact.add(function(body, bodyBody, shapeA, shapeB, equation )

                        let mass = .53 // mass of a fist punch
						let duration = 2; // default 2 ms
						let localX = equation[0].contactPointB[0];
						let localY = equation[0].contactPointB[1];
						if(this.spaceKey.isDown) // when the user press punch button
						  duration = this.spaceKey.duration; // length of time/ power

						let impulse = [	(mass * this.player.body.velocity.x) * duration,
  ((mass * 2) * duration) * -1 // using a static velocity of 2 for a "punch" multiply buy the time the user stays pressing the key
						body.applyImpulse(impulse,localX,localY); // give the object momentum change
			}, this);


Not sure if the properties used are correct but I'm getting closer. Not sure of the x and y properties to be using it the punch object or the ball contact point

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.