Phaser: Gravity originating from the body

I added a body that should have gravity for my game, so draw a large blank screen with a circle for Earth in the middle.

What methods will allow me to add to the game any other body added to the game, “accelerated” or “attracted” to this circle? Basically, if an asteroid appears, it should maintain its initial speed, but it is affected by Earth's gravity.

+5
source share
1 answer

I believe I found this method that you are looking for here .

I also have an example of this method in action here .

Here is the source code for my example:

// Global constants var GAME_WIDTH = 800; var GAME_HEIGHT = 600; var SHIP_X_POS = 100; var SHIP_Y_POS = 200; var PLANET_X_POS = 400; var PLANET_Y_POS = 300; var ACCELERATION_TOWARDS_PLANET = 500; var SHIP_VELOCITY_X = 150; var SHIP_VELOCITY_Y = 150; // Global variables var ship; var planet; var game = new Phaser.Game(GAME_WIDTH, GAME_HEIGHT, Phaser.AUTO, "game", {preload: preload, create: create, update: update}); function preload () { game.load.image("ship", "sprites/phaser_ship.png"); game.load.image("planet", "sprites/planet.png"); } function create () { var ship = game.add.sprite(SHIP_X_POS, SHIP_Y_POS, "ship"); game.physics.arcade.enable(ship); ship.body.velocity.x = SHIP_VELOCITY_X; ship.body.velocity.y = SHIP_VELOCITY_Y; var planet = game.add.sprite(PLANET_X_POS, PLANET_Y_POS, "planet"); game.physics.arcade.enable(planet); planet.body.immovable = true; game.physics.arcade.accelerateToObject(ship, planet, ACCELERATION_TOWARDS_PLANET); } function update () { // nothing to update } 
+3
source

Source: https://habr.com/ru/post/1202509/


All Articles