I need help with a calculation. It's simple, but the kind of thing my head just has trouble with.
I want stationary sprite A to rotate 10 deg. toward a designated point.
The "point to" command lets me rotate A all the way. But I just want 10 deg.
Seriously, this isn't trig, is it?! It's a matter of getting the positive and negative right, and removing or adding 180 deg if needed, or something like that.
The context is a turret-mounted gun that trains on the target but takes a bit of time to do it. So what I need is a slow rotation effect. (2D, not 3D!)
Offline
Here's one way to do (half) of it... just need an If in there to have it turn the other way if it needs to. I'll add that bit in if you need it.
point in direction (wherever)
set ($current_direction) to (direction)
point towards (other_sprite)
set ($new_direction) to (direction)
point in direction ($current)
set ($temp) to ($new_direction - $current_direction)
set ($remainder) to ($temp mod 10)
set ($temp) to [($temp - $remainder) / 10 ]
repeat ($temp) {
wait 0.5 secs
turn 10 degrees
}
wait 0.5 secs
turn ($remainder) degrees
Offline
http://scratch.mit.edu/projects/bhz/394380
download to see how it's done
Offline
Thanks MrWeston. I think what you posted is the part i don't have trouble with ... but I'm very grateful for a reminder of the mod command. It will always thrill me with its elegant usefulness, and its quality of never being thought of by me.
The problem tying me up is that if A is pointed 300 deg, and B is at 10 deg relative to A, then A will rotate -290. Of course I want +70. It's getting the difference ... it's like flying over the International Date Line.
I think my daughter gave me the solution. Set direction of A as relative bearing of 0. The difference between relative bearing and A's direction is your diffCalculated. You add the diffCalculated to the bearing of B. The difference between the relative bearing of A and the relative bearing of B is a number between 0 and 360. If <180, go one direction; if >180, go the other.
Give me a day to try things -- I'll post back with whatever worked.
Offline
smandoli wrote:
I need help with a calculation. It's simple, but the kind of thing my head just has trouble with.
I want stationary sprite A to rotate 10 deg. toward a designated point.
The "point to" command lets me rotate A all the way. But I just want 10 deg.
Seriously, this isn't trig, is it?! It's a matter of getting the positive and negative right, and removing or adding 180 deg if needed, or something like that.
The context is a turret-mounted gun that trains on the target but takes a bit of time to do it. So what I need is a slow rotation effect. (2D, not 3D!)
<when green flag clicked>
<repeat until><( <direction> <=> what ever you need )>
<turn cw( 10 )degrees>
<end>
thats what i do, if this is not what you mean i'm sorry :I
Last edited by mariobrosrule (2009-01-28 11:31:19)
Offline
if im correct, you want a turret to point to the mouse, but only 10 degrees to th left/right?
if(direction>80 and direction< 100){
point towards (mousePointer)
} else if (direction>100){
direction = 99
} else {
direction = 81
}
That should just about work. If you plan on rotating the turret mount, post it up and i'll fix it for you, it will take a quick script or two extra
Offline
Yeah I Think We Need A Blocks That Says (pointing Towards | Sprite | ) Or <pointing towards | Sprite | > (Same Thing But Different Shapes)
Offline
My idea is this:
Use two sprites.
On the Stage, put a short broadcast script:
[blocks]<when[ Stage ]clicked>
<broadcast[ click [/blocks]
The second sprite must be hidden, and at the same position of the sprite you want to rotate. Put this:
[blocks]<when I receive[ click
<point towards( mouse-pointer [/blocks]
The main sprite should have this:
[blocks]<when I receive[ click
<wait( 0.05 )secsc> (to let the other sprite rotate, otherwise it couldn't work well)
<if><( <direction> <<> (direction of HIDDEN SPRITE) )>
<turn cw( -10 )degrees>
<end>
<turn cw( 10 )degrees>
<end>[/blocks]
I haven't tried that, but I guess it should work
Offline
thanks everyone. I have a solution, but it's so complicated I am embarrassed to post it! I hope to 'refactor' it. You can see my turret at game Buzzard Island.
I have a problem, tho -- my cannons don't shoot right on the upload version. I figured FAQs would address it, but no. DON'T solve that here ... I hate cross-threading! -- I'll get back with more about incremental rotation when I can.
Offline
Thanks for the replies. This was a difficult problem ... the first step was discovering that a sprite uses positive degrees for right, negative for left! Which is obviously more useful than 0 to 360 for a full circuit, but it confused me.
My implemented solution is found in "Buzzard Island":
http://scratch.mit.edu/projects/smandoli/398737
technoguyx and mariobrosrule: This was like my son's solution (for his own game). One problem is that the final direction never resolves on the target. It twitches between almost-solutions, which was okay for my son's game but not for mine.
yambanshee and technoguyx and mariobrosrule: I didn't test, but I think none of these account for the target sneaking up from behind. My turret has a limited detection range, so the target can appear at 179 degrees. It means you must deal with any permutation of to-from, and correctly choose whether to rotate clockwise or counter-clockwise. That ALSO was an issue for my son's solution.
bhz: Your solution is terrific (just going by the demo play). Unfortunately your signature about '90% of teens' is troll-ish and this threw me enough that I actually overlooked the demo link you offered until just now. Apologies.
Last edited by smandoli (2009-02-02 12:34:17)
Offline