0% found this document useful (0 votes)
2 views

Lab.3_Computer Graphics

The document outlines a computer graphics lab exercise involving the animation of two cubes, where one cube moves in a square path around a stationary cube while also rotating at each corner. It also describes the animation of a sphere moving in a circular path using sine and cosine functions, with a loop to automate keyframe insertion for smooth movement. Additionally, it includes tasks for creating a moon orbiting a planet and multiple planets moving at different speeds while rotating on their axes.

Uploaded by

vohev62095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab.3_Computer Graphics

The document outlines a computer graphics lab exercise involving the animation of two cubes, where one cube moves in a square path around a stationary cube while also rotating at each corner. It also describes the animation of a sphere moving in a circular path using sine and cosine functions, with a loop to automate keyframe insertion for smooth movement. Additionally, it includes tasks for creating a moon orbiting a planet and multiple planets moving at different speeds while rotating on their axes.

Uploaded by

vohev62095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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
bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add()
c = bpy.context.active_object

c.location = (10, 0, 0) # Set initial position


c.keyframe_insert("location", frame=1)

c.location = (0, -10, 0) # Move the cube Left


c.keyframe_insert("location", frame=50)

c.location = (-10, 0, 0) # Move the cube Up


c.keyframe_insert("location", frame=100)

c.location = (0, 10, 0) # Move the cube Right


c.keyframe_insert("location", frame=150)

c.location = (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 = math.radians(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


bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_cube_add()
c = bpy.context.active_object # Moving cube

# Initial position and rotation (Frame 1)


c.location = (10, 0, 0)
c.rotation_euler.z = math.radians(0)
c.keyframe_insert("location", frame=1)
c.keyframe_insert("rotation_euler", frame=1)

# Move Left (Frame 50)


c.location = (0, -10, 0)
c.rotation_euler.z = math.radians(90)
c.keyframe_insert("location", frame=50)
c.keyframe_insert("rotation_euler", frame=50)

# Move Up (Frame 100)


c.location = (-10, 0, 0)
c.rotation_euler.z = math.radians(180)
c.keyframe_insert("location", frame=100)
c.keyframe_insert("rotation_euler", frame=100)

# Move Right (Frame 150)


c.location = (0, 10, 0)
c.rotation_euler.z = math.radians(270)
c.keyframe_insert("location", frame=150)
c.keyframe_insert("rotation_euler", frame=150)

# Return to start (Frame 200)


c.location = (10, 0, 0)
c.rotation_euler.z = math.radians(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

bpy.ops.mesh.primitive_uv_sphere_add()
planet = bpy.context.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 * math.pi: # Complete one full circle (360 degrees)
x = math.cos(angle) * radius # Compute new X position
y = math.sin(angle) * radius # Compute new Y position

# Step 3: Update sphere location


planet.location.x = x
planet.location.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 += math.radians(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.

You might also like