I'm trying to figure out a way to move an object in a circular path. I read about Bresenham's circle algorithm, but all the codes available online draw an entire circle.
For my game, I want it so that when the ball enters a region, it'll begin to move in a circular path and when it leaves the region, it'll go back into moving in a straight path from its tangential velocity.
As of right now, I'm trying to figure out the algorithm to include in my game loop. I have the object updating its position every iteration. I use Bresenham's algorithm to decide whether to go right or go diagonal. I want it to draw the upper left portion of a circle
This is what I get:
Here is my code at the moment:
# Drawing Curves with Midpt Circle Theorem
import pygame, sys, random, math
from pygame.locals import *
class Game(object):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((400,400))
self.screen.fill((255, 255, 255))
self.jom = pygame.Rect(100,100,2,2)
def run(self):
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pygame.draw.rect(self.screen, (0,0,0), self.jom)
## Center of Curvature
centercurv = (104,103)
## Update Position
## eqn of circle (x-104)**2 + (y-103)**2 - r**2 = 0, r == 5
#circleeqn = (self.jom.x-centercurv[0])**2 + (self.jom.y-centercurv[1])**2
#goright = (self.jom.x+1
eR = -2*self.jom.x+2*centercurv[0]+1
eD1 = -2*self.jom.x - 2*self.jom.y + 2*centercurv[0] + 2*centercurv[1] + 2
print(eR,eD1)
if abs(eR)-abs(eD1) > 0:
#self.jom.x += 2
self.jom.y -= 1
print('1')
elif abs(eR)-abs(eD1) < 0:
#self.jom.x+=2
print('2')
else:
if eR < 0 and eD1 < 0:
#self.jom.x+=2
print('3')
elif eR > 0 and eD1 > 0:
self.jom.y -= 1
print('4')
self.jom.x += 1
pygame.display.flip()
print('Quitting')
pygame.quit()
sys.exit()
if __name__ == '__main__':
game = Game()
game.run()