getting a ball to bounce inside a rotating cube??
Author |
Message |
coolgirl97
|
Posted: Thu Jun 06, 2013 8:37 am Post subject: getting a ball to bounce inside a rotating cube?? |
|
|
hey... i was given this assignment where i have a box that needs to rotate in all direction when pressing the arrow keys. There needs to be a ball moving inside the box as well...
i've got the box to rotate, and tried to get the ball to bounce around... but i think there's something wrong with my coding... because it is not working..
can u please have a look below, and help?? thank you so much!!
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%getting the cube
setscreen ("graphics:1280,1080")
%Variable for Colour
var box, background : int
%Variable for Size
var S : real
%Perspective Related Variables
var theta, phi, psi : real
%Location Related Variables
var x_cm, y_cm : real
%Cube Related Variables
var cube_x, cube_y, cube_z, vort_x, vort_y, vort_z, p_x, p_y : array 1 .. 8 of real
%---------------------------------------------------------------------------------------------
const DELTA_TIME := 1
const TIME_DELAY := 10
const DELTA_THETA := 0.1
const DELTA_PHI := 0.1
const DELTA_PSI := 0.1
background := 0
box := 7
S := 200
const ZV := 1500
var ch : string (1)
x_cm := 640
y_cm := 600
phi := 0
psi := 0
theta := 300
var t : real
const L := 75
const w := 20
t := 0
%---------------------------------------------------------------------------------------------
cube_x (1) := +S
cube_y (1) := +S
cube_z (1) := +S
cube_x (2) := -S
cube_y (2) := -S
cube_z (2) := -S
cube_x (3) := -S
cube_y (3) := +S
cube_z (3) := +S
cube_x (4) := +S
cube_y (4) := -S
cube_z (4) := -S
cube_x (5) := -S
cube_y (5) := -S
cube_z (5) := +S
cube_x (6) := +S
cube_y (6) := +S
cube_z (6) := -S
cube_x (7) := +S
cube_y (7) := -S
cube_z (7) := +S
cube_x (8) := -S
cube_y (8) := +S
cube_z (8) := -S
%---------------------------------------------------------------------------------------------
Draw.FillBox (0, 0, maxx, maxy, background)
%---------------------------------------------------------------------------------------------
%Rotative Formula
loop
for i : 1 .. 8
vort_x (i) := (cos (theta) * cos (phi) * cos (psi) - sin (phi) * sin (psi)) * cube_x (i) -
(cos (theta) * cos (phi) * sin (psi) + sin (phi) * cos (psi)) * cube_y (i) +
sin (theta) * cos (phi) * cube_z (i)
vort_y (i) := (cos (theta) * sin (phi) * cos (psi) + cos (phi) * sin (psi)) * cube_x (i) -
(cos (theta) * sin (phi) * sin (psi) - cos (phi) * cos (psi)) * cube_y (i) +
sin (theta) * sin (phi) * cube_z (i)
vort_z (i) := -sin (theta) * cos (psi) * cube_x (i) +
sin (theta) * sin (psi) * cube_y (i) +
cos (theta) * cube_z (i)
end for
for i : 1 .. 8
p_x (i) := vort_x (i) / (1 - vort_z (i) / ZV)
p_y (i) := vort_y (i) / (1 - vort_z (i) / ZV)
end for
%---------------------------------------------------------------------------------------------
%Cube Illustration
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (3)), round (y_cm + p_y (3)), box)
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (6)), round (y_cm + p_y (6)), box)
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), box)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (4)), round (y_cm + p_y (4)), box)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (5)), round (y_cm + p_y (5)), box)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), box)
drawline (round (x_cm + p_x (3)), round (y_cm + p_y (3)), round (x_cm + p_x (5)), round (y_cm + p_y (5)), box)
drawline (round (x_cm + p_x (3)), round (y_cm + p_y (3)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), box)
drawline (round (x_cm + p_x (4)), round (y_cm + p_y (4)), round (x_cm + p_x (6)), round (y_cm + p_y (6)), box)
drawline (round (x_cm + p_x (4)), round (y_cm + p_y (4)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), box)
drawline (round (x_cm + p_x (5)), round (y_cm + p_y (5)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), box)
drawline (round (x_cm + p_x (6)), round (y_cm + p_y (6)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), box)
delay (TIME_DELAY)
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (3)), round (y_cm + p_y (3)), white)
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (6)), round (y_cm + p_y (6)), white)
drawline (round (x_cm + p_x (1)), round (y_cm + p_y (1)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), white)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (4)), round (y_cm + p_y (4)), white)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (5)), round (y_cm + p_y (5)), white)
drawline (round (x_cm + p_x (2)), round (y_cm + p_y (2)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), white)
drawline (round (x_cm + p_x (3)), round (y_cm + p_y (3)), round (x_cm + p_x (5)), round (y_cm + p_y (5)), white)
drawline (round (x_cm + p_x (3)), round (y_cm + p_y (3)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), white)
drawline (round (x_cm + p_x (4)), round (y_cm + p_y (4)), round (x_cm + p_x (6)), round (y_cm + p_y (6)), white)
drawline (round (x_cm + p_x (4)), round (y_cm + p_y (4)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), white)
drawline (round (x_cm + p_x (5)), round (y_cm + p_y (5)), round (x_cm + p_x (7)), round (y_cm + p_y (7)), white)
drawline (round (x_cm + p_x (6)), round (y_cm + p_y (6)), round (x_cm + p_x (8)), round (y_cm + p_y (8)), white)
%---------------------------------------------------------------------------------------------
% Manipulating the Cube
if hasch then
getch (ch)
%Turn Upwards
if ch = chr (200) then
psi := psi + DELTA_PSI
%Turn Downwards
elsif ch = chr (208) then
psi := psi - DELTA_PSI
%Turn Right
elsif ch = chr (205) then
theta := theta + DELTA_THETA
%Turn Left
elsif ch = chr (203) then
theta := theta - DELTA_THETA
end if
end if
% ______________________________________________________________________________________________________________________
const DELTATIME := 0.10
const TIMEDELAY := 10
var x, y, z, x0, y0, z0, v0x, v0y, v0z, T : real
const x_right_boundary := 1100
const x_left_boundary := 250
const y_top_boundary := 1000
const y_bottom_boundary := 250
const RADIUS := 0.025
const DEPTH_SCALE := 0.1
x0 := 100
y0 := 100
z0 := 100
v0x := 30
v0y := 10
v0z := 20
T := 0
x := x0 + v0x * T
y := y0 + v0y * T
z := z0 + v0z * T
drawoval (645, 600, 20, 20, black)
delay (TIME_DELAY)
drawfilloval (645, 600, 20, 20, white)
T := T + 1
if x > x_right_boundary then
x0 := 2 * x_right_boundary - x
y0 := y
v0x := -v0x
T := 0
end if
if x < x_left_boundary then
x0 := 2 * x_left_boundary - x
v0x := -v0x
y0 := y
T := 0
end if
if y > y_top_boundary then
y0 := 2 * y_top_boundary - y
v0y := -v0y
x0 := x
T := 0
end if
if y < y_bottom_boundary then
y0 := 2 * y_bottom_boundary - y
v0y := -v0y
x0 := x
T := 0
end if
end loop |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
MihaiG

