Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

7 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

7 years ago by Joncom

Maybe you're trying to move the ball faster than maxVel.x and maxVel.y allow? If so, the speed will be limited, and it might happen on a particular axis or in an unexpected way?

http://impactjs.com/documentation/class-reference/entity#maxvel-x-maxvel-y

7 years ago by Naty

Thank you very much for your help Joncom, I think you are right. By slowing the speeds my entity (ball) began to move in all directions without problem. I am very happy with the result :)

7 years ago by Joncom

Hi Naty. You can still use fast speeds if you want, but just make sure you set your maxVel higher so that the speed does not get capped.

EntityExample = ig.Entity.extend({
    ...
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        this.maxVel.x = 99999;
        this.maxVel.y = 99999;
        ...
    },
    ...
});
Page 1 of 1
« first « previous next › last »