Skip to content
← Back to Community
How to make the ball take one straight line and not two paths?
Profile icon
HackingGo306

If you click on the grey background in the program, the ball will move to your mouse position.

The question is: how do I make the ball move to my mouse position in one straight path?


Usually, the ball moves in one diagonal path and then one horizontal or vertical line to reach my mouse position. How do I make it so it only takes one path instead of two?

Answered by SixBeeps [earned 5 cycles]
View Answer
Voters
Profile icon
DavidChoinacky
Profile icon
HackingGo306
Comments
hotnewtop
Profile icon
SixBeeps

How familiar are you with trigonometry?

Profile icon
HackingGo306

@SixBeeps Not really

Profile icon
SixBeeps

@HackingGo306 Do you know the distance formula at least?

Profile icon
HackingGo306

@SixBeeps
var dist = function(x1,y1,x2,y2){
var x_diff = x1 -x2
var y_diff = y1 - y2
distance = sqrt((x_diff · x_diff) + (y_diff · y_diff))
return distance
};

Are you talking about this?

Profile icon
SixBeeps

@HackingGo306 Yeah.
So, you'll calculate the distance and round it to a whole number. Then, find out the ratio of X distance to whole distance, and Y distance to whole distance. Loop whole distance times, and for each iteration, move by their respective ratios. That likely made no sense, so I'll put it into pseudocode form:

dist = distance between current position and target position xRatio = (target x - current x) / dist yRatio = (target y - current y) / dist repeat dist amount of times: move right by xRatio move down by yRatio wait a little bit
Profile icon
HackingGo306

@SixBeeps
Thanks!