I want to make this animation in OpenGL, here I attached a simple gif how I want it to looks like: animation

Main problem is that I can not figure out how to move by corner - circle should smoothly move by corner, not to just jump to another side's begin position.

import Blender
from Blender import Draw,BGL
from Blender.BGL import *
from math import sin,cos
import time

squareLength, squareX, squareY, circleRadius = 200, 200, 200, 20
movingX, movingY = 0, 0

def event(evt, val):
    if evt == Draw.ESCKEY :

def gui():
    global movingX, movingY

    glClearColor(0.17, 0.24, 0.31, 1.0)
    glColor3f(0.74, 0.76, 0.78)

    glVertex2i(squareX, squareY)
    glVertex2i(squareX, squareY + squareLength)
    glVertex2i(squareX + squareLength, squareY + squareLength)
    glVertex2i(squareX + squareLength, squareY)

    glColor3f(0.58, 0.65, 0.65)


    if movingX == -circleRadius:
      if movingY < squareLength:
        movingY += 1
        movingX = 0
        movingY = squareLength + circleRadius
      if movingY == squareLength + circleRadius:
        if movingX < squareLength:
          movingX += 1
          movingX = squareLength + circleRadius
          movingY = squareLength
        if movingY < 0:
          if movingX > 0:
            movingX -= 1
            movingX = -circleRadius
            movingY = 0
          if movingY > 0:
            movingY -= 1
            movingX = squareLength
            movingY = -circleRadius

    glTranslatef(movingX, movingY, 0)

    for i in xrange(0, 360, 1):
        glVertex2f(squareX + sin(i) * circleRadius, squareY + cos(i) * circleRadius)



Draw.Register(gui, event, None)

Can you please say me, how can I optimize this code to make circle moving by corners?

Just start learning computer graphics and think up such exercises for training, so I will really appreciate for any help.

  • \$\begingroup\$ the center follows a rounded rectangle \$\endgroup\$ – ratchet freak Nov 18 '13 at 17:46
  • 1
    \$\begingroup\$ @ratchetfreak but in that way circle will a bit overlap a corner \$\endgroup\$ – Lu Vue Nov 18 '13 at 18:21
  • \$\begingroup\$ offset it by the radius of the circle \$\endgroup\$ – ratchet freak Nov 18 '13 at 23:19

Just did this using SDL but the theory is the same.

Make your circle orbit your square by the distance from the center of the square to the corner plus the radius of your circle.

Then limit the distance your circle can be from your square. Minimum would be -half square width -circle radius, maximum would be +half square width +circle radius.

Here's the code I used:

circle.x = lim(cos(circlePos*(3.1415/180))*(sqrt((float)sqrd(square.w/2)+(float)sqrd(square.h/2))+(circle.w/2)),(-square.w/2)-(circle.w/2),(square.w/2)+(circle.w/2))+square.x;
circle.y = lim(sin(circlePos*(3.1415/180))*(sqrt((float)sqrd(square.w/2)+(float)sqrd(square.h/2))+(circle.h/2)),(-square.h/2)-(circle.h/2),(square.h/2)+(circle.h/2))+square.y;

circle and squares are SDL_Rect structs that just store an x,y,w,h. lim() and sqrd() are functions that I made to simplify stuff.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.