Discuss Scratch

AntonL1kesPotato
Scratcher
1000+ posts

Direction of sauce

I need help with a small visual in my game. There are sauces that i can put on the burger. The mechanic works, but the visual aspect doesn't. The sauce bottle needs to turn more towards the burger (which is the center) as it approaches more. Like this:


The script that takes care of the direction is in the Items sprite, in the “saucetopping” custom block. I put a comment to localize it easier.
The project: https://scratch.mit.edu/projects/791836542/










Making games on Scratch and learning other engines/frameworks. Also likes to make music using trackers.
































































You are using extensions, aren't you?
deck26
Scratcher
1000+ posts

Direction of sauce

Note sure what you think your script is doing - it only seems to allow the bottle to point in direction 90 or -90 degrees. You do realise that the costume as it appears in the costume editor is as it will be when pointing at 90 degrees?
AntonL1kesPotato
Scratcher
1000+ posts

Direction of sauce

deck26 wrote:

Note sure what you think your script is doing - it only seems to allow the bottle to point in direction 90 or -90 degrees. You do realise that the costume as it appears in the costume editor is as it will be when pointing at 90 degrees?
I know, i forgot to point out that it's just a placeholder. I tried putting in some math blocks, but it didn't work so i removed them. Feel free to change the script inside the repeat until “not mouse down” except for “go to mouse pointer” and the variables.










Making games on Scratch and learning other engines/frameworks. Also likes to make music using trackers.
































































You are using extensions, aren't you?
deck26
Scratcher
1000+ posts

Direction of sauce

It would be easier if you were just clicking on a sauce bottle so you could control the path but you're essentially just dragging the bottle. One option might be to make the rotation depend on the x position.

For example it is easy to see that if a bottle moved from x=90 to x=-90 you could just point in direction x - eg if x=30 you point in direction 30. That would rotate in the opposite direction to what you show but is easily fixed by pointing in direction 180 - x instead.

So for a range 90 to -90 that works OK and we can modify that for different values, just scaling the range of possible values to be 180.

So for a bottle moving from 50 to -70 we have a range of 120 and want to multiply by 1.5 but we want the central position to correspond to the value 90 in our range from 0 to 180. That just means in this case the average is 10 and it corresponds to the value 0. So the value x+10 gives a range from 60 to 60 and we can multiply that by 1.5 giving

point in direction 180 - ((x + 10) * 1.5)

or more generally

point in direction 180 - ((x + ave) * (180 / (max-x - min-x))) where ave is (max-x - min-x) / 2

You appear to be comfortable using lists so it shouldn't be hard to calculate and store the relevant values for each bottle.

Last edited by deck26 (Feb. 24, 2023 14:44:35)

ventoaurum
Scratcher
100+ posts

Direction of sauce

remove the if direction>90 and if direction<-90 or else stuff.
then replace it with
point in direction (([atan v] of (((mouse x) / (mouse y)) + ((180) * <[mouse y] < [0]>))) - (90)

Last edited by ventoaurum (Feb. 24, 2023 15:18:00)



(signature)
i make bad decisions professionally
AntonL1kesPotato
Scratcher
1000+ posts

Direction of sauce

deck26 wrote:

It would be easier if you were just clicking on a sauce bottle so you could control the path but you're essentially just dragging the bottle. One option might be to make the rotation depend on the x position.

For example it is easy to see that if a bottle moved from x=90 to x=-90 you could just point in direction x - eg if x=30 you point in direction 30. That would rotate in the opposite direction to what you show but is easily fixed by pointing in direction 180 - x instead.

So for a range 90 to -90 that works OK and we can modify that for different values, just scaling the range of possible values to be 180.

So for a bottle moving from 50 to -70 we have a range of 120 and want to multiply by 1.5 but we want the central position to correspond to the value 90 in our range from 0 to 180. That just means in this case the average is 10 and it corresponds to the value 0. So the value x+10 gives a range from 60 to 60 and we can multiply that by 1.5 giving

point in direction 180 - ((x + 10) * 1.5)

or more generally

point in direction 180 - ((x + ave) * (180 / (max-x - min-x))) where ave is (max-x - min-x) / 2

You appear to be comfortable using lists so it shouldn't be hard to calculate and store the relevant values for each bottle.
Thanks! This makes it clear!










Making games on Scratch and learning other engines/frameworks. Also likes to make music using trackers.
































































You are using extensions, aren't you?

Powered by DjangoBB