Sign in to follow this  
bigboy

P2 physics for top-down tank motion

Recommended Posts

The best example I've found of what I'm trying to achieve is this - http://schteppe.github.io/p2.js/demos/topDownVehicle.html

 

However, instead of a car I have a tank, so the movement should be more... tankish, without this front-wheels-turning effect. But this is not a problem. The problem is that my tank's movement looks more like a boat on a water movement. I press UP for acceleration and when I press LEFT or RIGHT it still moves in the same direction for a while, like drifting. And if I release UP button and press LEFT or RIGHT again it drifts as well, without taking into account, that he has turned 90 degrees already. Looks awful in terms of a tank behaviour.

 

In other words, I want my tank to stay "on track" while turning, without such huge drift (or asteroid in space) effect.

 

I suppose this is something I should do in update section. Here is what I have there for now:

    if (cursors.left.isDown) {tank.body.angularVelocity = -0.75;}    else if (cursors.right.isDown) {tank.body.angularVelocity = 0.75;}    else {tank.body.setZeroRotation();}    if (cursors.up.isDown) {tank.body.thrust(120);}    else if (cursors.down.isDown) {tank.body.reverse(70);}        else {tank.body.damping = 0.8;}

cursors = game.input.keyboard.createCursorKeys();

 

 

And a few more, not related to main topic, questions:

  1. What kind of P2 library Phaser is using? Is it the same as in the example I've provided in the beginning?
  2. Is it technically possible in Phaser to calculate collision angle between shell and an object, e.g. another tank?

 

Thank you.

Share this post


Link to post
Share on other sites

Yes I saw it and actually used some parts as a starting point. Answering your question, I wanna use P2 because it gives a better body handling - it actually turns it, when arcade physics just pretend to turn the actual physical body of a tank (it turns an image, but if you use debug you'll see what i'm talking about). And the reason for that is that my tanks are not perfect square shapes, they are rectangles. Moreover, I have a plan to use PhysicsEditor to shape some of the tanks and give them some angles. So that's why I'm not using arcade.

Share this post


Link to post
Share on other sites

I've run into this problem too. Can't seem to get vehicle movement in Phaser using acceleration of velocity from rotation. copied the tank example exactly but I think it only works with certain velocity increments, angle increments, dimensions, etc. 

How do you accelerate via rotation in such a way that there is no drift effect? You basically need to translate the x/y velocity to the new rotation instead of simply adding to it when the sprite rotates. Surely someone using Phaser has attempted top down vehicle movements ... heck even top down aircraft movement needs to not drift. Having just started on a new physics-centric game, I'm baffled by this and am surprised there aren't any examples. 

 

Share this post


Link to post
Share on other sites

The problem is simply that your tank slows down too quickly. The 'undesirable' behaviour you are describing is exactly how objects move, velocity does not require the presence of force.

I can't remember the exact terminology in P2 but you want to increase damping (or some other variable, maybe friction would do it), that means that with no forces acting upon the body it will stop very quickly. Old-school movement methods that simply apply velocity directly (ignoring force) actually simulate this very well.

I'll accept that tank movement is slightly more complex as it uses tracks rather than wheels but a good simplification is to simply ensure that the vehicle is at rest before turning, usually this is what people expect of tank movement even if it is not a strict simulation.

Share this post


Link to post
Share on other sites
On 2/28/2016 at 5:23 AM, mattstyles said:

I can't remember the exact terminology in P2 but you want to increase damping (or some other variable, maybe friction would do it), that means that with no forces acting upon the body it will stop very quickly. 

 

Is what you mean drag? Tried that and doesn't have the desired effect (of simulating track movement). 

Share this post


Link to post
Share on other sites

Nope its not drag, I was pretty sure it was damping, but you might be able to simulate it with friction against the ground surface. 

You just need to be able to the stop the momentum, or velocity, of the tank very quickly when acceleration is not applied. You might also want to halt acceleration whenever an angular force (turning) is applied. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.