|
Posted: Thu Jun 06, 2013 8:04 pm Post subject: Re: getting a ball to bounce inside a rotating cube?? |
|
|
Cool program, before we go ahead and address the main issue you're having lets talk about some basic code practices.
In your main loop you have (roughly lines 170) a bunch of variable/constant declaration inside a loop.. does this seem right? We can move those outside the loop.
I usually keep all my parameters at the very top so i can easily adjust them!
Second of all, try to refrain from using getch, it only allows for the detection of one key intut at a time, i like using Input.KeyDown
In the code below
code: |
f x > x_right_boundary then
x0 := 2 * x_right_boundary - x
y0 := y
v0x := -v0x
T := 0
end if
if x < x_left_boundary then
x0 := 2 * x_left_boundary - x
v0x := -v0x
y0 := y
T := 0
end if
if y > y_top_boundary then
y0 := 2 * y_top_boundary - y
v0y := -v0y
x0 := x
T := 0
end if
if y < y_bottom_boundary then
y0 := 2 * y_bottom_boundary - y
v0y := -v0y
x0 := x
T := 0
end if
|
You seem to be forgetting what happens if it hits the z boundry!
Your drawing code can be placed in a seperate procedure so your code can be more readable.
From a quick look. I think you need to read up on planes in R3 (3d space). Your if statements try to assume that even if we rotate the cube each side has a fixed value for x,y,z while in reality, they have a wide range of values. Ie. If the ball hits the bottom of the cube near the front the x could be 5 while if it hits it at the back it could be -10.
This is a tough endeavor, your concept for rotating in 3d is well done. I might suggest trying to bounce the ball of a square rotating in R2 space. Once you understand the concept youll see it's not a matter of if x < a certain value. It's if a point interesects with a plane in R3.
If you can show me an example of a rotating square in 2d and a ball bouncing around i will guide and give you some further tips! |
|
|
|
|
 |
scii
|
Posted: Tue Aug 13, 2013 12:42 am Post subject: Re: getting a ball to bounce inside a rotating cube?? |
|
|
You can search it in google |
|
|
|
|
 |
|
|