Jump to content

Using AngularVelocity, "snap" to angles that are multipliers of 90


Aeon
 Share

Recommended Posts

I'm currently using p2 to move a ship in a similar way as shown in the image above

01.gif

(Note: the demo above only looks like it works, but it actually moves in basically any random direction except the one is facing)

 

The problem number one is the sprite of the ship. The angle "cero" seems to be by default facing up in p2, but in my sprite it's not quite looking up.

 

spaceship.png?raw=true

 

The second issue comes with the "snapping". I wan't to make the ship always face either 45degrees, -45degrees, 135degrees or -135degrees. This was the idea the hole time. I'm thinking that maybe I'm over-complicating myself, and I should have just make the sprite flip and change the direction at once, but now that I've seen how it looks like when the sprite "slowly rotates" from one direction to the other, I would like to achieve the same result!

 

This is my code so far

 

 

on create()

//create player    player = game.add.sprite(game.world.centerX, game.world.centerY, 'ship');		//enable player physics		game.physics.p2.enable(player);    player.body.collideWorldBounds = true;		player.body.angle = (Math.PI/4);		//  By default the ship will collide with the World bounds,    //  however because you have changed the size of the world (via layer.resizeWorld) to match the tilemap    //  you need to rebuild the physics world boundary as well. The following    //  line does that. The first 4 parameters control if you need a boundary on the left, right, top and bottom of your world.    //  The final parameter (false) controls if the boundary should use its own collision group or not. In this case we don't require    //  that, so it's set to false. But if you had custom collision groups set-up then you would need this set to true.    game.physics.p2.setBoundsToWorld(true, true, true, true, false);		/*			Player animations			version 1.0				normal: the player is in first frame. May as well be not moving.				begin_fly: the player is begining to fly.				fly: the player is flying.		*/		player.anchor.set(0.5, 0.5);

on update()

if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))    {        player.body.rotateLeft(100);    }    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))    {        player.body.rotateLeft(-100);    }		else		{				player.body.setZeroRotation();				console.log(player.angle);		}		player.body.thrust(400); //MOVE FOWARD CONSTANTLY

TLDR:

 

The code does the trick okey so far but:

1. The player is facing a wrong direction

2. Once you stop pressing left or right, the player will remain in whathever angle it was left, instead of "snapping" to a 45degree angle.

 

 

Any idea, though, recomendation, etc. would be higly appreciated! :·3

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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