May 23, 2012, 04:18:02 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Prowlie Welcome back! The site migration is complete and normal service resumed.
Advanced search
Pages: [1]   Go Down
Print
Author Topic: A question about Vpython (collisions)  (Read 1523 times)
0 Members and 1 Guest are viewing this topic.
adzy-2k6
Senior Member
****
Offline Offline

Posts: 720


« on: March 13, 2009, 03:49:57 AM »

Today i started an activity called scientific programming at college, which is an extra activity that visual python to program movement, momentum, etc. Well, i finished an activity today, but think i could shorten the code a bit.

Code:
#Importing Visual
from visual import*
#Generates the ball's and right, left, ceiling, floor, and back walls
ball1 = sphere(pos=(0,0,0), radius=0.5, color=color.yellow)
ball2 = sphere(pos=(0,1,0), radius=1, color=color.red)
ball3 = sphere(pos=(0,0,0), radius=2, color=color.blue)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
wallL = box(pos=(-6,0,0), size=(0.2,12,12), color=color.green)
wallC = box(pos=(0,6,0), size=(12,0.2,12), color=color.green)
wallB = box(pos=(0,0,-6), size=(12,12,0.2), color=color.red)
wallF = box(pos=(0,-6,0), size=(12,0.2,12), color=color.green)
#Sets the time interval to 0.05 and velocity to (1,1.5,2)
dt=0.05
ball1.velocity = vector(1,1,2)
ball2.velocity = vector(2,4,1)
ball3.velocity = vector(1,2,3)
while (1==1):
    #Ball1 collision controlls
    rate(100)
    ball1.pos = ball1.pos + ball1.velocity*dt
    if ball1.pos.x > wallR.pos.x:
        ball1.velocity.x = -ball1.velocity.x
    if ball1.pos.x < wallL.pos.x:
        ball1.velocity.x = -ball1.velocity.x
    if ball1.pos.y > wallC.pos.y:
        ball1.velocity.y = -ball1.velocity.y
    if ball1.pos.y < wallF.pos.y:
        ball1.velocity.y = -ball1.velocity.y
    if ball1.pos.z < wallB.pos.z:
        ball1.velocity.z = -ball1.velocity.z
    if ball1.pos.z > 6:
        ball1.velocity.z = -ball1.velocity.z
    #Ball2 collision controlls
    ball2.pos = ball2.pos + ball2.velocity*dt
    if ball2.pos.x > wallR.pos.x:
        ball2.velocity.x = -ball2.velocity.x
    if ball2.pos.x < wallL.pos.x:
        ball2.velocity.x = -ball2.velocity.x
    if ball2.pos.y > wallC.pos.y:
        ball2.velocity.y = -ball2.velocity.y
    if ball2.pos.y < wallF.pos.y:
        ball2.velocity.y = -ball2.velocity.y
    if ball2.pos.z < wallB.pos.z:
        ball2.velocity.z = -ball2.velocity.z
    if ball2.pos.z > 6:
        ball2.velocity.z = -ball2.velocity.z
        #Ball3 collision controlls
    ball3.pos = ball3.pos + ball3.velocity*dt
    if ball3.pos.x > wallR.pos.x - 1:
        ball3.velocity.x = -ball3.velocity.x
    if ball3.pos.x < wallL.pos.x + 1:
        ball3.velocity.x = -ball3.velocity.x
    if ball3.pos.y > wallC.pos.y - 1:
        ball3.velocity.y = -ball3.velocity.y
    if ball3.pos.y < wallF.pos.y+1:
        ball3.velocity.y = -ball3.velocity.y
    if ball3.pos.z < wallB.pos.z -1:
        ball3.velocity.z = -ball3.velocity.z
    if ball3.pos.z > 5:
        ball3.velocity.z = -ball3.velocity.z

As you can see, it is very long for something so simple. My main question is, is there a way to make a general if command, such as
Code:
if x.pos.y < wallF.pos.y:
        x.velocity.y = -x.velocity.y
And set x to represent one of the balls, and switch it between several objects., eg
Code:
x=ball1
if x.pos.y > wall.pos.y
x.velocity.y = -x.velocity.y
?
Logged

Cel
Senior Member
****
Offline Offline

Gender: Male
Posts: 967


« Reply #1 on: May 09, 2009, 01:26:57 AM »

Yes, there is. Also, you need to consider the ball's mass. It's not a single point. It has width, height and depth.
You need to take out the collision test into a function, so you'll have something like (oh, and don't copy my syntax, I dont know python)
Code:
bool TestCollision(object ball, object wall, string axis)
{
swtich(axis)
{
case "x":
if ((ball.x >= wall.x) && (ball.x + ball.width <= wall.x))
{
return true;
}
case "y":
{
if ((ball.y >= wall.y) && (ball.y + ball.height <= wall.y))
{
return true;
}
}
case "z":
{
if ((ball.z >= wall.z) && (ball.z + ball.depth <= wall.z))
{
return true;
}
}

return false;
}

void TestRoomCollision(object ball)
{
if ((TestCollision(ball, wall1, "x") || (TestCollision(ball, wall2, "x"))
{
ball.velocity.x *= -1;
}

if ((TestCollision(ball, wall3, "y") || (TestCollision(ball, wall4, "y"))
{
ball.velocity.y *= -1;
}

if ((TestCollision(ball, wall5, "z") || (TestCollision(ball, wall3, "z"))
{
ball.velocity.z *= -1;
}
}

And then in the main, you do this:
Code:
while (true)
{
TestRoomCollision(ball1);
TestRoomCollision(ball2);
TestRoomCollision(ball3);
}
Logged

My DeviantArt

<a href="http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf</a><a href="http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf" target="_blank">http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf</a>
Brackenwood
   

 Logged
Pages: [1]   Go Up
Print
Jump to:  

Theme by Pieter, based on Black Rain by Crip Powered by SMF 1.1.16 | SMF © 2011, Simple Machines XHTML | CSS

Page created in 0.05 seconds with 25 queries.