This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-08-25 16:44:33

ZombieHappines
Scratcher
Registered: 2012-01-19
Posts: 100+

i need help

i have a idea of a cube and when you press the left arrow key or right arrow key it rotates to move forward or backward and if it isnt flat against the ground it falls flat against the ground but i cant think of a script

Last edited by ZombieHappines (2012-08-25 16:44:54)


http://i47.tinypic.com/10wr3bq.jpg
http://i50.tinypic.com/2w7onpy.png

Offline

 

#2 2012-08-25 16:48:41

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: i need help

Wes64 made something like this here, check out his scripts.  smile


http://i50.tinypic.com/312u714.jpg

Offline

 

#3 2012-08-25 16:50:44

ZombieHappines
Scratcher
Registered: 2012-01-19
Posts: 100+

Re: i need help

thats a lot to complicated


http://i47.tinypic.com/10wr3bq.jpg
http://i50.tinypic.com/2w7onpy.png

Offline

 

#4 2012-08-25 16:54:10

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: i need help

ZombieHappines wrote:

thats a lot to complicated

The script you are thinking of IS complicated. Lots of trigonometry is involved in the basics.


http://i50.tinypic.com/312u714.jpg

Offline

 

#5 2012-08-25 17:03:43

ZombieHappines
Scratcher
Registered: 2012-01-19
Posts: 100+

Re: i need help

BirdByte wrote:

ZombieHappines wrote:

thats a lot to complicated

The script you are thinking of IS complicated. Lots of trigonometry is involved in the basics.

yes but there has to be one a little shorter


http://i47.tinypic.com/10wr3bq.jpg
http://i50.tinypic.com/2w7onpy.png

Offline

 

#6 2012-08-25 19:12:26

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: i need help

ZombieHappines wrote:

BirdByte wrote:

ZombieHappines wrote:

thats a lot to complicated

The script you are thinking of IS complicated. Lots of trigonometry is involved in the basics.

yes but there has to be one a little shorter

There is, though you wouldn't be able to controls the shape's radius, just the number of sides and their length.


http://i46.tinypic.com/35ismmc.png

Offline

 

#7 2012-08-25 22:28:24

ZombieHappines
Scratcher
Registered: 2012-01-19
Posts: 100+

Re: i need help

i want a regular square that does that stuff without the side changing and the side length
sorry i meant square

Last edited by ZombieHappines (2012-08-25 23:02:29)


http://i47.tinypic.com/10wr3bq.jpg
http://i50.tinypic.com/2w7onpy.png

Offline

 

#8 2012-08-25 22:56:18

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: i need help

ZombieHappines wrote:

i want a regular cube that does that stuff without the side changing and the side length

Oh, you're thinking of a [b]cube[/cube] not a square. My mistake. Anyway that would require a lot of stamping or trigonometry.


http://i46.tinypic.com/35ismmc.png

Offline

 

#9 2012-08-26 08:01:08

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: i need help

Hey, I made a simpler script that hopefully does what you need. It's still not easy though - what you're trying to do is kind of complicated.

Start with creating a 50px by 50px square  smile  Since it's a bit of a hassle, I made one and uploaded it on Scratch Resources here  smile

First of all, you want to be able to nicely control the *rotation* of the square with the arrow keys. For that we'll use something similar to the common velocity or speed method for scrollers. Instead of controlling the sprite's vertical/horizontal movement, we'll use it for rotation:

when gf clicked
set [turn speed v] to [0]
forever
  if <key [left arrow v] pressed?>
    change [turn speed v] by (-1)
  end
  if <key [right arrow v] pressed?>
    change [turn speed v] by (1)
  end
  set [turn speed v] to ((turn speed) * (0.9))
end
Now, you'll want to make a script that turns the sprite using this (turn speed) variable.
We also have to set the y position so that it looks like the square is rolling over its corner. For that, we'll need some math.

when gf clicked
forever
  set [angle v] to ((((direction) + (45)) mod (90)) - (45) )
end
This is the angle of the square, compared to it being flat on the ground (a number ranging from -45 to 0 to 45). It's also the amount of degrees we still have to turn before it's lying down on it's side.

Now for the turning/positioning:

when gf clicked
forever
  turn right (turn speed) degrees
  set y to ((-100) + ( ( (([sqrt v] of (2)) - (1)) * (25)) * (([abs v] of (angle)) / (45)) ) )
end
The turning is kind of obvious.

The second block is a little harder. If you'd be interested in understanding it, (sqrt(2) - 1) * 25 is the difference between 1) the distance from the center to the side and 2) the distance from the center to a corner. If the square is standing on a corner, you want to add that to the y position (so it's a little higher). If it's on its side, you add nothing. If it's somewhere in between, you add something that is somewhere in between 0 and the full difference between those 2 distances.

-100 is a fixed position, so you can change this to move your sprite up/down.

Note that this is actually just approximate and not physically correct - but it sure looks like it  big_smile

You can experiment with your project at this point, you'll notice that the square rotates nicely, and moves as if it's rolling over its corners.

What we haven't done yet, is making it "fall" onto its side realistically. That's also kinda hard. That's what this script does:

when gf clicked
forever
if <<not <key [left arrow v] pressed?>> and <not <key [right arrow v] pressed?>>>
  if <([abs v] of (angle)) > [3]>  //"accelerate" to fall on its side
    if <(angle) > (0)>
      change [turn speed v] by ( ((45) - ([abs v] of (angle))) * (-0.03))
    else
      change [turn speed v] by ( ((45) - ([abs v] of (angle))) * (0.03))
    end
  else //almost on its side, realign it
    set [turn speed v] to [0] //stop turning
    point in direction ((round((direction)/(90)))*(90))
  end
end
end
I'm still cleaning up my project I made to go with this tutorial before I share it, but you should really try to make your project from this tutorial - you'll hopefully understand how everything works  smile  If you can't get it to work, I'll a working version of this project later.

Enjoy!

Last edited by JSO (2012-08-26 15:07:38)


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#10 2012-08-26 14:16:32

ZombieHappines
Scratcher
Registered: 2012-01-19
Posts: 100+

Re: i need help

thanks so much


http://i47.tinypic.com/10wr3bq.jpg
http://i50.tinypic.com/2w7onpy.png

Offline

 

#11 2012-08-26 14:25:33

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: i need help

ZombieHappines wrote:

thanks so much

Let me know how it works out  smile

PS: I fixed the first script in the tutorial. I forgot to add

 set [turn speed v] to ((turn speed) * (0.9))

Last edited by JSO (2012-08-26 15:09:17)


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

Board footer