Jump to content

Basic Physics Problems (on my end)


SIRSizzlebottom
 Share

Recommended Posts

Howdy ladies and gents,

I am brand spanking new to phaser (as are most folks that post here I'd assume) and have run into some trouble while trying to come up with a quick prototype for a game that i have very limited time to release. My game programming experience is mainly hobbyist C#/XNA with a bit of C++ graphics programming so this whole "use someone else's engine" thing is a little alien to me. But I love being uncomfortable and so far I love how crazy and backwards this experience has been.

Now that the obligatory introduction in first question and mandatory small talk requirements have been met lets get down to the meat of my problem.

 

I'm trying to be able to fling a sprite by clicking/touching, dragging and having the sprite follow and releasing at which an applied velocity moves the sprite until gravity and drag do its thing to create a realistic physics simulation of what would happen.

 

I have the physics enabled like this:

game.physics.enable(player, Phaser.Physics.ARCADE);player.body.collideWorldBounds = true;player.body.maxVelocity.setTo(300, 300);player.body.drag.setTo(100, 100);player.body.gravity.setTo(0, 300);
 
input set up like this:
 
player.inputEnabled = true;player.input.enableDrag();player.events.onDragStart.add(startDrag, this);player.events.onDragStop.add(stopDrag, this);function startDrag() {   player.body.moves = false;}    function stopDrag() {    player.body.velocity.y = 0;    player.body.velocity.x = 0;    player.body.moves = true;}

the velocity.x && velocity.y = 0 thing was my way of stopping a weird thing that happened where a fling would not even occur if you did a uturn with the mouse and it would just drop the sprite as opposed to the current let fly feature.

 

and nothing at the moment in my update function as I found out that I don't actually have to calculate momentum when the user releases the sprite and stuff like that. I'm hitting several problems with this.
1. the sprite flies at a steady speed. I thought setting drag and gravity.would slow him down automatically. he flies steadily in both directions.
2: the sprite collides with the world bounds then continues to slide along them as if stuck on a single axis. doesn't fall down from the ceiling and will physically stop moving once it hits a corner.
3. the sprite bounces on the floor for some strange reason.
 
I think that's about all i can remember right now. I can post the full code although that really is the most interesting bit. The test screen is just blank with world collision on and a single sprite. nothing fancy.
I'd really appreciate it if someone could help me out. I would love to have this prototype working so we can start having fun with it.
 
Link to comment
Share on other sites

I think to make this work effectively what you'd need to do is calculate the distance and angle the mouse moved in a small period of time before release, rather than relying on the physics code to interpolate the very last frame of movement to determine the final velocity at release. This is a method commonly known as 'low pass filtering' where you take broader averaged readings of the results to reduce high frequency errors such as noise. If you take a sample of the last 100ms or so (you may have to tweak the sample period) and determine the average angle and distance this should give you better results.

 

Also, with maxVelocity set to 300 (a pretty low value) you'll probably need higher gravity and drag settings to notice them taking effect I'd imagine? The effects you're describing make me think maxVelocity is a clamp applied to the actual velocity, though there's nothing I can see in the code that suggests that is the case.

 

It would be useful if you could upload the code - the best way would be to post a jsfiddle/codepen example so it can be tweaked.

Link to comment
Share on other sites

so essentially you're telling me to remove the ARCADE physics model being applied onto the sprite's velocity at the initial release of the drag, calculate momentum and send the sprite off then re-enable physics? Would setting velocity.X & Y be enough to pull this off? I'll give it a go when i get back home and i'll post the 30 lines of code. 

 

as for the maxVelocity thing, i think i'm having problems with that because of a typo somewhere that I can't catch.

Link to comment
Share on other sites

No not at all - by all means use Arcade physics, but rather than relying on the movement from one frame before release (which could really go anywhere) I'd highly recommend you take an average sample of several frames before release. A longer sample will produce a more consistent and predictable trajectory, though too much and you'll not capture any sudden changes in direction. 100ms (roughly 6 frames) feels to me like it'd be about right.

Link to comment
Share on other sites

i restarted the project and rewrote pretty much what i had in a cleaner fashion and strictly following the order/structure in which the physics properties were set and such of the examples and im seeing many improvements. I've just been using whatever is on the backend of the dragEvent to determine where the sprite goes after release and it seems to be working fine for my needs at the moment.

 

There's still some weird stuff going on like the sprite bouncing off of the bottom Y world bounds and the speed value getting freaky when it hits any of the other bounds (getting stuck in them) but I can work around those. I think. Thanks a bunch lewster. See you around.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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