Jump to content

Mouse moves track


axxion
 Share

Recommended Posts

i use this code but if mouse pointer stop object always chills

function update() {game.physics.arcade.collide(balls,platforms);   if (game.input.mousePointer.isDown)    {        balls.forEach(game.physics.arcade.moveToPointer, game.physics.arcade, false, 900);    }    else    {        balls.setAll('body.velocity.x', 0);        balls.setAll('body.velocity.y', 0);    }		 }

and if i use code this style, object gone in screen immediately

<!doctype html> <html lang="en"> <head> 	<meta charset="UTF-8" />    <title> game</title>	<script type="text/javascript" src="js/phaser.min.js"></script>    <style type="text/css">        body {            margin: 0;        }    </style></head><body><script type="text/javascript">var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });function preload() {    game.load.image('ball', 'assets/sprites/shinyball.png');  game.load.image('ground', 'assets/platform.png');}var balls;function create() {game.world.setBounds(0, 0, 2000, 2000);    game.physics.startSystem(Phaser.Physics.ARCADE);		game.stage.backgroundColor = '#0072bc';    balls = game.add.group();    balls.enableBody = true;    var ball = balls.create(0, 0, 'ball');       platforms = game.add.group();	platforms.enableBody = true;		    var ledge = platforms.create(400, 400, 'ground');    ledge.body.immovable = true;	}function update() {game.physics.arcade.collide(balls,platforms);   if (game.input.mousePointer.isDown)    {        balls.x = game.input.mousePointer.x;balls.y = game.input.mousePointer.y;    }    else    {        balls.setAll('body.velocity.x', 0);        balls.setAll('body.velocity.y', 0);    }		 }</script></body></html>
Link to comment
Share on other sites

You are saying a object not have this two features same time: mouse track and collision, right?

 

And thanks for replys, i want tutorial or document for this style issues, and maybe u have some suggestion for me physics in 2d games etc...

 

i want mouse track with a object and this object collide other objects.

 

thanks

Link to comment
Share on other sites

Yeah basically, collisions rely on the object colliding having a velocity. If you're telling the object every frame what its x and y positions should be, then the object has no velocity to calculate, and collisions will not occur... or will occur but go crazy, depending how and when in the frame you do it. You need something more like the 'moveToPointer' code (which sets the velocity such that it moves to the pointer position) but only when the pointer is held down on the object, and somehow smooth out that weird vibrating/orbiting issue that happens when it reaches the pointer.

Link to comment
Share on other sites

The usual way to do this is to make the object that is tracking the mouse be connected to an invisible point with a joint.

You move the invisible point to match the mouse pointer, then the joint makes the tracking object follow closely.

 

The difference it makes to the physics is that you are no longer directly moving the collision entity, so the physics can stretch/break the joint if it needs to in order to stop impossible situations.

 

(A simple "impossible situation" example:  you move the mouse through a solid wall... the physics collisions say "that can't happen" but the object is attached to your mouse so it has to happen... result = broken physics).

 

With the invisible point solution, the mouse moves through the wall, and the invisible (non-colliding point) moves through the wall too, but the joint stretches or breaks so the tracking object stops when it hits the wall.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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