Discuss Scratch
- Discussion Forums
- » Suggestions
- » a [point towards x( ) y( ) block
- TheLogFather
-
1000+ posts
a [point towards x( ) y( ) block
I think the most compact is probably this:
EDIT: I see Zro has already said exactly that a while back…
define point towards (x) (y)
point in direction ( ( [atan v] of ( ( (x) - (x position) ) / ( (y) - (y position) ) ) ) + ( (180) * ( (y position) > (y) ) )
EDIT: I see Zro has already said exactly that a while back…
Last edited by TheLogFather (Jan. 4, 2016 19:55:40)
- Aboot4
-
100+ posts
a [point towards x( ) y( ) block
I posted that back in the day when I was a lot worse than I am now at Scratch xDDThere are easier ways to do a point towards block. Guys, come on. Here's the easiest way to do it, and do it well, bro:define Move (Steps) steps towards x: (X) y: (Y)
if <<(Y) > (y position)> or <(Y) = (y position)>> then
point in direction ([atan v] of (((X) - (x position)) / ((Y) - (y position))))
else
point in direction ((180) + ([atan v] of (((X) - (x position)) / ((Y) - (y position)))))
end
if <not <<(y position) = (Y)> and <(x position) = (X)>>> then
move (Steps) steps
end
- MathlyCat
-
1000+ posts
a [point towards x( ) y( ) block
There is no need for trig knowledge, if you want it to be exceptionally nice then yes some trig!
- pusheenkittens
-
100+ posts
a [point towards x( ) y( ) block
Easily workaroundable, but support anyway.
- scratchisthebest
-
1000+ posts
a [point towards x( ) y( ) block
No support because you can just add this to your backpack:
That's the whole point of the backpack, to allow you to save little code snippets like this one, and put them in your project when you feel like it.
define point towards (x) (y)
point in direction ( ( [atan v] of ( ( (x) - (x position) ) / ( (y) - (y position) ) ) ) + ( (180) * ( (y position) > (y) ) )
That's the whole point of the backpack, to allow you to save little code snippets like this one, and put them in your project when you feel like it.
- AwesomeSmilee
-
500+ posts
a [point towards x( ) y( ) block
Support. Gets annoying to use this every time-
define point towards x: (x) y: (y)Workaround from Scratch Wiki.
if <((y) - (y position)) = [0]> then
if <((x) - (x position)) < [0]> then
point in direction (-90 v)
else
point in direction (90 v)
end
else
if <((y) - (y position)) < [0]> then
point in direction ((180) + ([atan v] of (((x) - (x position)) / ((y) - (y position))))
else
point in direction ([atan v] of (((x) - (x position)) / ((y) - (y position)))
- Ninkancho
-
500+ posts
a [point towards x( ) y( ) block
“Easily” workaroundable? Those workarounds don't look easy. Complete support from me; why should pointing toward the mouse-pointer or another sprite be so easy when merely facing a point in space isn't?
- pusheenkittens
-
100+ posts
a [point towards x( ) y( ) block
Method 2: No support. Easily workaroundable.define point towards coordinates x: (x) y: (Y)See?
if <((Y) - (y position)) = [0]> then
if <(x) < (x position)> then
point in direction (-90 v)
else
point in direction (90 v)
end
else
if <((Y) - (y position)) > [0]> then
point in direction ([atan v] of (((x) - (x position)) / ((Y) - (y position))))
else
point in direction (([atan v] of (((x) - (x position)) / ((Y) - (y position)))) - (180))
end
end
when green flag clickedThe sprite is blank and is in the wanted direction. That doesn't slow down my projects.
point towards [sprite v]
- asivi
-
1000+ posts
a [point towards x( ) y( ) block
To calculate direction and distance from a point to another
define Direction&distance from x1 (x1) y1 (y1) to x2 (x2) y2 (y2)as you can see in direction&distance between 2 points
set [atan v] to ([atan v] of (((x2::custom) - (x1::custom)) / ((y2::custom) - (y1::custom))))
point in direction ((atan) + ((180) * <(y1::custom) > (y2::custom)>))
set [distance v] to (([abs v] of (((y1::custom) - (y2::custom)) / ([cos v] of (atan)))) + (([abs v] of ((x1::custom) - (x2::custom))) * <(y2::custom) = (y1::custom)>))
Direction&distance from x1 (1) y1 (1) to x2 (1) y2 (1) :: custom
- iamunknown2
-
1000+ posts
a [point towards x( ) y( ) block
Support; if we have a “glide” block, why can't we have this (which is much more difficult to program than the glide block)?
Last edited by iamunknown2 (April 29, 2016 13:26:45)
- Ferexes
-
24 posts
a [point towards x( ) y( ) block
Support.
It tends to annoy the heck out of me when I have to do the workaround.
It tends to annoy the heck out of me when I have to do the workaround.
- joefarebrother
-
500+ posts
a [point towards x( ) y( ) block
Support. There are 2 ways to workaround: One uses trig, and the other uses an extra sprite.
Using too many extra sprites is inelegant, and you can't expect all beginners to know trig.
If you're going to not support this, you can't support
Using too many extra sprites is inelegant, and you can't expect all beginners to know trig.
If you're going to not support this, you can't support
move () stepseither, becuase that's workaroundable with trig, and the trig workaround with this block is more complicated than move 10 steps is.
- theonlygusti
-
1000+ posts
a [point towards x( ) y( ) block
Okay, here is the workaround: No support. Workaroundable. Look them up.
set [Δx v] to (([x position v] of [wanted sprite v]) - (x position))
set [Δy v] to (([y position v] of [wanted sprite v]) - (y position))
if<(Δy) = [0]> then
if<(Δx) < [0]> then
point in direction (-90 v)
else
point in direction (90 v)
end
else
if<(Δy) < [0]> then
point in direction ((180) + ([atan v] of ((Δx) / (Δy))))
else
point in direction ([atan v] of ((Δx) / (Δy)))
end
end
As you can see guys, it is very obvious and intuitive, I can't believe New Scratchers don't use this more often, how silly of them. </sarcasm>
Guys, seriously, simply having a workaround is not a good reason to not support a block.
Last edited by theonlygusti (April 29, 2016 16:30:12)
- DaSpudLord
-
1000+ posts
a [point towards x( ) y( ) block
Change to semi-support.
EDIT: Not because of theonlygusti's post. That was a huge coincidence.
And there is an easier workaround- just use a second sprite.
EDIT: Not because of theonlygusti's post. That was a huge coincidence.
And there is an easier workaround- just use a second sprite.
Last edited by DaSpudLord (April 29, 2016 16:30:40)
- theonlygusti
-
1000+ posts
a [point towards x( ) y( ) block
I'd just like to add that I have severely needed this block on so many occasions, and, put simply, it just doesn't make sense for Scratch to not have this block; they have “if on edge, bounce” for crying out loud!
- joefarebrother
-
500+ posts
a [point towards x( ) y( ) block
And there is an easier workaround- just use a second sprite.
If you need to use this workaround, say, 20 times in a project, all with different inputs, would you need 20 different sprites? If not, you would need 2 extra variables and a broadcast instead, to pass over information to the second sprite, so it can move to the right place for you to point towards. How would you deal with race conditions, that is, where 2 sprites want to point in different directions at the same time? That would mess up the extra sprite leading to both pointing towards the same point, not what you want. So you would need an extra sprite, broadcast, and 2 variables, for EVERY sprite you want to use the workaround in. Say you wanted to use it in 5 sprites. You'll need 5 extra sprites, 5 extra broadcasts, and 10 extra variables. And you can't just copy over your custom block to every individual sprite (you ARE using a custom block, right? That's
define point towards x: (x) y: (y)
set [wanted x v] to (x)
set [wanted y v] to (y)
broadcast [point towards v] and wait
point towards [extra sprite v]
if you were wondering), because you'll need to change all the variables and broadcasts and sprite names.
Or you can just use a bit of trigonometry
Or it could be a primitive block
- theonlygusti
-
1000+ posts
a [point towards x( ) y( ) block
Hah hah, LOL, yeah. I never thought of the other workaround: create 172800 new Sprites and place them at every integer coordinate on the screen. Then just point towards the one you want. LOL so simple.
- Rex208
-
500+ posts
a [point towards x( ) y( ) block
Semi-support: Although it's workaroundable, the workarounds is kinda clunky.
- joefarebrother
-
500+ posts
a [point towards x( ) y( ) block
Pretty sure you can have non-integer coordinates too. Hah hah, LOL, yeah. I never thought of the other workaround: create 172800 new Sprites and place them at every integer coordinate on the screen. Then just point towards the one you want. LOL so simple.
- Discussion Forums
- » Suggestions
-
» a [point towards x( ) y( ) block