Jump to content

Can't figure out how to use force/mass/accel/etc for natural motion


GreatBigBore
 Share

Recommended Posts

I'm trying to simulate something like a guy on roller skates going around and picking up objects on the floor. I don't know the terminology for the world orientation--the view is from overhead, so I'm not using any gravity physics. The up/down axis is from the computer screen to my eyes. I'm looking at the top of my skater's head. I hope that makes sense.

I'm using a sprite for my skater, and emitter particles as the objects. I'm trying to simulate something like friction on the floor, so my guy has to use up some energy to maintain his velocity. It seems like I need to give him some mass, which is easy enough with body.mass, but for the friction, I'm not clear on whether to use body.drag or body.damping, or both, or something else entirely -- experimenting with them, I can't really tell what they're doing. When my guy sees an object some distance away, I'd like to have him change his speed and direction in a natural way, so I'm thinking accelerateToObject(). Then when he has the object, the friction of the floor would slow him back down. For this it seems that I need to reset his acceleration to zero, which seems to be in body.acceleration.x and .y, although I'm not clear on whether these are doing what I think they're doing.

The basic problem is I can't quite grasp the relationships among acceleration, mass, force, damping, and drag, and I'm not clear on whether these are the right things for me to be using in the first place. If any of this is making sense, can someone give me any suggestions?

Link to comment
Share on other sites

To the code!

You didn't say, but I'm going to assume you're using Arcade physics.

Start here: https://github.com/photonstorm/phaser/blob/master/src/physics/arcade/Body.js#L556

Then go here: https://github.com/photonstorm/phaser/blob/master/src/physics/arcade/World.js#L248

That leads here: https://github.com/photonstorm/phaser/blob/master/src/physics/arcade/World.js#L270

Drag only takes affect if the body is not currently under acceleration (you can see it in computeVelocity in arcade.World.js). I wouldn't worry *too* much about mass at this point, since that's mostly related to collisions and stuff -- not movement. You should also probably check out maxVelocity, that'll be useful.

First, we calculate velocity as "velocity += acceleration * this.game.time.physicsElapsed". The cool thing about simplified 2d physics is it's equal across axes, so we can consider one dimension and still speak meaningfully. That line of code means that "velocity" and "acceleration" are defined as units per second (un its instead of pixels cuz you could zoom in and whatever, but you get my drift -- see what I did there). After all that, then the new velocity is applied to the position as "this.position.x += this.newVelocity.x;" (and so on for y). So acceleration modifies velocity, then velocity modified position. I bet you could ignore acceleration entirely and get a good feeling out of your rollerskating dude. It sounds like you don't want accelerateToObject since that sets acceleration, which means you wouldn't get the drag effect you want -- unless you set a meaningful maxVelocity, a high drag, then set acceleration to 0 like you're doing.

I feel a bit like I just repeated back to you what you wrote, but maybe with some good references?

 

Link to comment
Share on other sites

4 hours ago, drhayes said:

I feel a bit like I just repeated back to you what you wrote, but maybe with some good references?

The references help a lot, thanks.

4 hours ago, drhayes said:

To the code!

It will take me a while to read through all of it and understand how everything fits together. I'll start studying it, but if you have any thoughts on how I can get some immediate gratification, I would love to hear them. Here's the issue: I can use moveToObject(), but then the change of direction is jerky. That's why I wanted to use accelerateToObject(), so the direction change would be smooth. The reason for my original confusion is that accelerateToObject() often doesn't get my skater to the object. He usually gets there, but sometimes he just sort of moves in that direction but misses the target completely. I think there's something important about acceleration that's going over my head.

I'm going to start studying the library right now, in case your answer is that I just need to start studying the library! Thanks much for helping me get this far.

Link to comment
Share on other sites

The *toObject calls don't update past the initial call, they don't track the object as it moves. Is it that? Like you have to keep the object as a target? accelerateToObject is probably want you want for that slippery feel... but, at this point, I think it's feel and messing with the numbers.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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