Discuss Scratch

TheBurger82
Scratcher
100+ posts

How do I make a sprite rotate around a certain point?

(I've never posted here before, but I think this counts because it uses trigonometry and stuff. :P)

How do I make a sprite rotate around a certain point?

Like if I want the rotation center to change without moving the sprite in the costume editor.

Last edited by TheBurger82 (Aug. 15, 2020 16:23:16)

Maximouse
Scratcher
1000+ posts

How do I make a sprite rotate around a certain point?

Hmm.

The idea is that you do rotate the sprite but you also move it around at the same time. Let's say cx and cy are coordinates of the point around which you want to rotate the sprite, relative to its center (by center I mean rotation center as set in the costume editor). We have to find the position where the center is moved to after rotation. The formula for rotation around (0, 0) by angle φ (counterclockwise; won't explain the formula here but it isn't difficult using basic trigonometry) is:
x' = x cos φ - y sin φ
y' = x sin φ + y cos φ

In this case cx and cy are arbitrary, so
x' = cx + ((x - cx) cos φ - (y - cy) sin φ)
y' = cy + ((x - cx) sin φ + (y - cy) cos φ)

In scratchblocks:
define turn left (angle) degrees around (cx) (cy)
// cx and cy are relative to sprite center position
turn left (angle) degrees
set x to ((cx) + ((((x position) - (cx)) * ([cos v] of (angle))) - (((y position) - (cy)) * ([sin v] of (angle)))))
set y to ((cy) + ((((x position) - (cx)) * ([sin v] of (angle))) + (((y position) - (cy)) * ([cos v] of (angle)))))

I haven't tested it yet but I think it should work.

Edit: it doesn't, but I have to go to sleep now so I'll fix it tomorrow.

Last edited by Maximouse (Aug. 15, 2020 18:22:37)

--Explosion--
Scratcher
1000+ posts

How do I make a sprite rotate around a certain point?

Well, Maximouse's method is not how I would've done it, but…
Point towards coordinate:
Direction = arctangent((y2- y1)/(x2-x1))+180 if y2-y1 is larger than 0
Basicaly the arctangent of the slope; which is rise over run. Now we need to figure out where to move the sprite:
x3=sine(Rotation)*(distance between x1,y1 and x2,y2)
y3=The same as above but with cosine.

Now we first move the sprite to x3,y3 and then point towards x1,y1.
ExplodedKid2000
Scratcher
11 posts

How do I make a sprite rotate around a certain point?

Hi
Don't know if this can help, but I found a way to make a sprite rotate around the cursor

when green flag clicked //This will reset the code
set [dist v] to (30)
set [angle v] to (0)

when green flag clicked //This is the code that makes it work. Use this in the sprite you want to rotate.
go to x: (0) y: (0)
forever
go to x: ((mouse x) + (([sin v] of (angle)) * (dist))) y: ((mouse y) + (([cos v] of (angle)) * (dist)))
point towards [mouse-pointer v]
turn cw (180) degrees
end

This will allow the sprite to rotate around your cursor at a certain distance defined by the “dist” variable
You can replace the “mouse x” and “mouse y” with the coordinates of the center of rotation. I hope this helps

Tropics-09
Scratcher
4 posts

How do I make a sprite rotate around a certain point?

Uhh, it just stays flipped, am i missing something?

Here's a screenshot.

testtubeguy9999
Scratcher
53 posts

How do I make a sprite rotate around a certain point?

go to this project:
https://scratch.mit.edu/projects/208441254/
and just replace the other sprite position blocks with the x and y you want!
dynamicsofscratch
Scratcher
1000+ posts

How do I make a sprite rotate around a certain point?

this topic issue has been solved by @Maximouse so gonna close report this to prevent further necroposting

Powered by DjangoBB