python - Create a square with sides at any angle -
i trying create rotating turret. turret rotates correctly problem when make turret shoot space bar bullet isn't same size or shape @ every angle. tried using angle turret facing trig calculations , find 2 corner points needed create bullet (which circle). nothing have tried work. here code:
barrel = [260,210,270,210,270,170,260,170] def rotatebarrel(): global angle anglechange = 2 mountcenterx = 265 mountcentery = 215 #rotate barrel cycle = 1 while cycle < len(barrel): x = barrel[cycle-1]-mountcenterx y = barrel[cycle]-mountcentery barrel[cycle-1] = (x*math.cos(anglechange*math.pi/180)-y*math.sin(anglechange*math.pi/180))+mountcenterx barrel[cycle] = (x*math.sin(anglechange*math.pi/180)+y*math.cos(anglechange*math.pi/180))+mountcentery cycle += 2 angle += anglechange if angle == 360: angle = 0 canvas.coords(barrel,barrel) self.after(1,rotatebarrel) def spinningshoot(event): global angle speed = 10 shotxpos = barrel[6]+10*(math.cos(angle*math.pi/180)) shotypos = barrel[7]-10*(math.sin(angle*math.pi/180)) cornerx = barrel[6]+10*(math.cos((90-angle)*math.pi/180)) cornery = barrel[7]-10*(math.sin((90-angle)*math.pi/180)) shot = canvas.create_oval(shotxpos,shotypos,cornerx,cornery,fill="white") xmotion = speed*math.cos(angle*math.pi/180) ymotion = speed*math.sin(angle*math.pi/180) shots.append(shot) shotspos.append(shotxpos) shotspos.append(shotypos) shotsmotion.append(xmotion) shotsmotion.append(ymotion)
it looks me shot not going centered on "barrel", calculation using (90-angle) going give angular width shot of
angle - ( 90 - angle )
which 2 * angle - 90
(ie shot wider depending on size of angle).
i have thought use (angle - 45 ) , (angle + 45 ) shot same angular width , centered on barrel.
you need increase "radius" second corner. im confused +10 , -10, not quite sure do.
probably better approach calculate "centre" of bullet , draw circle centered on it. im sure there function takes centre , radius. have
radius=10 centrex= radius * cos ( angle * math.pi /180 ) centrey= radius * sin ( angle * math. pi / 180 )
and pass 2 , radius function circle
one other little thing, suggest changing line
if angle == 360: angle = 0
to
if angle >= 360: angle = angle-360
as if angle initialized other number or changed angle step "miss" 360 , never wrap around.
Comments
Post a Comment