Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tutorial: Basic POOL and realistic collision
Index -> Programming, Turing -> Turing Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tendulkar




PostPosted: Wed Feb 04, 2004 4:23 pm   Post subject: (No subject)

this is great! excellent work thoughtful
Sponsor
Sponsor
Sponsor
sponsor
gamer




PostPosted: Thu Apr 08, 2004 2:39 pm   Post subject: (No subject)

helo thoughtful btw GREAT JOB on this pool program, u see im kind of new to program so my question is, do you think u can explain to me how the collision part of the program works?? i understand the fact that u applied trig but how exactly did you do it?
gamer




PostPosted: Sat Apr 10, 2004 6:21 pm   Post subject: (No subject)

heloo??

thoughtful can ya reply?

or anyone else who knows please tell me, i reli gota understand
thnx
gamer




PostPosted: Mon Apr 12, 2004 3:52 pm   Post subject: (No subject)

still no reply??? omg
Paul




PostPosted: Mon Apr 12, 2004 3:55 pm   Post subject: (No subject)

Thoughtful is not on Compsci very often, and don't triple post lol, Asian Sensation would be angry at you. Im sure whenever he gets on, he'll try and reply. You could always send him a PM.
Tony




PostPosted: Mon Apr 12, 2004 3:59 pm   Post subject: (No subject)

you find the angle
you break it up into vector components (x and y)
you find total energy transfer from the collision
you use vector components as ratios to redestribute the energy after collision.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
gamer




PostPosted: Mon Apr 12, 2004 4:14 pm   Post subject: (No subject)

thnx
evildaddy911




PostPosted: Sun Oct 16, 2011 1:35 pm   Post subject: RE:Tutorial: Basic POOL and realistic collision

i dont have gr11 math till next semester, can you explain what you mean by vector components?
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Wed Oct 19, 2011 3:33 pm   Post subject: RE:Tutorial: Basic POOL and realistic collision

It's actually physics... lets say you have a line. This line goes up by 3 and across by 4. Those woule be the x and y vector components: The change in x and the change in y.

Correect me if I'm wrong.
evildaddy911




PostPosted: Wed Oct 19, 2011 3:47 pm   Post subject: RE:Tutorial: Basic POOL and realistic collision

so its like finding slope, but not actually finding the slope, cuz slope is change in y divided by change in x, but you dont do the dividing
Raknarg




PostPosted: Wed Oct 19, 2011 3:53 pm   Post subject: RE:Tutorial: Basic POOL and realistic collision

Its basically trigonometry. Usually when you're working with vectors, you are given a bunch of distances with directions (ex. 35m[E25oS], or 25 meters, East 25 degrees to the South) and from that you calculate the overall displacement and the overall angle (compared from your starting point to your finishing point).

Of course, this is a really bad explaination. You should look it up:
http://zonalandeducation.com/mstm/physics/mechanics/vectors/introduction/introductionVectors.html
That seems promising.
evildaddy911




PostPosted: Thu Oct 20, 2011 3:29 pm   Post subject: Re: Tutorial: Basic POOL and realistic collision

okay, so the vectors are really just horizontal/vertical distances.... and i puzzled out a quick screensaver-like program...
im not sure what you mean by displacement, but i think i could figure out the angle...
anyways, voila:
Turing:

var x, y, xv, yv : int
var change_time : int := 10000
var last_change : int := -change_time
x := Rand.Int (10, maxx - 10)
y := Rand.Int (10, maxy - 10)
xv := Rand.Int ((-5), 5)
yv := Rand.Int ((-5), 5)
loop
    var now := Time.Elapsed
    if now - last_change >= change_time then
        xv := Rand.Int ((-5), 5)
        yv := Rand.Int ((-5), 5)
        last_change := now
    end if
    drawfilloval (x, y, 10, 10, white)
    x := xv + x
    y := y + yv
    if x <= 10 or x >= maxx - 10 then
        xv := xv * (-1)
    end if
    if y >= maxy - 10 or y <= 10 then
        yv := yv * (-1)
    end if

    drawfilloval (x, y, 10, 10, black)
    drawfilloval (x - 2, y, 1, 2, brightgreen)
    drawfilloval (x + 2, y, 1, 2, brightgreen)
    drawline (x - 3, y - 4, x + 3, y - 4, brightred)
    delay (20)
end loop

it draws a smiley face ball bouncing off the walls, and every 10 seconds it randomly changes the x and y vectors
Raknarg




PostPosted: Thu Oct 20, 2011 3:53 pm   Post subject: RE:Tutorial: Basic POOL and realistic collision

That would work. You could even use angles:

Turing:

View.Set ("offscreenonly, graphics:700;700")

const xPos := 350
const yPos := 350
var x, y, b : int := 0 % variables for the Mouse.Where
var t : real % Theta, or the angle
var bltx, blty, bltvx, bltvy : real := 0 % the bullet positions, and the amount the x and y changes
var bltd : boolean := true % whether or not the bullet is live or dead

loop
    Mouse.Where (x, y, b)
    if x = xPos and y > yPos then
        t := 270
    elsif x < xPos and y = yPos then
        t := 0
    elsif x = xPos and y < yPos then
        t := 90
    elsif x > xPos and y = yPos then
        t := 180
    elsif x = xPos and y = yPos then
        t := 0
    elsif x > xPos then
        t := arctand ((y - yPos) / (x - xPos)) + 180
    else
        t := arctand ((y - yPos) / (x - xPos))
    end if
    % This thing here calculates your angle. It'll make sense if you've ever done vectors before. It uses the function tan-1. The ones before it are for mathematical correctness; you cannot divide anything by zero.
   
    if b = 1 and bltd = true then % Only shoots when the bullet is dead and the button is clicked.
        bltd := false
        bltx := xPos - (40 * cosd (t)) % setting the position of the bullet
        blty := yPos - (40 * sind (t)) % same
        bltvx := -5 * cosd (t) % setting the change in x
        bltvy := -5 * sind (t) % setting the change in y
    end if
   
    Draw.FillOval (xPos, yPos, 20, 20, blue)
    Draw.ThickLine (xPos, yPos, xPos - round (40 * cosd (t)), yPos - round (40 * sind (t)), 4, 7) % This is important for drawing shapes according to an angle. You add onto the original postion. In the case of x, its the radius * cos(angle). For y it's radius * sin (angle)
    if bltd = false then
        Draw.FillOval (round (bltx), round (blty), 4, 4, brightred)
        Draw.FillOval (round (bltx), round (blty), 2, 2, 7)
        bltx += bltvx % Changes the bullet x position relative to the change in x
        blty += bltvy % Changes the bullet y position relative to the change in y
        if bltx > maxx or bltx < 0 or blty > maxy or blty < 0 then % If the bullet hits the edge, it dies
            bltd := true
        end if
    end if
    View.Update
    cls
end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 28 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: