Jump to content

Physics/ Collision


Shaam17
 Share

Recommended Posts

This is a simple  example of collision, unlike normal collisions, its is Circular body Collision.

Two  balls are made to move in random positions with certain velocity and they are bound to the Height and Width of the screen, on contact with its bounds the velocity increases and move in random directions,  When the 2 balls are in contact with each other they are separated by a certain velocity and moves again in certain velocity. The velocity of each ball is different after they are been in contact with each other.

Phaser3 Code:

var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 1000,

    physics: {
        default: 'arcade',
        arcade: {
            debug: true,
            gravity: { y: 100 }
        }
    },
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('ball1', 'assets/sprites/ball1.png');

this.load.image('ball2', 'assets/sprites/ball2.png');
}

function create ()
{
    var ball1 = this.physics.add.image(100, 240, 'ball1l');
    var ball2 = this.physics.add.image(700, 240, 'ball2');

    ball1.setCircle(46);
    ball2.setCircle(46);
    ball1.setCollideWorldBounds(true);
    ball2.setCollideWorldBounds(true);
    ball1.setBounce(1);
    ball2.setBounce(1);
    ball1.setVelocity(150);
    ball2.setVelocity(-200, 60);

    this.physics.add.collider(ball1, ball2);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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