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

#1 2012-04-26 20:12:47

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

If touching (color or sprite), Bounce

I looked this up on Scratch Suggestions, It was Declined. Lightnin said it was already possible. Could anyone the me how to do it? Thanks!


http://i47.tinypic.com/2iaa73k.png

Offline

 

#2 2012-04-27 03:21:28

Jem12
Scratcher
Registered: 2012-04-20
Posts: 100+

Re: If touching (color or sprite), Bounce

I'm not quite sure about this but you can have

If touching colour/sprite
move 10 space <direction>

Sorry if this doesn't work  sad  but I hope it works.

cool


"In the begging the universe was created, this made a lot of people angry and was widly regarded as a bad move" ~  Douglas Adams ~ The resturant at the end of the universe

Offline

 

#3 2012-04-27 08:58:42

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

Re: If touching (color or sprite), Bounce

I would ignore direction and use velocity (if that fits your scenario).

set [x v] to (0)
set [y v] to (0)
forever
if<key [left arrow v] pressed?>
change [x v] by (-.2)
end
if<key [right arrow v] pressed?>
change [x v] by (.2)
end
if<key [up arrow v] pressed?>
change [y v] by (.2)
end
if<key [down arrow v] pressed?>
change [y v] by (-.2)
end
change x by (x)
if<touching [sprite or color]?>
set [x v] to ((x)*(-1))
change x by (x)
end
change y by (y)
if<touching [sprite or color]?>
set [y v] to ((y)*(-1))
change y by (y)
end
A "real" bouncing script would be quite difficult, as you would have no idea of the angle of the sprite at the place you're hitting it.  And, for instance, if you said "go to [sprite1]; bounce off of [sprite1]" which direction would face and move?


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

Offline

 

#4 2012-04-27 09:10:33

zammer990
Scratcher
Registered: 2012-01-22
Posts: 500+

Re: If touching (color or sprite), Bounce

when gf clicked
forever
if touching colour [X]
point in direction ((direction) * [-1])


http://i45.tinypic.com/2ynq7nn.jpg Play now!

Offline

 

#5 2012-04-27 10:14:44

Jamohyperturbopro
Scratcher
Registered: 2009-06-25
Posts: 8

Re: If touching (color or sprite), Bounce

You will need to calculate the amount of rotation based on the 'Normal' of the surface you are bouncing off (this is the line at 90˚ to the surface) because "bouncing" generally follows the laws of reflection.

for a flat surface, the rotation calculation is simple:

turn cw (((2) * ((normal*) - (direction))) + (180)) degrees
* direction parallel to the normal.

on the far right edge of the screen, the normal* is 90˚ (right) or 270˚ (left).
on the bottom of the screen, it is 0˚ or 180˚.
if there was a surface leaning against the right wall at 30˚, the normal* could be -60˚ as it can be any direction parallel to the normal.

so your code is:
if <touching [surface v] ?>
turn cw (((2) * ((normal*) - (direction))) + (180)) degrees
end
Normals on a Circle (Edited: Previously I showed a needlessly complicated method)
If you're bouncing off a circle, your normal is the direction from one of the objects to the other. as there is currently no block for this, the best way is to use the difference between the x and y positions of the sprites and some trig.

dif x and dif y are the differences between the sprites' co-ordinates. In this case, it doesn't matter which sprite's you subtract from the other's, nor that the result is negative or reversed in some quadrants, as the outputs will all be parallel.

set [dif x v] to (([x position v] of [circle v]) - (x position))
set [dif y v] to (([y position v] of [circle v]) - (y position))
set [normal* v] to ([atan v] of ((dif x) / (dif y)))
put this just before the rotation is calculated as shown above.

You could use the circular bouncing to approximate bouncing off a fairly round sprite.

A demonstration: http://scratch.mit.edu/projects/Jamohyperturbopro/2499394

However, if you want to apply gravity, it is, of course, better to use x and y velocity.

Last edited by Jamohyperturbopro (2012-04-28 02:31:32)

