Computer
Graphics
Lab.3
Animate a Cube Moving Around Another Cube
• The first cube stays at (0, 0, 0).
• The second cube moves in a square (10 units per side) over 200 frames.
o The positions are chosen to create a
symmetrical and closed shape
o The Z-coordinate is always 0, so the
cube stays in the XY plane.
The cube starts at (10, 0, 0)
The cube moves in this order:
▪ Left: From (10, 0, 0) to (0, -10, 0)
▪ Up: From (0, -10, 0) to (-10, 0, 0)
▪ Right: From (-10, 0, 0) to (0, 10, 0)
▪ Back to Start: F(0, 10, 0) to (10, 0, 0)
Start
Code:
import bpy
[Link].primitive_cube_add()
[Link].primitive_cube_add()
c = [Link].active_object
[Link] = (10, 0, 0) # Set initial position
c.keyframe_insert("location", frame=1)
[Link] = (0, -10, 0) # Move the cube Left
c.keyframe_insert("location", frame=50)
[Link] = (-10, 0, 0) # Move the cube Up
c.keyframe_insert("location", frame=100)
[Link] = (0, 10, 0) # Move the cube Right
c.keyframe_insert("location", frame=150)
[Link] = (10, 0, 0) # Move back to the original
c.keyframe_insert("location", frame=200)
Add a rotation animation along with the movement
In this version, the cube rotates 90° at each corner to enhance the animation.
• c.rotation_euler.z = [Link](X) is used to set rotation at different frames.
• c.keyframe_insert("rotation_euler", frame=X) is added to store rotation at each
frame.
Code:
import bpy
import math
# Create two cubes: One stationary, one moving
[Link].primitive_cube_add()
[Link].primitive_cube_add()
c = [Link].active_object # Moving cube
# Initial position and rotation (Frame 1)
[Link] = (10, 0, 0)
c.rotation_euler.z = [Link](0)
c.keyframe_insert("location", frame=1)
c.keyframe_insert("rotation_euler", frame=1)
# Move Left (Frame 50)
[Link] = (0, -10, 0)
c.rotation_euler.z = [Link](90)
c.keyframe_insert("location", frame=50)
c.keyframe_insert("rotation_euler", frame=50)
# Move Up (Frame 100)
[Link] = (-10, 0, 0)
c.rotation_euler.z = [Link](180)
c.keyframe_insert("location", frame=100)
c.keyframe_insert("rotation_euler", frame=100)
# Move Right (Frame 150)
[Link] = (0, 10, 0)
c.rotation_euler.z = [Link](270)
c.keyframe_insert("location", frame=150)
c.keyframe_insert("rotation_euler", frame=150)
# Return to start (Frame 200)
[Link] = (10, 0, 0)
c.rotation_euler.z = [Link](360)
c.keyframe_insert("location", frame=200)
c.keyframe_insert("rotation_euler", frame=200)
Animating a Sphere in a Circular Path
In a unit circle, any point on the circle can be
represented using sine and cosine.
𝒙 = 𝒓 ∙ 𝐜𝐨𝐬(𝜽)
𝒚 = 𝒓 ∙ 𝐬𝐢𝐧(𝜽)
𝒑(𝒙, 𝒚) = (𝒓 ∙ 𝐜𝐨𝐬(𝜽) , 𝒓 ∙ 𝐬𝐢𝐧(𝜽))
where:
• 𝒓 is the radius of the circle.
• 𝜽 is the current angle in radians.
• (𝒙, 𝒚) are the coordinates of the object on the circle.
We calculate the x, y coordinates values based on the radius (which is a constant) and 𝜽
So, as angle(𝜽) increases, the x, y coordinates move smoothly around the circle.
We need to update the sphere’s position multiple times to create movement.
• Instead of manually setting keyframes at every step, we use a loop to automate the
process.
• The loop iterates from 0 to 2π (one full circle), calculating new positions at each step.
Steps to Implement the Solution
Initializing Animation Variables
1. Angle
o The angle determines the current position of the sphere along the circular
path.
o It starts at 0 (which is the rightmost point of the circle).
o The angle increases step by step to move the sphere along the circular path.
2. Radius
o The radius defines the size of the circular path.
o A larger radius means a bigger circle, while a smaller radius means a tighter
loop.
3. Frame
o The frame number controls when a position is recorded in Blender’s
timeline.
o The animation starts at frame 0, and we increase frame number over time to
control animation speed.
Move the Sphere in a Circular Path: Use a while loop to update the sphere’s position in a
circle.
o Compute the new (x, y) coordinates using cosine & sine functions.
o Insert a keyframe at each step for animation.
o Increase angle to move the sphere forward in the circular path.
o Increase f to control the animation speed.
Full Code:
import bpy
import math
[Link].primitive_uv_sphere_add()
planet = [Link].active_object
# Step 1: Initialize animation parameters
frame = 0 # Start frame number
angle = 0 # Initial angle (in radians)
radius = 5 # Radius of the circular path
# Step 2: Loop to animate the sphere in a circular motion
while angle <= 2 * [Link]: # Complete one full circle (360 degrees)
x = [Link](angle) * radius # Compute new X position
y = [Link](angle) * radius # Compute new Y position
# Step 3: Update sphere location
[Link].x = x
[Link].y = y
# Step 4: Insert a keyframe at the current frame
planet.keyframe_insert("location", frame=frame)
# Step 5: Increment angle and frame number
angle += [Link](40) # Increase angle for the next step
frame += 20 # Move to the next frame
Task.1: First task is to create a moon that orbits (moves in a circular path)
around a planet
Task.2: Create multiple planets that move at different speeds, each rotating on
its axis.