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

#1 2008-07-27 18:57:42

17church
Scratcher
Registered: 2008-07-27
Posts: 3

Mathematical Functions

Hi there,

Does anyone have any simple examples they would like to share on how to use the mathematical functions (sin, cos etc). I have used the help options on all of them but would learn them faster if I could see them in action - the simpler the better.

Would really appreciate some help!

Offline

 

#2 2008-07-27 19:02:42

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Mathematical Functions

Well, for sin and cos, my Tank Game uses it to go to the correct position. The abs can be used for determining the distance to something (abs of your X to abs of their X). The possibilities are endless with them


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#3 2008-07-27 19:06:50

17church
Scratcher
Registered: 2008-07-27
Posts: 3

Re: Mathematical Functions

Any chance of a quick example?

Haha, maths was not my best subject, but I do learn quickly once shown how it could be used!

Offline

 

#4 2008-07-27 19:08:35

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Mathematical Functions

17church wrote:

Any chance of a quick example?

Haha, maths was not my best subject, but I do learn quickly once shown how it could be used!

Hmmm, alright, lemme see… Well, here is the tank game:


<change x by( <sin( Xposition of Body
<change y by( <cos( Yposition of Body


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#5 2008-07-27 19:22:08

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Mathematical Functions

17church wrote:

Any chance of a quick example?

Haha, maths was not my best subject, but I do learn quickly once shown how it could be used!

Here's an example using the Scratch coordinate system (zero is up):  Let's say you would like to compute the position of a point (let's call it Point B) that lies in direction 25 and at a distance of 10 steps from your current point (let's call it point A).  The X and Y coordinates can be calculated using:

                     Point_B_X = Point_A_X + Sin(25) * 10
                     Point_B_Y = Point_A_Y + Cos(25) * 10

Where Point_A_X is the X position of Point A and so on.  Hope that helps!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#6 2008-07-27 21:14:16

17church
Scratcher
Registered: 2008-07-27
Posts: 3

Re: Mathematical Functions

Cool, will have a play.

One  more thing: I am working on a team basketball game at the moment, quite hard, but I'm making good progress. I have the user controlling one team, using space bar to pass the ball to another player until it gets to the hoop.

Two questions:
1) Can I get a player (sprite) to pass the ball to the nearest player in his team? I just cant seem to get this one, so at the moment I have them passing in sequence, but that gets boring. I have made the ball follow the mouse pointer which solves that problem but would prefer it to pass to nearest sprite on click.

2) The other team is set to pass in sequence automatically, which I am happy with. I would like there to somehow be intercepts, so I I pass the ball to my player, but an opposition player is in the way then they grab the ball. But it seems like once the command of passing to my own player is in motion it doesnt stop, even if I have told it to stop (if touching...)

Confusing?

Offline

 

#7 2008-07-27 21:20:50

Bluestribute
Scratcher
Registered: 2008-01-24
Posts: 1000+

Re: Mathematical Functions

1) you can either A: set a variable, and assign each member (if they don't have the ball. Again, using variables to see who does) a number, and if the variable equals their number, they get the ball. Or, you can check the distance to each teamate with the   <distance to[
blocks to see who is closets. The same for the computer players intercepting. When you pass, set a variable to 1, and if the distance to the ball is less than a certain number and passing equals 1, the computer player wil intercept, but remember to reset the variable when the ball is caught


http://img247.imageshack.us/img247/1204/bluestributett4.jpg
That's my PSN ID. I know tons of COD4 glitches. Add me as your friend. Oh, and get a headset

Offline

 

#8 2008-07-28 05:42:11

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Mathematical Functions

17church wrote:

Cool, will have a play.

One  more thing: I am working on a team basketball game at the moment, quite hard, but I'm making good progress. I have the user controlling one team, using space bar to pass the ball to another player until it gets to the hoop.

Two questions:
1) Can I get a player (sprite) to pass the ball to the nearest player in his team? I just cant seem to get this one, so at the moment I have them passing in sequence, but that gets boring. I have made the ball follow the mouse pointer which solves that problem but would prefer it to pass to nearest sprite on click.

2) The other team is set to pass in sequence automatically, which I am happy with. I would like there to somehow be intercepts, so I I pass the ball to my player, but an opposition player is in the way then they grab the ball. But it seems like once the command of passing to my own player is in motion it doesnt stop, even if I have told it to stop (if touching...)

Confusing?

Probably should have started a new thread for this since it pretty much is a new topic...whatever.

Anyway, Blue is correct that the "Distance To" sensor block will be useful in finding the closest player.  You will also find this much easier to do in the next release since you can build a list of distances to players and sort the list to find the closest player.  In the current version, I might instead use a sensing sprite with the Ghost effect set to 100 so it is invisible.  Have the sensing sprite locate on the player passing the ball and have it increase size repeatedly until it contacts another player on your team - that will be the closest player.  It might be a slow technique but it will be fairly easy to code.

Another thing is if you are using Glide blocks to pass the ball, you are not going to be able to intercept them...they go until they get to their final destination.   Instead, you will probably need to use Move blocks in a Repeat Until loop with the exit condition of the loop set up for when the ball touches an enemy player.  Hope that helps.  Sounds like a fun game!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer