This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#26 2010-04-27 04:42:20

Phi_Lho
Scratcher
Registered: 2010-03-22
Posts: 75

Re: Why did this work (math question)

Wow, lot of answers... to say they don't understand the topic... People like to increase their message count, don't they?

juststickman wrote:

_x += Math.cos(Math.atan2(ship._y-this._y, .ship._x-this._x)*-1)*5
_y -= Math.sin(Math.atan2(ship._y-this._y, ship._x-this._x)*-1)*5

Why did that work?
I thought you were supposed to use sin for x and cos for y...

No, that's the reverse, cos is the horizontal projection of the angle and sin is the vertical one. See the diagrams in Wikipedia's Trigonometric functions for example.

Let's break down these formulae:
- ship._x - this._x is the horizontal distance between the ship and the player.
- ship._y - this._y is the vertical distance between the ship and the player.
- atan2 is used to compute an angle between these two objects. Not sure why it is inverted (*-1). Even less for y which is inverted then subtracted...
- Thus, cos of this angle is near zero when the objects are one above the other, and near 1 when they are (nearly) on the same horizontal axis. And that's the reverse for sin.
- So, multiplied by 5, it gives move units in the right direction.

Last edited by Phi_Lho (2010-04-27 07:57:51)


http://i241.photobucket.com/albums/ff159/PhiLho/KM150.pnghttp://i241.photobucket.com/albums/ff159/PhiLho/PhiLhoLogo.png

Offline

 

#27 2010-04-27 05:18:35

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: Why did this work (math question)

It worked as Atan function got u the slope of the enemy ship and then using the sin and cos functions u moved it in the direction  big_smile


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#28 2010-04-27 11:07:37

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: Why did this work (math question)

Oh I just went absent minded for a few days ^.^"

EDIT: just one thing then... how come it's the other way with scratch?

Last edited by juststickman (2010-04-27 11:12:40)


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

#29 2010-04-27 17:43:46

TheSaint
Scratcher
Registered: 2008-11-04
Posts: 1000+

Re: Why did this work (math question)

juststickman wrote:

Oh I just went absent minded for a few days ^.^"

EDIT: just one thing then... how come it's the other way with scratch?

...its not.

I like how everyone ignored my answer, and then said that Phi Lo's was the best, even though mine said the exact same thing. No Offense Phi Lo.

Offline

 

#30 2010-04-27 17:46:14

joeisawesome
Scratcher
Registered: 2008-06-10
Posts: 1000+

Re: Why did this work (math question)

lol, sorry, only taking Algebra 1.


http://image.hotdog.hu/user/Alex-21/Zenekarok-Ensiferum.jpg

Offline

 

#31 2010-04-28 11:00:00

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: Why did this work (math question)

joeisawesome wrote:

lol, sorry, only taking Algebra 1.

Me too.

That's why I was confused.


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

Board footer