Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Moving an object in a circular path pi314159geek Unladen Swallow Posts: 3 Threads: 2 Joined: Dec 2017 Reputation: 0 Likes received: 0 #1 Dec-25-2017, 02:50 AM I want one circle to orbit another along a pre-drawn circular path of radius 300. I know others have asked the same question, but all of those answers covered how to calculate coordinates that the object must travel through, not how to actually make the object move. This is especially tricky for me since there's no "move to a point" command in Python. ```def move_circle(acircle): #Specifies parameters to draw the new orbital new_orbital = Circle(atom_center, 300) new_orbital.setOutline("red") new_orbital.draw(win) #Calculates the center of the circle to be moved a = acircle.getCenter() b = a.getX() c = a.getY() for i in range(int(-b),int(b)): radius = 300 theta = math.radians(i) x = radius*math.cos(theta) y = radius*math.sin(theta) print(x,y) s = (b-x)**2 t = (c-y)**2 d = sqrt(s+t) ```(The last few lines of code are just me experimenting with the distance formula). I can calculate the coordinates that the object must travel through, using the mathematical equation of a circle, and the coordinates seem accurate. But how do I make "acircle" move through those calculated coordinates? Thanks! Windspar Minister of Silly Walks Posts: 415 Threads: 13 Joined: Oct 2016 Reputation: 32 Likes received: 70 #2 Dec-25-2017, 12:06 PM You can use a timer or you can use some type of ticks in main loop. example in pygame ```import pygame import math def move_coords(angle, radius, coords): theta = math.radians(angle) return coords[0] + radius * math.cos(theta), coords[1] + radius * math.sin(theta) def main(): pygame.display.set_caption("Oribit") screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() coords = 400, 200 angle = 0 rect = pygame.Rect(*coords,20,20) speed = 50 next_tick = 500 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ticks = pygame.time.get_ticks() if ticks > next_tick: next_tick += speed angle += 1 coords = move_coords(angle, 2, coords) rect.topleft = coords screen.fill((0,0,30)) screen.fill((0,150,0), rect) pygame.display.flip() clock.tick(30) pygame.quit() if __name__ == '__main__': main() ``` 99 percent of computer problems exists between chair and keyboard. hobby-programmer.com « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post moving image with blit rwahdan 2 290 Jul-10-2019, 06:24 PM Last Post: nilamo [PyGame] Keep moving pressing a key alexponceal 1 164 Jul-09-2019, 04:07 PM Last Post: Windspar [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 340 Jun-11-2019, 08:32 PM Last Post: metulburr moving a sprite pfaber11 3 416 May-15-2019, 12:52 PM Last Post: pfaber11 drawing, moving, and collision problems (pygame) SheeppOSU 26 1,857 Apr-22-2019, 03:09 AM Last Post: SheeppOSU Moving chess piece using mouse reepio13 7 1,782 Feb-24-2018, 11:26 PM Last Post: reepio13

Forum Jump:

Users browsing this thread: 1 Guest(s)