gnarvin Posted August 1, 2014 Share Posted August 1, 2014 Hello again, Im trying to set up my game so that when an enemy gets within a certain distance of the player they start to chase the player. Im trying to use Math.distance but keeps telling me undefined is not a function. Right now I just want to most basic form of this to work so I tried (this is what function runs from the forEach in the update.)var enemy;if(Math.distance(this.player.x, this.player.y, enemy.x, enemy.y) <= 10){//enemy moves towards player}I thought this would give me the distance between the player and each enemy and if within 10 it would run the proper code, but I'm not doing something right obviously. Also another question, what a the easiest or best way to determine knock back, Like when played hits an enemy how do I make player bounce back according to the direction they collide. Also same goes for the emitter is there a somewhat simple way to determine the direction of the bullet so the blood splatter makes sense? Link to comment Share on other sites More sharing options...
Dumtard Posted August 1, 2014 Share Posted August 1, 2014 Are you using javascript's Math object or Phaser's Math object? It appears you are using javascript's which does not have a distance function, that is why it is telling you undefined is not a function, because it is not defined. You can use Phaser though http://docs.phaser.io/Phaser.Math.html#distance. esteban.guelvenzu 1 Link to comment Share on other sites More sharing options...
esteban.guelvenzu Posted August 1, 2014 Share Posted August 1, 2014 The Math.distance function is pretty simple: distance: function (x1, y1, x2, y2) { var dx = x1 - x2; var dy = y1 - y2; return Math.sqrt(dx * dx + dy * dy); }, are you sure "enemy" is an object with an "x" and "y" property defined? Link to comment Share on other sites More sharing options...
esteban.guelvenzu Posted August 1, 2014 Share Posted August 1, 2014 Dumtard is right. console.log (Math.distance(0 , 0 , 100 , 0));TypeError: Math.distance is not a function console.log (Phaser.Math.distance(0 , 0 , 100 , 0));100 Link to comment Share on other sites More sharing options...
gnarvin Posted August 2, 2014 Author Share Posted August 2, 2014 Awesome, I knew it was something really obvious, for some reason it didn't click that I needed the Phaser before Math. Thank you so much, this helps me understand how to use the Phaser docs a little better too. Link to comment Share on other sites More sharing options...
szocske Posted August 2, 2014 Share Posted August 2, 2014 For knockback and splatter just subtract the shooter's coordinates from the players coordinates, and normalize with the distance you just learned how to calculate :-)If player coordinates are P=(xp, yp), shot by someone at O=(xo, yo), then the unit-length vector pointing the way the the player's internals will splatter through the exit wound is V=(xo-xp/distance(P,O),yo-yp/distance)Multiply by a factor proportionate to the strength of the weapon* and add to the speed of the player (assuming you are working with speeds and drag). That's some gruesome math right there, I'm sure kids would listen more at school if teachers used examples like this :-)*: and be eager to learn about the difference between impulse and kinetic energy to get even more realistic knockbacks :-) dthrasher90, drhayes and lewster32 3 Link to comment Share on other sites More sharing options...
InsaneHero Posted August 2, 2014 Share Posted August 2, 2014 Lovely gruesome example there szocske, but you need some extra brackets or the division will be done before the subtraction (remember BODMAS)! V = ((xo-xp)/distance(P,O),(yo-yp)/distance); lewster32 1 Link to comment Share on other sites More sharing options...
gabdab Posted April 22, 2016 Share Posted April 22, 2016 Avoid sqrt if possible by using squared distance (ex: dist = 2 ; sqdist = 4): functiondistanceSq(object,target) { var xDif = object.x - target.x; var yDif = object.y - target.y; return (xDif * xDif) + (yDif * yDif); }; Link to comment Share on other sites More sharing options...
Coast2Coast Posted April 22, 2016 Share Posted April 22, 2016 also you can take a look to the method distanceToXY or distanceBetween (http://phaser.io/docs/2.3.0/Phaser.Physics.Arcade.html#distanceToXY) Link to comment Share on other sites More sharing options...
WombatTurkey Posted April 22, 2016 Share Posted April 22, 2016 Someone necro'd a nearly 2 year old thread drhayes 1 Link to comment Share on other sites More sharing options...
Recommended Posts