Offline

 

#6 2012-04-27 20:52:51

chanmanpartyman
Scratcher
Registered: 2011-05-30
Posts: 500+

Re: If touching (color or sprite), Bounce

MoreGamesNow wrote:

I would ignore direction and use velocity (if that fits your scenario).

set [x v] to (0)
set [y v] to (0)
forever
if<key [left arrow v] pressed?>
change [x v] by (-.2)
end
if<key [right arrow v] pressed?>
change [x v] by (.2)
end
if<key [up arrow v] pressed?>
change [y v] by (.2)
end
if<key [down arrow v] pressed?>
change [y v] by (-.2)
end
change x by (x)
if<touching [#000000]?>
set [x v] to ((x)*(-1))
change x by (x)
end
change y by (y)
if<touching [#000000]?>
set [y v] to ((y)*(-1))
change y by (y)
end
A "real" bouncing script would be quite difficult, as you would have no idea of the angle of the sprite at the place you're hitting it.  And, for instance, if you said "go to [sprite1]; bounce off of [sprite1]" which direction would face and move?

And if you want it to point to where it is headed

point in direction ([atan v] of ((x) / (y)))
if <(y) < (0)>
 turn cw (180) degrees
end
add this to the script above.

Last edited by chanmanpartyman (2012-04-27 20:55:10)

Offline

 

#7 2012-05-01 20:07:46

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

bump


http://i47.tinypic.com/2iaa73k.png

Offline

 

#8 2012-05-01 23:03:47

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

Re: If touching (color or sprite), Bounce

berberberber wrote:

bump

If my script didn't do what you wanted, try telling us more precisely what you want.  It's far more effective than us trying to guess  smile


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

Offline

 

#9 2012-05-02 10:49:06

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

MoreGamesNow wrote:

berberberber wrote:

bump

If my script didn't do what you wanted, try telling us more precisely what you want.  It's far more effective than us trying to guess  smile

Thanks, but i've already use your script in a velocity platformer!

 
when gf clicked
say [thanks v] [a v] [lot v]
smile

Last edited by berberberber (2012-05-02 10:50:35)


http://i47.tinypic.com/2iaa73k.png

Offline

 

#10 2012-05-03 05:37:07

Jem12
Scratcher
Registered: 2012-04-20
Posts: 100+

Re: If touching (color or sprite), Bounce

Yep I knew I wouldent be much help  sad 

<I didn't help much :(>
<But I did my best>
tongue

_____________________________________________________________________________
Check out this game! Mario fans will like it!!


"In the begging the universe was created, this made a lot of people angry and was widly regarded as a bad move" ~  Douglas Adams ~ The resturant at the end of the universe

Offline

 

#11 2012-05-06 17:16:19

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

Jamohyperturbopro wrote:

You will need to calculate the amount of rotation based on the 'Normal' of the surface you are bouncing off (this is the line at 90˚ to the surface) because "bouncing" generally follows the laws of reflection.

for a flat surface, the rotation calculation is simple:

turn cw (((2) * ((normal*) - (direction))) + (180)) degrees
* direction parallel to the normal.

on the far right edge of the screen, the normal* is 90˚ (right) or 270˚ (left).
on the bottom of the screen, it is 0˚ or 180˚.
if there was a surface leaning against the right wall at 30˚, the normal* could be -60˚ as it can be any direction parallel to the normal.

so your code is:
if <touching [surface v] ?>
turn cw (((2) * ((normal*) - (direction))) + (180)) degrees
end
Normals on a Circle (Edited: Previously I showed a needlessly complicated method)
If you're bouncing off a circle, your normal is the direction from one of the objects to the other. as there is currently no block for this, the best way is to use the difference between the x and y positions of the sprites and some trig.

dif x and dif y are the differences between the sprites' co-ordinates. In this case, it doesn't matter which sprite's you subtract from the other's, nor that the result is negative or reversed in some quadrants, as the outputs will all be parallel.

set [dif x v] to (([x position v] of [circle v]) - (x position))
set [dif y v] to (([y position v] of [circle v]) - (y position))
set [normal* v] to ([atan v] of ((dif x) / (dif y)))
put this just before the rotation is calculated as shown above.

You could use the circular bouncing to approximate bouncing off a fairly round sprite.

A demonstration: http://scratch.mit.edu/projects/Jamohyperturbopro/2499394

However, if you want to apply gravity, it is, of course, better to use x and y velocity.

One more thing, would it be possible to use this with a gravity script?
bump


http://i47.tinypic.com/2iaa73k.png

Offline

 

#12 2012-05-08 17:24:11

eiim7
Scratcher
Registered: 2012-05-08
Posts: 7

Re: If touching (color or sprite), Bounce

MoreGamesNow wrote:

I would ignore direction and use velocity (if that fits your scenario).

set [x v] to (0)
set [y v] to (0)
forever
if<key [left arrow v] pressed?>
change [x v] by (-.2)
end
if<key [right arrow v] pressed?>
change [x v] by (.2)
end
if<key [up arrow v] pressed?>
change [y v] by (.2)
end
if<key [down arrow v] pressed?>
change [y v] by (-.2)
end
change x by (x)
if<touching [sprite or color]?>
set [x v] to ((x)*(-1))
change x by (x)
end
change y by (y)
if<touching [sprite or color]?>
set [y v] to ((y)*(-1))
change y by (y)
end
A "real" bouncing script would be quite difficult, as you would have no idea of the angle of the sprite at the place you're hitting it.  And, for instance, if you said "go to [sprite1]; bounce off of [sprite1]" which direction would face and move?

I made x be

 <x position>
and y
 <y position>
.

Offline

 

#13 2012-05-08 18:00:04

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

Re: If touching (color or sprite), Bounce

eiim7 wrote:

I made x be

 <x position>
and y
 <y position>
.

I'm not sure what you mean.

@berberberber

If you mean like a platformer, then sure!  Is this what you mean?

Last edited by MoreGamesNow (2012-05-08 18:01:26)


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

Offline

 

#14 2012-05-27 22:15:31

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

bump


http://i47.tinypic.com/2iaa73k.png

Offline

 

#15 2012-05-27 23:28:06

wasabi56
Scratcher
Registered: 2012-02-10
Posts: 500+

Re: If touching (color or sprite), Bounce

berberberber wrote:

I looked this up on Scratch Suggestions, It was Declined. Lightnin said it was already possible. Could anyone the me how to do it? Thanks!

Okay, out of all the ones here, this is probably the easiest...

when gf clicked
forever
if <touching [sprite x v] ?>
point in direction <(direction)+[180]>
end

when gf clicked 
forever
if <touching Color [] ?>
point in direction <(direction)+[180]>
end


http://i.picasion.com/pic58/c23d4d2fc75f459dcf3d9ebf3e8ba395.gif
http://www.weebly.com/uploads/1/0/1/4/10146167/2294523_orig.png

Offline

 

#16 2012-05-28 00:25:38

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

wasabi56 wrote:

berberberber wrote:

I looked this up on Scratch Suggestions, It was Declined. Lightnin said it was already possible. Could anyone the me how to do it? Thanks!

Okay, out of all the ones here, this is probably the easiest...

when gf clicked
forever
if <touching [sprite x v] ?>
point in direction <(direction)+[180]>
end

when gf clicked 
forever
if <touching Color [] ?>
point in direction <(direction)+[180]>
end

I meant off a line, coming at any angle.


http://i47.tinypic.com/2iaa73k.png

Offline

 

#17 2012-05-28 00:49:57

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

Re: If touching (color or sprite), Bounce

You have to know the slope/direction of the surface you're hitting.  Once you know that, it's not so hard to calculate the bounce.

Offline

 

#18 2012-05-28 00:58:44

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: If touching (color or sprite), Bounce

I need help with post 10.


http://i47.tinypic.com/2iaa73k.png

Offline

 

Board footer