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!
Offline
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
but I hope it works.
Offline
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) endA "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?
Offline
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.
if <touching [surface v] ?> turn cw (((2) * ((normal*) - (direction))) + (180)) degrees endNormals on a Circle (Edited: Previously I showed a needlessly complicated method)
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.
Last edited by Jamohyperturbopro (2012-04-28 02:31:32)
Offline
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) endA "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 endadd this to the script above.
Last edited by chanmanpartyman (2012-04-27 20:55:10)
Offline
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
Offline
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
![]()
Thanks, but i've already use your script in a velocity platformer!
when gf clicked say [thanks v] [a v] [lot v]
Last edited by berberberber (2012-05-02 10:50:35)
Offline
Yep I knew I wouldent be much help
<I didn't help much :(> <But I did my best>
Offline
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 endNormals 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
Offline
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) endA "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
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)
Offline
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
Offline
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.
Offline