Jump to content

Box2D plugin flinging a sprite issue


kleepklep
 Share

Recommended Posts

Hello,
 
I'm converting a Box2D project from Flash and World Construction Kit to HTML5 using Phaser and R.U.B.E. and ran into something I'm not sure how to solve.

 

I have round bodies that land on a kinematic body which is repositioned when the user clicks on it so that it flings them a little bit. Not quite like a catapult, but more like if you were on one end of a seesaw and you were lifted up quickly so you got some air. The bodies get air in the Flash version, but in the Phaser version they don't get any. I've attached videos to illustrate this.

 

I checked all of my variables (density, friction, gravity, etc.) and didn't find any differences. I tried changing the position of the platform more quickly, but that didn't help. I have other cases where I lift up a square body quickly to fling a round body when it hits a sensor above it. In my Flash version I don't have to do anything but that, but in Phaser I also have to use applyForce(x, y) on the body to compensate for the lack of power. That's an easy fix for that case, but for this one I'm not sure how I would handle it.

 

I suspect that World Construction Kit might be updating the linear and angular velocity of the platform when it is repositioned which might give it that extra kick. If that's the case and Phaser doesn't do this, I'll have to calculate and update those variables manually somehow. I'll dig into this deeper to see if this is what's up, but any insight would be much appreciated!

 

videos.zip

Link to comment
Share on other sites

I should mention that I am updating the y position of the platform using body.y. I'm doing this using a timer because I have to synchronize its movement with an animation.

 

None of these other options do anything to the kinematic body: body.applyForce(0, -100), body.moveUp(-100), body.velocity.y = -100

Link to comment
Share on other sites

  • 1 month later...

Here's what I wound up doing.

On my seesaw platforms I assign a collision category like so:

seesaw.body.setCollisionCategory(1);
On my ball objects I add a callback:

ball.body.setCategoryContactCallback(1, function (body1, body2, fixture1, fixture2, begin) {	if (begin) {		//console.log(body1.name, 'hit seesaw', body2.name);		body2.onBallLand(body1);	} else {		//console.log(body1.name, 'left seesaw', body2.name);		body2.onBallLeave(body1);	}}, this);
The onBallLand and onBallLeave functions add or remove balls from an array that tells me what's on a seesaw. I'm updating a variable that holds each ball's starting y position when it lands and when the seesaw starts moving. When the seesaw stops moving I calculate each ball's y distance moved and if it's negative lob it upward with applyForce(0, vy * 4). I tried using velocity.y initially but was getting readings that didn't make sense, like a positive when a ball was being lifted upward. This is likely because I am updating each seesaw's position in synch with an animation using animations.currentAnim.onUpdate and animations.currentAnim.onComplete which are firing less often that each box2d update.

Anyway, if anyone has a similar issue and needs some help let me know!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...