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.
Offline
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
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?
Offline
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!
Offline
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)
Offline
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
Offline
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 endWhat would I set point 1 x and point 2 x to if I only have 1 x coordinate
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.
Last edited by SciTecCf (2012-07-04 16:11:00)
Offline
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 endWhat would I set point 1 x and point 2 x to if I only have 1 x coordinate
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.
I'm still terribly confused ):
Offline
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! (:
Offline