Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Need help enabling scroll slope function with fully rotating sprite.
- frieddan000games
- Scratcher
5 posts
Need help enabling scroll slope function with fully rotating sprite.
I have an empty scrolling terrain project. I want to add a Tank sprite in the center axis. I want to be able to control it using W & S for forward and reverse and A & D for rotating the sprite.
I want to program my scrolling sprites so that i can move forward and backwards while in any direction. In order to do this, i have to make a slop function or something.
when green flag clicked
forever
if W key pressed
change variable Y by 2
Change variable X by ?
when green flag clicked
forever
if S key pressed
change variable Y by -2
Change variable X by ?
when green flag clicked
forever
if A key pressed
Rotate 2 degrees <–
when green flag clicked
forever
if D key pressed
Rotate 2 degrees –>
I want to program my scrolling sprites so that i can move forward and backwards while in any direction. In order to do this, i have to make a slop function or something.
when green flag clicked
forever
if W key pressed
change variable Y by 2
Change variable X by ?
when green flag clicked
forever
if S key pressed
change variable Y by -2
Change variable X by ?
when green flag clicked
forever
if A key pressed
Rotate 2 degrees <–
when green flag clicked
forever
if D key pressed
Rotate 2 degrees –>
- deck26
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Is this https://scratch.mit.edu/projects/51479882/ what you mean?
- frieddan000games
- Scratcher
5 posts
Need help enabling scroll slope function with fully rotating sprite.
Yes but I want so that my sprite can rotate. The scroll x and scroll y will change in a slope based on the ange direction of my sprite.
- deck26
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
You mean the sprite stays still and the world rotates around it? I suspect you're more likely to get problems with lag doing that. Should be possible but you'll need trig to work out the position of each of the background sprites. The rotational direction of the sprites is less of an issue.
- frieddan000games
- Scratcher
5 posts
Need help enabling scroll slope function with fully rotating sprite.
no, the opposite The sprite rotates and and when it moves forward or reverse, the world changes X and Y at a certain slope
https://scratch.mit.edu/projects/65912898/
https://scratch.mit.edu/projects/65912898/
Last edited by frieddan000games (June 5, 2015 22:55:01)
- Birdlegs
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
change [scrollx v] by ([sin v] of (direction))-Mathemagical-
change [scrolly v] by ([cos v] of (direction))
Last edited by Birdlegs (June 6, 2015 03:16:16)
Get a sweet lick of my new game right here! It's a tasty one!
- deck26
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
If North remains in the same direction at all times then standard x,y scrolling is all you need. That would seem to be the case if the sprite is rotating. having the world and the sprite rotating seems confusing to me.
Or has @JoeyTheChicken hit the nail on the head and all you actually want is the relative speed across the ground to remain constant so you use trig to work out the proportional values for scrollx and scrolly.
Or has @JoeyTheChicken hit the nail on the head and all you actually want is the relative speed across the ground to remain constant so you use trig to work out the proportional values for scrollx and scrolly.
Last edited by deck26 (June 6, 2015 18:58:51)
- Birdlegs
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
If they wanna accelerate, deck26, they can add–
change [scrollx v] by (([sin v] of (direction)) * (acceleration))Right?
change [scrolly v] by (([cos v] of (direction)) * (acceleration))
Get a sweet lick of my new game right here! It's a tasty one!
- deck26
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Some sort of multiplier is probably required anyway or movement would be limited to 1 unit in whatever direction. If they wanna accelerate, deck26, they can add–change [scrollx v] by (([sin v] of (direction)) * (acceleration))Right?
change [scrolly v] by (([cos v] of (direction)) * (acceleration))
- Birdlegs
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Yeah, pretty muchSome sort of multiplier is probably required anyway or movement would be limited to 1 unit in whatever direction. If they wanna accelerate, deck26, they can add–change [scrollx v] by (([sin v] of (direction)) * (acceleration))Right?
change [scrolly v] by (([cos v] of (direction)) * (acceleration))
Get a sweet lick of my new game right here! It's a tasty one!
- frieddan000games
- Scratcher
5 posts
Need help enabling scroll slope function with fully rotating sprite.
joey, what do these blocks do? can i get a sample?
- Birdlegs
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Essentially, if you replaced changing a variable with changing actual X and Y, it would pretty much be a perfect replica of the “move () steps” block. Since sine and cosine can help us find how much X or Y would change in the normal world based on direction of movement, they're excellent for the function. joey, what do these blocks do? can i get a sample?
Get a sweet lick of my new game right here! It's a tasty one!
- deck26
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Not sure how familiar you are with trigonometry but a couple of examples might help.
sin (90 degrees) = 1 and cos (90) = 0
so if direction = 90 the script changes scrollx by 1 (multiplied by a value) and scrolly is unchanged
sin (45) = cos (45) = sqrt( 1/2) = 0.707
so if direction = 45 scrollx and scrolly are both changed by the same amount.
For angles outside the range 0-90 sin and cos will change sign according to which quadrant the angle is in so scrollx and scrolly may be changed by a positive or negative amount.
The interactive demo on this site might help https://www.mathsisfun.com/sine-cosine-tangent.html
NB In @JoeyTheChicken's script it isn't acceleration, it's speed. Repeating the same block gives the same value for the change in scrollx but if it was acceleration the change would increase.
sin (90 degrees) = 1 and cos (90) = 0
so if direction = 90 the script changes scrollx by 1 (multiplied by a value) and scrolly is unchanged
sin (45) = cos (45) = sqrt( 1/2) = 0.707
so if direction = 45 scrollx and scrolly are both changed by the same amount.
For angles outside the range 0-90 sin and cos will change sign according to which quadrant the angle is in so scrollx and scrolly may be changed by a positive or negative amount.
The interactive demo on this site might help https://www.mathsisfun.com/sine-cosine-tangent.html
NB In @JoeyTheChicken's script it isn't acceleration, it's speed. Repeating the same block gives the same value for the change in scrollx but if it was acceleration the change would increase.
Last edited by deck26 (June 9, 2015 11:58:05)
- Birdlegs
- Scratcher
1000+ posts
Need help enabling scroll slope function with fully rotating sprite.
Ah–nice catch. And Fried, think of ‘cos of scrolly,’ for example, finding the rate of change the speed the sprite would have along the ‘y’ axis were you using the “move () steps” block. It's using the angle, if I've brushed up on my trig (Geometry next year in school), to find the value of one side length of a right (Isosceles, I think) triangle divided by the other. Sorry. This is all based on an article I read a while back to teach myself some trig, but I don't remember the specifics NB In @JoeyTheChicken's script it isn't acceleration, it's speed. Repeating the same block gives the same value for the change in scrollx but if it was acceleration the change would increase.
Last edited by Birdlegs (June 9, 2015 15:57:05)
Get a sweet lick of my new game right here! It's a tasty one!
- Discussion Forums
- » Help with Scripts
- » Need help enabling scroll slope function with fully rotating sprite.