0% found this document useful (0 votes)
19 views6 pages

2023fall223n-assignment4

The assignment requires creating a user interface for a Ricochet Ball simulation where a ball bounces off walls without friction. Users will input the ball's speed and direction, and the program will update the ball's position and handle collisions with walls. The assignment is due on November 6, 2023, and is worth 20 points, with additional challenge elements that have been canceled.

Uploaded by

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

2023fall223n-assignment4

The assignment requires creating a user interface for a Ricochet Ball simulation where a ball bounces off walls without friction. Users will input the ball's speed and direction, and the program will update the ball's position and handle collisions with walls. The assignment is due on November 6, 2023, and is worth 20 points, with additional challenge elements that have been canceled.

Uploaded by

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

2023 Fall CPSC 223n

Assignment 4
Ricochet Ball

Background

Think of your graphic panel as the top of a billiard table. When the cue ball hits the edge of
the table it ricochets off the edge and continues traveling in another direction. In a real
billiards game friction will make the ball eventually stop moving or perhaps the ball falls into a
pocket, and then it has obviously stopped moving. In this assignment we assume there are
no side pockets and no friction. Here the ball keeps moving forever at the same constant
speed: well, actually it moves until you click on the new button or the exit button.

Basic requirements

Make a user interface similar to the diagram in this document. This interface will have a small
header panel at the top, a large graphic panel in the middle, and a medium size control panel
at the bottom. When the interface first appears there are no objects in view. The plain
background of the graphical area is the only thing visible.

A picture is better than words

The next page has an artist's conception of the appearance of the form.
Sample User Interface

Ricochet Ball by Pierre Stuvesent

Enter Speed (pixel/second)


Go

0.0 100.0

Enter Direction (degrees) 35.85 Quit

Here is how it works

The play begins with a ball at the center of the graphic area.

The user inputs the initial direction of travel degrees. Imagine there are invisible x and y
axes in the graph panel. Treat the graphic panel like a cartesian plane in the math class. If
(x,y) is a point in the plane then its angle (direction of travel) is the angle formed by the x-axis
and the straight line between (x,y) and the origin. In this discussion we call that angle Θ.
The user inputs the speed of the ball by using the slider bar. The slider bar in this picture is
based on the scale from 0.0 to 100.0. You may choose another scale if desired, such as 0.0
to 150.0. You decide this matter.

When the user clicks on GO, both clocks start ticking. Also, Δx and Δy are computed.

In this case we don’t have those similar triangles as in assignments 3 and 2. But, we know
the following.

1. Let D = speed in pix/sec read from the slider

2. Then compute d = speed in pix/tic

3. Then Δx = d * cos(Θ) and Δy = d * sin(Θ).

During execution the user may change the speed by moving the slider bar. That
implies that each time the motion clock make one tic the the three calculations above
have to be repeated.

When the user clicks on Go the label changes to Pause. When the user clicks of Pause the
label changes to Go.

Use you past experience to choose good frequencies for motion clock and refresh clock. If
you don’t have an idea send a private message to a classmate and find out what he or she
uses for those numbers.

When the ball touches a wall it bounces off of that wall and continues moving. This will be
explained in lecture or you can skip the lecture and read the next to last page of this
document.

That concludes the baseline program


Due date: November 6, 2023 on or before midnight between November 5 and November 6.

The baseline program is worth 20 points. The Challenge program will be included here in a
few days. The challenge will be worth only 5 points.

Update: I regret to say that there will be no challenge part of this program.
Lecture material:

This is suppose to be explained in the lecture. But let’s put it here any way.

One of the key subjects of computer graphics is “collision detection” and what to do after the
collision has happened.

Each time the motion clock tics the coordinates of the ball are updated and then a check is
made to see if the ball has collided with one of the walls. This happens inside the handler
function of the motion clock.

Since there are 4 walls the handler function must run 4 different checks for a collision. If there
is no collision with any of the 4 walls then nothing special happens.

If there is a collision with one of the wall then Δx or Δy must be updated.

Here is the update algorithm.


If the ball collides with the north or south walls then set Δy = -Δy.
If the ball collides with the east or west walls then set Δx = - Δx.

We could prove these changes correctly implement a “bounce” off of the wall, but for now
simply use the two equations.

I think that is the end of the lecture material.


Dates

Midterm is scheduled for October 17

This assignment #4 will be due one week later: October 24.

Enjoy your program. Watch the ball bounce.

Practice run: Guess the size of the input direction in degrees that will make your ball go
directly into one of the 4 corners. Run the program repeatedly and each time refine your
direction angle until you can place the ball directly into one corner.

See if you can confuse the ball in such a way that it does not know which wall it is bouncing
off of. I don’t think you can confuse it, but it is fun to try.

You might also like