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

#1 2012-07-04 15:33:03

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Is there an algebraic formula for the direction of a sprite?

I was wondering if there was any way to determine the direction of a sprite based on it's x and y coordinates. For example, (x-y*-1 = direction). Something like that. That's not the actual formula, but I was wondering if there is a correct one? I know I could just look at the direction its pointed in, but that's not the direction I'm looking for. I'm trying to use calculations so that I can make a sprite point towards the direction of a stamped sprite based on it's x and y coordinates, which will be stored in variables for use in the formula.Thanks.


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#2 2012-07-04 15:38:13

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Is there an algebraic formula for the direction of a sprite?

So, you want it to point away from the middle?


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&display=small

Offline

 

#3 2012-07-04 15:41:24

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

SciTecCf wrote:

So, you want it to point away from the middle?

I don't know what that means ):


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#4 2012-07-04 15:45:49

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

A sprite's position is much different from its direction.  If by direction, you mean the direction it would face if it were pointing toward the origin (0,0), then you can use a bit of trig to calculate the angle based on the slope of the line connecting the origin to the sprite.

EDIT: 500th post!

EDIT 2: Oh, wait, I see now.  You want the direction between two coordinates.  I'll post again later with an explanation of how to do that (it's not so complicated).

Last edited by amcerbu (2012-07-04 15:47:24)

Offline

 

#5 2012-07-04 15:49:57

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Is there an algebraic formula for the direction of a sprite?

Zparx wrote:

SciTecCf wrote:

So, you want it to point away from the middle?

I don't know what that means ):

Like this?


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&display=small

Offline

 

#6 2012-07-04 15:50:22

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

amcerbu wrote:

A sprite's position is much different from its direction.  If by direction, you mean the direction it would face if it were pointing toward the origin (0,0), then you can use a bit of trig to calculate the angle based on the slope of the line connecting the origin to the sprite.

EDIT: 500th post!

YES! That's what I want to do. Do you know of any way I can do this?


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#7 2012-07-04 15:55:58

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

amcerbu wrote:

A sprite's position is much different from its direction.  If by direction, you mean the direction it would face if it were pointing toward the origin (0,0), then you can use a bit of trig to calculate the angle based on the slope of the line connecting the origin to the sprite.

EDIT: 500th post!

EDIT 2: Oh, wait, I see now.  You want the direction between two coordinates.  I'll post again later with an explanation of how to do that (it's not so complicated).

"

Okay, phew! It's a really neat project and I only need a bit of complex math like that to make it work. Thank you!


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#8 2012-07-04 15:57:10

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Is there an algebraic formula for the direction of a sprite?

Zparx wrote:

amcerbu wrote:

A sprite's position is much different from its direction.  If by direction, you mean the direction it would face if it were pointing toward the origin (0,0), then you can use a bit of trig to calculate the angle based on the slope of the line connecting the origin to the sprite.

EDIT: 500th post!

YES! That's what I want to do. Do you know of any way I can do this?

set [delta_x v] to ((point 2 x) - (point 1 x))
set [delta_y v] to ((point 2 y) - (point 1 y))
if<(delta_y) = [0]>
 if<(delta_x) < (0)>
  set [dir v] to (-90)
 else
  set [dir v] to (90)
 end
else
 if<(delta_y) < (0)>
  set [dir v] to ((180) + ([atan v] of ((delta_x) / (delta_y))))
 else
  set [dir v] to ([atan v] of ((delta_x) / (delta_y)))
 end
end

Last edited by SciTecCf (2012-07-04 15:59:11)


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#9 2012-07-04 16:05:28

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

SciTecCf wrote:

Zparx wrote:

amcerbu wrote:

A sprite's position is much different from its direction.  If by direction, you mean the direction it would face if it were pointing toward the origin (0,0), then you can use a bit of trig to calculate the angle based on the slope of the line connecting the origin to the sprite.

EDIT: 500th post!

YES! That's what I want to do. Do you know of any way I can do this?

set [delta_x v] to ((point 2 x) - (point 1 x))
set [delta_y v] to ((point 2 y) - (point 1 y))
if<(delta_y) = [0]>
 if<(delta_x) < (0)>
  set [dir v] to (-90)
 else
  set [dir v] to (90)
 end
else
 if<(delta_y) < (0)>
  set [dir v] to ((180) + ([atan v] of ((delta_x) / (delta_y))))
 else
  set [dir v] to ([atan v] of ((delta_x) / (delta_y)))
 end
end

What would I set point 1 x and point 2 x to if I only have 1 x coordinate  hmm


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#10 2012-07-04 16:10:50

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Is there an algebraic formula for the direction of a sprite?

Zparx wrote:

SciTecCf wrote:

Zparx wrote:

YES! That's what I want to do. Do you know of any way I can do this?

set [delta_x v] to ((point 2 x) - (point 1 x))
set [delta_y v] to ((point 2 y) - (point 1 y))
if<(delta_y) = [0]>
 if<(delta_x) < (0)>
  set [dir v] to (-90)
 else
  set [dir v] to (90)
 end
else
 if<(delta_y) < (0)>
  set [dir v] to ((180) + ([atan v] of ((delta_x) / (delta_y))))
 else
  set [dir v] to ([atan v] of ((delta_x) / (delta_y)))
 end
end

What would I set point 1 x and point 2 x to if I only have 1 x coordinate  hmm

To get the direction between 2 points.

I apologize for my misunderstanding of so many things. Do you want it to point towards x0 y0? If so, just take out the "turn cw 180 degrees" block at the end of the script in the project I linked to earlier.  wink

Last edited by SciTecCf (2012-07-04 16:11:00)


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#11 2012-07-04 23:07:21

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

SciTecCf wrote:

Zparx wrote:

SciTecCf wrote:


set [delta_x v] to ((point 2 x) - (point 1 x))
set [delta_y v] to ((point 2 y) - (point 1 y))
if<(delta_y) = [0]>
 if<(delta_x) < (0)>
  set [dir v] to (-90)
 else
  set [dir v] to (90)
 end
else
 if<(delta_y) < (0)>
  set [dir v] to ((180) + ([atan v] of ((delta_x) / (delta_y))))
 else
  set [dir v] to ([atan v] of ((delta_x) / (delta_y)))
 end
end

What would I set point 1 x and point 2 x to if I only have 1 x coordinate  hmm

To get the direction between 2 points.

I apologize for my misunderstanding of so many things. Do you want it to point towards x0 y0? If so, just take out the "turn cw 180 degrees" block at the end of the script in the project I linked to earlier.  wink

I'm still terribly confused ):


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

#12 2012-07-05 05:38:08

Zparx
Scratcher
Registered: 2011-03-23
Posts: 500+

Re: Is there an algebraic formula for the direction of a sprite?

Nevermind! Thank you, SciTecCf! I tweaked around with your script and got it to work the way I wanted! Here's the project I put together with it! (:

http://scratch.mit.edu/projects/Zparx/2655203

I was sure to include your name in the Project Notes for credit! (:


http://images3.wikia.nocookie.net/__cb20101119183412/halo/images/4/43/Hero2.png
^ My rank on Halo: Reach :3

Offline

 

Board footer