8 years ago by Naty
For a game of mini-golf, I read the position in which mouse is clicked (released). I calculate the difference between the position of the click and the position of the ball and with it assign a value to the speed in x and y axis but surprisingly the ball only moves in 4 possible directions which to be drawn form an X, ball entity does not move horizontal or vertical or in intermediate angles, Only at the angles that make up the X. If I assign velocity values using fixed numbers (not variables), then the direction of motion is free and works well. But I need to be able to define those values dynamically.
if (ig.input.released('click')) {
var EndX = ig.input.mouse.x;
var EndY = ig.input.mouse.y;
var dirx = this.pos.x+this.size.x/2-EndX; //mov direction x
var diry = this.pos.y+this.size.y/2-EndY; //mov direction y
var modulus = Math.sqrt(Math.pow(dirx,2)+Math.pow(diry,2));
this.vel.x = Math.round(100*dirx/modulus);
this.vel.y = Math.round(100*diry/modulus);
}
Please help and thanks
if (ig.input.released('click')) {
var EndX = ig.input.mouse.x;
var EndY = ig.input.mouse.y;
var dirx = this.pos.x+this.size.x/2-EndX; //mov direction x
var diry = this.pos.y+this.size.y/2-EndY; //mov direction y
var modulus = Math.sqrt(Math.pow(dirx,2)+Math.pow(diry,2));
this.vel.x = Math.round(100*dirx/modulus);
this.vel.y = Math.round(100*diry/modulus);
}
Please help and thanks