A formula to get to some point but continue past it...

Obviously the regular dist formula I’ve been using doesn’t work in this case:

[CODE] this.dx = this.destination[0]-x;
this.dy = this.destination[1]-y;

this.dist = (this.dx^2+this.dy^2)^.5;

if(this.dist>1){
x += (this.dx/this.dist)*this.speed;
y += (this.dy/this.dist)*this.speed;
}[/CODE]
With this, the object stops at the given point, I believe that getangle could help me out in this case but I know tricxta is way better in maths than I am, so before I spend the week trying to reinvent some wheel, I’m asking for help. Tnx :smiley:

EDIT:
I figured it out thanks to google and this website.

[CODE] this.dx = this.destination[0]-x;
this.dy = this.destination[1]-y;

this.angle = getangle(this.dx,this.dy);

this.u = {cos(this.angle),sin(this.angle)};

x += this.u[0]*this.speed;
y -= this.u[1]*this.speed;
[/CODE]

EDIT AGAIN:
This is just another way of calculating the direction, in fact all I needed to do was set this.dx and this.dy in if(created), stupid me.

[CODE]if (created) {
this.destination = {playerx,playery};
this.dx = this.destination[0]-x;
this.dy = this.destination[1]-y;
this.speed = .5;
timeout = .05;}

if(timeout){

this.angle = getangle(this.dx,this.dy);

this.u = {cos(this.angle),sin(this.angle)};

x += this.u[0]*this.speed;
y -= this.u[1]*this.speed;

if(x<1||y<1||x>64||y>64) destroy;

timeout = .05;
}[/CODE]

Someone merge this in useful formulas so this kind of stupidity doesn’t show itself again X_X… sometimes all you need is a good night’s sleep… and a little luck.

  this.u = {cos(this.angle),sin(this.angle)};

  x += this.u[0]*this.speed;
  y -= this.u[1]*this.speed;

What a stupid way of coding, where’d you get this from? o_0

[QUOTE=tricxta;98612]

What a stupid way of coding, where’d you get this from? o_0
[/QUOTE]

Play nice. Could you explain what you think could be improved and why?

Took the this.u formula from said website then figured out the x+ and y-.
Could I have used cos for both so they’re both using additions?, is that what you mean?
I haven’t tried actually.

What’s the point in having the array work out the values when you could save yourself a few lines and there’d be alot less confusion doing something like:

x+= cos(this.angle)*this.speed;
y-= sin(this.angle)*this.speed;

Also here’s the mathematical backing behind it just so you know.

When you learnt trigonometry in highschool you would’ve learnt that sin(angle) = opp/hyp and that cos(angle) = adj/hyp

so if we let the hypotenuse equal 1 then we’d be left with sin(angle) = opp and cos(angle) = adj, through my triangle labelling you can obviously start to see how it works. The adjacent(adj) part is how far we have to move across and our opposite(opp) tells us how far we have to move up.

I learned more about my math teacher’s personal life than anything else in highscool maths.
Bad teachers I tell ya, the books were way better, in fact I got my way out of a course I was flunking by actually forgetting all the stupid things my teacher said and learning it again from the book (all that in 2 days compared to a full school year of listening to a teacher, cuz that year I was staying awake).

So to me cos and sin were a function on a calculator and we learned which specific cases required us to press that button.

I wish I had those school books handy.

i know my triangles