Jump to content

Weird physics with a modification of the tank game


rodrigouroz
 Share

Recommended Posts

Hi all

 

I'm trying to build a tank game for an Artificial Intelligence simulation (so the game will be played by AI instead of a human).

 

I got the tank game example and modified it a little bit but I'm stuck with a problem I can't sort out. When my tank is moving and I start to rotate, the tank continues to move in the same direction it was moving, with little or no drift at all. It's hard to explain so I'm posting the source code here just in case anyone can take a look and shed some light.

 

Thanks in advance,

 

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game-canvas', { preload: preload, create: create, update: update });function preload () {    game.load.atlas('tank', 'assets/tanks.png', 'assets/tanks.json');    game.load.image('earth', 'assets/scorched_earth.png');}var land;var shadow;var tank;var turret;var currentSpeed = 0;var cursors;function create () {    //  Resize our game world to be a 2000 x 2000 square    game.world.setBounds(-1000, -1000, 2000, 2000);    //  Our tiled scrolling background    land = game.add.tileSprite(0, 0, 800, 600, 'earth');    land.fixedToCamera = true;    //  The base of our tank    tank = game.add.sprite(0, 0, 'tank', 'tank1');    tank.anchor.setTo(0.5, 0.5);    tank.animations.add('move', ['tank1', 'tank2', 'tank3', 'tank4', 'tank5', 'tank6'], 20, true);        //  Finally the turret that we place on-top of the tank body    turret = game.add.sprite(0, 0, 'tank', 'turret');    turret.anchor.setTo(0.3, 0.5);    //  This will force it to decelerate and limit its speed    game.physics.enable([tank, turret], Phaser.Physics.ARCADE);        tank.body.drag.set(100);    tank.body.maxVelocity.set(250);    tank.body.collideWorldBounds = true;    tank.body.maxAngular = 50;    tank.body.angularDrag = 50;    turret.body.maxAngular = 200;    turret.body.angularDrag = 80;    //  A shadow below our tank    shadow = game.add.sprite(0, 0, 'tank', 'shadow');    shadow.anchor.set(0.5, 0.5);    tank.bringToTop();    turret.bringToTop();    game.camera.follow(tank);    //game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300);    game.camera.focusOnXY(0, 0);    cursors = game.input.keyboard.createCursorKeys();}function update () {    if (cursors.left.isDown) {        tank.body.angularAcceleration -= 20;    }    else if (cursors.right.isDown) {        tank.body.angularAcceleration += 20;    } else {        tank.body.angularAcceleration = 0;    }    if (cursors.up.isDown) {        tank.throttle += 10;        if (tank.throttle > 60) {            tank.throttle = 60;        }        tank.body.acceleration.x = Math.cos(tank.rotation) * tank.throttle;        tank.body.acceleration.y = Math.sin(tank.rotation) * tank.throttle;    } else {        tank.body.acceleration.set(0, 0);        tank.throttle = 0;    }    if (game.input.keyboard.isDown(Phaser.Keyboard.A)) {        turret.body.angularAcceleration = -150;        } else if (game.input.keyboard.isDown(Phaser.Keyboard.S)) {        turret.body.angularAcceleration = 150;        } else {        turret.body.angularAcceleration = 0;    }    land.tilePosition.x = -game.camera.x;    land.tilePosition.y = -game.camera.y;    //  Position all the parts and align rotations    shadow.x = tank.x;    shadow.y = tank.y;    shadow.rotation = tank.rotation;    turret.x = tank.x;    turret.y = tank.y;}
 
Link to comment
Share on other sites

I think the problem is that you're driving the forward motion of your tank only in your cursor.up.isDown check, whereas the example only sets the speed there. The forward momentum is then worked out every updateby checking the speed and applying it in the direction the tank is facing:

    if (currentSpeed > 0)    {        game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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