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

#1 2012-04-21 00:48:51

jsboygenius
New Scratcher
Registered: 2012-04-21
Posts: 4

How do you make a Sprite move away in the direction of another?

I am trying to make a cannon for a project, but I need help making it so that the cannonball goes straight in the direction of the cannon. They are two different Sprites. If you know of a way that doesn't involve making 360 different "if" blocks for each possible direction, please post!

Offline

 

#2 2012-04-21 01:23:39

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: How do you make a Sprite move away in the direction of another?

Here are some example scripts that make it look like the cannonball is affected by gravity.  Try this: 

when gf clicked // Put this script in the cannon sprite.
repeat until (key [space v] pressed?)
  if (key [right v] pressed?)
    turn cw [5] degrees
  end
  if (key [left v] pressed?)
    turn ccw [5] degrees
  end
end
broadcast [shoot v]

when I receive [shoot v] // Put this script in the cannonball sprite.
go to [CannonSprite v]
set [xVelocity v] to ([10] * ([cos v] of ([90] - ([direction v] of [CannonSprite v]))))
set [yVelocity v] to ([10] * ([sin v] of ([90] - ([direction v] of [CannonSprite v]))))
repeat until <touching [edge v]?>
  change x by (xVelocity)
  change y by (yVelocity)
  change [yVelocity v] by [-0.5]
end

Offline

 

#3 2012-04-21 11:16:33

iTweak0r
Scratcher
Registered: 2011-07-30
Posts: 100+

Re: How do you make a Sprite move away in the direction of another?

Amcerbu,

I don't get how you use trig in Scratch.
Cause I took some lessons in trig and know the formulas....

But how can I use that in Scratch?


Make it in Scratch! because it's cooler when it's made in scratch
http://i.imgur.com/D4iqPHR.png

Offline

 

#4 2012-04-21 13:11:21

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: How do you make a Sprite move away in the direction of another?

@iTweakOr

Trigonometry is a mathematical concept that describes the relationship between angles and lengths.  For example, trig allows you to calculate the angles in a triangle when you know the lengths of its sides. 

The post I shared above converts the direction the cannon points into a right triangle with leg lengths xVelocity and yVelocity (legs are the two sides in a right triangle that meet at a right angle).  Imagine a right triangle with a center at the origin (0,0), and one leg going right, one going up.  The cosine function takes in the angle of the triangle and returns the ratio of the horizontal side over the hypotenuse (diagonal).  Likewise, the sine function takes in the same angle and returns the ratio of the vertical side over the hypotenuse.  In these scripts, the trig functions are used to determine the values of xVelocity and yVelocity.

Every frame (repeat until touching edge), the cannonball changes its position by xVelocity and yVelocity, giving the appearance that it is moving.  Additionally, every frame, yVelocity changes by -0.5, giving the effect that gravity is acting on the ball.  In each frame, the ball will change its x position by a constant value, and change its y position by a value that is decreasing, giving the image of falling.

Offline

 

#5 2012-04-21 13:46:44

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How do you make a Sprite move away in the direction of another?

iTweak0r wrote:

Amcerbu,

I don't get how you use trig in Scratch.
Cause I took some lessons in trig and know the formulas....

But how can I use that in Scratch?

If you have a question, you really should start your own post rather than asking it in someone else's.  It just keeps things organized in the forums.

Also, one of the tricky things in Scratch is that sine and cosine are usually flipped (since "0 degrees" is up/north, rather than right/east).


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#6 2012-04-21 14:28:54

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do you make a Sprite move away in the direction of another?

Try

point towards [cannon v]
repeat until <touching [edge v]?>
  move (-5) steps
end

Offline

 

#7 2012-04-21 19:04:56

jsboygenius
New Scratcher
Registered: 2012-04-21
Posts: 4

Re: How do you make a Sprite move away in the direction of another?

@amcerbu
So, how would I do this without having a gravity effect? Oh, and the cannon follows your "mouse-pointer," not arrow keys. (Wasn't sure if that affected anything.) And one last thing, would this work if I made three cannonball sprites, giving you three shots until one of the cannonballs touches the edge? Thanks!

Offline

 

#8 2012-04-22 16:00:14

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: How do you make a Sprite move away in the direction of another?

@jsboygenius:
If you want the cannon to follow the mouse, and the cannonball to have no gravity, change the scripts to be this:

when gf clicked // Put this script in the cannon sprite.
repeat until (mouse down?)
  point towards [mouse pointer v]
end
broadcast [shoot v]

when I receive [shoot v] // Put this script in the cannonball sprite.
go to [CannonSprite v]
set [xVelocity v] to ([10] * ([cos v] of ([90] - ([direction v] of [CannonSprite v]))))
set [yVelocity v] to ([10] * ([sin v] of ([90] - ([direction v] of [CannonSprite v]))))
repeat until <touching [edge v]?>
  change x by (xVelocity)
  change y by (yVelocity)
end

Offline

 

#9 2012-04-22 22:25:28

jsboygenius
New Scratcher
Registered: 2012-04-21
Posts: 4

Re: How do you make a Sprite move away in the direction of another?

Thank you so much!

Offline

 

Board footer