Probably not the best name... Anyways, I need a script that allows sprite to detect and turn away from a circle if they are touching it...
Offline
Can you explain further?
Offline
Use the following script and make the balls blue:
when gf clicked forever if <touching color [#0000FF]> turn cw (180) degrees repeat until <touching color [#0000FF]> move (10) steps end end end
Offline
Assuming you have a sprite for a sircle, or a set of coords, you can work out the distance (scratch has a block for sprite distance, but the formula for coords is harder), do this:
sqrt of (playerx - circlex)^2 + (playery - circley)^2
Effectively the Pythagoras theorem. After you have the distance you check if the distance is less than the radius, then simply do your turning script (change direction, bounce off etc)
Offline
Implementation of amercbu's method. circle's center is the circle sprite, and padding is the amount of extra space you want between the circle and the sprite. Circle radius is the radius of the circle.
when gf clicked forever if <(distance to [circle's center v]) < (circle's radius)> change x by (([cos v] of (direction)) * ((distance to [circle's center v]) + (padding))) change y by (([sin v] of (direction)) * ((distance to [circle's center v]) + (padding))) end end
Last edited by bobbybee (2012-07-08 15:35:29)
Offline
When the circle bounces, it needs to know how far it should bounce extra. You can set it to 0 if you want to make the circle a boundary, but if you make it 10 if you want it to bounce 10px automatically.
Offline
amcerbu wrote:
You can also point towards the center of the circle and turn 180 degrees for a roughly approximate bounce. The actual physics are a bit more complicated (although I can explain them, if you want).
Well you could do that with a bit more complexity:
1) store your direction in variable (old)
2) point towards the center of the circle
3) turn (direction - old) degrees
That should work for a pretty accurate bounce.
Offline
^^ Yeah, that's the bounce method that I used for collision with circles in one of my old (and failed) pinball projects:
http://scratch.mit.edu/projects/amcerbu/1004827
Last edited by amcerbu (2012-07-09 08:43:50)
Offline
There was one SDS project with circle bouncing. What was that script?
Offline