Courses/CS 461/Winter 2006/Nghia Phan/Nghia Phan/Week 4

From CSWiki

Jump to: navigation, search

Contents

[edit] Demonstration

Demonstrated in class 04 Feb, 2006

[edit] Shooting Pool

I have been working for the whole week to figure out how to build this game. Luckily, I found some gas molecules modelings in the model library of Netlogo. So, I just modify the codes to build this game. Although it is not perfect, but it is playable.

[edit] Screen Shot

Image:Nghia_phan_week4_1.PNG Image:Nghia_phan_week4_2.PNG Image:Nghia_phan_week4_3.PNG

[edit] Instructions of how to play

1) Click on new game
2) If you want to setup the direction, click on setup direction button, and then move your mouse to the view. From here, you will see an arrow on the main ball moving in the direction of the mouse.
3) If you want to exit setup the direction, left-click on the view
4) And then click hit button to shoot the ball
5) Good Luck!

[edit] Download

Shooting Pool Version 1.0 Game
Shooting Pool Version 2.0 (Revised on 2/4/2006 @ 5:45 PM) Game
Shooting Pool Version 3.0 (Revised on 2/5/2006 @ 1:54 PM) Game

[edit] Code

  __extensions [ "sound.jar" ]  ;sound extension 

globals
[
  clock                     ;; how much simulated time has passed so far
  tick-length               ;; how much simulation will pass in this step
  box-edge                  ;; distance of box edge from axes
  colliding-particles
  sorted-colliding-particles
  colliding-particle-1
  colliding-particle-2
  colliding-pair
  original-tick-length
  last-view-update
  manage-view-updates?
  view-update-rate          ;; specifies the minimum amount of simulation time that must
                            ;; pass before the view is updated
                            
  main_ball_xcor
  main_ball_ycor
  main_ball_direction
  ball_is_not_moving?
  main_ball_size
  
  force_of_friction
]

breeds 
[
  particles
  direction_guides
]

particles-own
[
  speed
  mass
]

direction_guides-own []

;;;;;;;;;;;;;;;;

to setup

      foreach [60 62 64 65 67 69 71 72] [ 
      start-note "XYLOPHONE" ? 65
      wait 0.2
      stop-note "XYLOPHONE" ?
      ]
  ca
  set-default-shape particles "circle"
  set clock 0
  set manage-view-updates? true
  set view-update-rate 0.2
  set box-edge (screen-edge-x);set box-edge (screen-edge-x - 1)
  ;make-box
  
  set main_ball_xcor 0
  set main_ball_ycor (- screen-edge-y + 2)
  set main_ball_direction 1
  set main_ball_size 2.0
  set  ball_is_not_moving? true 
  
  ;;Create direction arrow on main ball
  create-custom-direction_guides 1
  [
    set xcor main_ball_xcor
    set ycor main_ball_ycor
    jump main_ball_size
  ]
  
  make-particles
  make-holes
  
  ;; set variable tick length based on fastest particle.   If the fastest particle has a speed of 1,
  ;; then tick-length is 1.  If the fastest particles has a speed of 10, then tick-length is 1/10.
  set tick-length (1 / (ceiling max values-from particles [speed]))
  set original-tick-length tick-length
end


to set_direction

;;if (mouse-inside? and ball_is_not_moving?)
if (mouse-inside?)
[
  ask turtle 1
  [
    set heading towardsxy-nowrap mouse-xcor mouse-ycor
    set main_ball_direction heading
  ]
  
  ask direction_guides
  [
    set xcor main_ball_xcor
    set ycor main_ball_ycor
    set heading main_ball_direction
    jump (main_ball_size)
  ]
]

;;if (mouse-down? and ball_is_not_moving?)
if (mouse-down?)
[
  ;;set ball_is_not_moving? false
  stop
]

end

to hit
  ask direction_guides[hideturtle]
  
  if any? turtles with [color = white]
  [
    ask turtle 1 
    [
      set speed Velocity
      set force_of_friction (Velocity / 1000)
    ]
    
    move_particles
  ]
  
  ask direction_guides
  [
    set xcor main_ball_xcor
    set ycor main_ball_ycor
    set heading main_ball_direction
    jump (main_ball_size)
    showturtle
  ]
  ;;set ball_is_not_moving? true
  stop
end

to move_particles
  set colliding-particles []
  ask particles [without-interruption [check-for-wall-collision]]
  ask particles [without-interruption [check-for-particle-collision ]]
  sort-collisions
  
  ask particles 
  [ 
    if (speed > 0)
    [
      jump speed * tick-length 
      set speed (speed - force_of_friction)
            
      if (value-from self [who] = 1)
      [
        set main_ball_direction heading
        set main_ball_xcor xcor
        set main_ball_ycor ycor
      ]
    ]
  ]
  
  if colliding-particles != []
      [collide-winners]
  set clock clock + tick-length

  ;; flag that updates display only after enough simulation time has passed.
  ;; the display-update-rate sets the minimum simulation time that must pass
  ;; before updating the display.  This avoids many redisplays of the view for
  ;; a series of small time steps in the simulation (which would make the view show
  ;; what looks like particles slowing down as they get near multiple collision points)
  if manage-view-updates? [
    if (clock - last-view-update) > view-update-rate
    [ display
      set last-view-update clock ]
  ]
  
  ;;Kill turtle if falls into black patches
  ask particles
  [
    if (pcolor-of patch-here = black)
    [
      if (value-from self [who] = 1)
      [
        user-message "You have lost the game.  Please try new game!"
      ]
      beep
      die
    ]
  ]
  
  ;;None of the particle has speed <= 0, then execute
  if (max values-from particles [speed] > 0)
  [
    move_particles
  ]
end

to-report convert-heading-x [heading-angle]
  report sin heading-angle
end

to-report convert-heading-y [heading-angle]
  report cos heading-angle
end


to check-for-particle-collision

;; check-for-particle-collision is a particle procedure that determines the time it takes to the collision between
;; two particles (if one exists).  It solves for the time by representing the equations of motion for
;; distance, velocity, and time in a quadratic equation of the vector components of the relative velocities
;; and changes in position between the two particles and solves for the time until the next collision

  let my-x xcor
  let my-y ycor
  let my-particle-size size
  let my-x-speed (speed * convert-heading-x heading)
  let my-y-speed (speed * convert-heading-y heading)

  ;; ask particles to check for a collision with that are in the set of particles lower in who # then myself
  ;; this ensures that check-for-particle-collision only happens once for a pair of particles

  ask particles with [( (self != myself) and (who < who-of myself) )]
  [
      without-interruption [

         let dpx (xcor - my-x)   ;; relative distance between particles in the x direction
         let dpy (ycor - my-y)    ;; relative distance between particles in the y direction
         let x-speed (speed * convert-heading-x heading) ;; speed of other particle in the x direction
         let y-speed (speed * convert-heading-y heading) ;; speed of other particle in the x direction
         let dvx (x-speed - my-x-speed) ;; relative speed difference between particles in the x direction
         let dvy (y-speed - my-y-speed) ;; relative speed difference between particles in the y direction
         let sum-r (((my-particle-size) / 2 ) + ((size-of self) / 2 )) ;; sum of both particle radii



        ;; To figure out what the difference in position (P1) between two particles at a future time (t) would be,
        ;; one would need to know the current difference in position (P0) between the two particles
        ;; and the current difference in the velocity (V0) between of the two particles.

        ;; The equation that represents the relationship would be:   P1 = P0 + t * V0

        ;; we want find when in time (t), P1 would be equal to the sum of both the particle's radii (sum-r).
        ;; When P1 is equal to is equal to sum-r, the particles will just be touching each other at
        ;; their edges  (a single point of contact).

        ;; Therefore we are looking for when:   sum-r =  P0 + t * V0

        ;; This equation is not a simple linear equation, since P0 and V0 should both have x and y components
        ;;  in their two dimensional vector representation (calculated as dpx, dpy, and dvx, dvy).


        ;; By squaring both sides of the equation, we get:     (sum-r) * (sum-r) =  (P0 + t * V0) * (P0 + t * V0)

        ;;  When expanded gives:   (sum-r ^ 2) = (P0 ^ 2) + (t * PO * V0) + (t * PO * V0) + (t ^ 2 * VO ^ 2)

        ;;  Which can be simplified to:    0 = (P0 ^ 2) - (sum-r ^ 2) + (2 * PO * V0) * t + (VO ^ 2) * t ^ 2

        ;;  Below, we will let p-squared represent:   (P0 ^ 2) - (sum-r ^ 2)
        ;;  and pv represent: (2 * PO * V0)
        ;;  and v-squared represent: (VO ^ 2)

        ;;  then the equation will simplifiy to:     0 = p-squared + pv * t + v-squared * t^2


         let p-squared   ((dpx * dpx) + (dpy * dpy)) - (sum-r ^ 2)   ;; p-squared represents difference of the square of the radii
                                                                     ;; and the square of the initial positions

         let pv  (2 * ((dpx * dvx) + (dpy * dvy)))  ;;the vector product of the position times the velocity
         let v-squared  ((dvx * dvx) + (dvy * dvy)) ;; the square of the difference in speeds
                                                    ;; represented as the sum of the squares of the x-component
                                                    ;; and y-component of relative speeds between the two particles


         ;; p-squared, pv, and v-squared are coefficients in the quadratic equation shown above that represents how distance
         ;; between the particles and relative velocity are related to the time, t, at which they
         ;; will next collide (or when their edges will just be touching)

         ;; Any quadratic equation that is the function of time (t), can represented in a general form as a*t*t + b*t + c = 0,
         ;; where a, b, and c are the coefficients of the three different terms, and has solutions for t
         ;; that can be found by using the quadratic formula.  The quadratic formula states that if a is not 0,
         ;; then there are two solutions for t, either real or complex.

         ;; t is equal to (b +/- sqrt (b^2 - 4*a*c)) / 2*a

         ;; the portion of this equation that is under a square root is referred to here
         ;; as the determinant, D1.   D1 is equal to (b^2 - 4*a*c)
         ;; and:   a = v-squared, b = pv, and c = p-squared.


         let D1 pv ^ 2 -  (4 * v-squared * p-squared)


         ;; the next line next line tells us that a collision will happen in the future if
         ;; the determinant, D1 is >= 0,  since a positive determinant tells us that there is a
         ;; real solution for the quadratic equation.  Quadratic equations can have solutions
         ;; that are not real (they are square roots of negative numbers).  These are referred
         ;; to as imaginary numbers and for many real world systems that the equations represent
         ;; are not real world states the system can actually end up in.

         ;; Once we determine that a real solution exists, we want to take only one of the two
         ;; possible solutions to the quadratic equation, namely the smaller of the two the solutions:

         ;;  (b - sqrt (b^2 - 4*a*c)) / 2*a
         ;;  which is a solution that represents when the particles first touching on their edges.

         ;;  instead of (b + sqrt (b^2 - 4*a*c)) / 2*a
         ;;  which is a solution that represents a time after the particles have penetrated
         ;;  and are coming back out of each other and when they are just touching on their edges.


         let time-to-collision  -1

         if D1 >= 0
            [
              ifelse (v-squared = 0)
              [
                set time-to-collision (- pv - sqrt D1)
              ]
              [
                set time-to-collision (- pv - sqrt D1) / (2 * v-squared) 
              ]
            ]        ;;solution for time step


         ;; if time-to-collision is still -1 there is no collision in the future - no valid solution
         ;; note:  negative values for time-to-collision represent where particles would collide
         ;; if allowed to move backward in time.
         ;; if time-to-collision is greater than 1, then we continue to advance the motion
         ;; of the particles along their current trajectories.  They do not collide yet.

         if time-to-collision > 0 and time-to-collision < 1
             [
              set colliding-pair (list time-to-collision self myself)  ;; sets a three element list of time to
                                                        ;; collision and the colliding pair
              set colliding-particles lput colliding-pair colliding-particles  ;; adds above list to collection
                                                                               ;; of colliding pairs and time
                                                                               ;; steps
             ]
      ]
  ]

end


to check-for-wall-collision ;; particle procedure for determining if a particle will hit one of the
                            ;; four walls of the box

  let x-speed (speed * convert-heading-x heading)
  let y-speed (speed * convert-heading-y heading)
  
  
  let xpos-plane (screen-edge-x - 0.5);let xpos-plane (box-edge - 0.5)      ;;inside boundary of right side of the box
  let xneg-plane (- screen-edge-x + 0.5);let xneg-plane (- box-edge + 0.5)    ;;inside boundary of left side of the box
  let ypos-plane (screen-edge-y - 0.5);let ypos-plane (box-edge - 0.5)      ;;inside boundary of top side of the box
  let yneg-plane (- screen-edge-y + 0.5);let yneg-plane (- box-edge + 0.5)    ;;inside boundary of bottom side of the box

  ;; find point of contact on edge of circle
  ;; points of contact located at 1 radius above, below, to the left, and to the right
  ;; of the center of the particle

  let contact-point-xpos (xcor + (size / 2))
  let contact-point-xneg (xcor - (size / 2))
  let contact-point-ypos (ycor + (size / 2))
  let contact-point-yneg (ycor - (size / 2))

  ;; find difference in position between plane location and edge of circle

  let dpxpos (xpos-plane - contact-point-xpos)
  let dpxneg (xneg-plane - contact-point-xneg)
  let dpypos (ypos-plane - contact-point-ypos)
  let dpyneg (yneg-plane - contact-point-yneg)

  let t-plane-xpos 0

  ;; solve for the time it will take the particle to reach the wall by taking
  ;; the distance to the wall and dividing it by the speed in the direction to the wall

  ifelse  x-speed != 0 [set t-plane-xpos (dpxpos / x-speed)] [set t-plane-xpos 0]
   if t-plane-xpos > 0 and t-plane-xpos  < 1
      [
       assign-colliding-wall t-plane-xpos "plane-xpos"
      ]

  let t-plane-xneg 0
  ifelse  x-speed != 0 [set t-plane-xneg (dpxneg / x-speed)] [set t-plane-xneg 0]
   if t-plane-xneg > 0 and t-plane-xneg  < 1
      [
       assign-colliding-wall t-plane-xneg "plane-xneg"
      ]
  let t-plane-ypos 0
  ifelse  y-speed != 0 [set t-plane-ypos (dpypos / y-speed)] [set t-plane-ypos 0]
   if t-plane-ypos > 0 and t-plane-ypos  < 1
      [
       assign-colliding-wall t-plane-ypos "plane-ypos"
      ]

  let t-plane-yneg 0
  ifelse  y-speed != 0 [set t-plane-yneg (dpyneg / y-speed)] [set t-plane-yneg 0]
  if t-plane-yneg > 0 and t-plane-yneg  < 1
      [
       assign-colliding-wall t-plane-yneg "plane-yneg"
      ]

end

to assign-colliding-wall [wall-position wall]
  ;; this procedure is used by the check-for-wall-collision procedure
  ;; to assemble the correct particle-wall pair

  set colliding-pair (list wall-position self wall)
  set colliding-particles lput colliding-pair colliding-particles

end


to sort-collisions
  ;; Sort the list of projected collisions between all the particles into an ordered list.
  ;; Take the smallest time-step from the list (which represents the next collision that will
  ;; happen in time).  Use this time step as the tick-length for all the particles to move through

  ifelse colliding-particles != []
   [
    set sorted-colliding-particles sort-by [first ?1 < first ?2] colliding-particles
    let dt min map [first ?] sorted-colliding-particles
    let winners filter [first ? = dt] sorted-colliding-particles
    set colliding-particle-1 item 1 first winners
    ;; colliding-particle-2 can both be an agent (the other colliding particle)
    ;; or a string (the name of the plane that the particle will collide with).
    set colliding-particle-2 item 2 first winners

      if dt > 0
         [set tick-length dt]

   ]
   ;; When there are no collisions for the next time step,
   ;; tick-length goes back to the value of original-tick-length
   [set tick-length original-tick-length]

end


to collide-winners  ;; deal with 3 possible cases of collisions:
                    ;; particle and one wall, particle and two walls, and two particles

    ;; deal with a case where the next collision in time is between a particle and a wall

    if colliding-particle-2 = "plane-xpos" or colliding-particle-2 = "plane-xneg"
         [ask colliding-particle-1 [set heading (- heading)]
          stop]
    if colliding-particle-2 = "plane-ypos" or colliding-particle-2 = "plane-yneg"
         [ask colliding-particle-1 [set heading (180 - heading)]
          stop]

    ;; deal with the remaining case of the next collision in time being between two particles.

    ask colliding-particle-1 [collide-with colliding-particle-2]

end


to collide-with [ other-particle ] ;; particle procedure

  ;;; PHASE 1: initial setup

    ;; for convenience, grab some quantities from other-particle
    let mass2 mass-of other-particle
    let speed2 speed-of other-particle
    let heading2 heading-of other-particle

  ;;modified so that theta is heading toward other particle
  let theta towards-nowrap other-particle

  ;;; PHASE 2: convert velocities to theta-based vector representation

  ;; now convert my velocity from speed/heading representation to components
  ;; along theta and perpendicular to theta
  let v1t (speed * cos (theta - heading))
  let v1l (speed * sin (theta - heading))

  ;; do the same for other-particle
  let v2t (speed2 * cos (theta - heading2))
  let v2l (speed2 * sin (theta - heading2))
    
  ;;; PHASE 3: manipulate vectors to implement collision

  ;; compute the velocity of the system's center of mass along theta
  let vcm (((mass * v1t) + (mass2 * v2t)) / (mass + mass2) )

  ;; now compute the new velocity for each particle along direction theta.
  ;; velocity perpendicular to theta is unaffected by a collision along theta,
  ;; so the next two lines actually implement the collision itself, in the
  ;; sense that the effects of the collision are exactly the following changes
  ;; in particle velocity.
  set v1t (2 * vcm - v1t)
  set v2t (2 * vcm - v2t)


  ;;; PHASE 4: convert back to normal speed/heading

  ;; now convert my velocity vector into my new speed and heading
  set speed sqrt ((v1t * v1t) + (v1l * v1l))
  ;; if the magnitude of the velocity vector is 0, atan is undefined. but
  ;; speed will be 0, so heading is irrelevant anyway. therefore, in that
  ;; case we'll just leave it unmodified.
  if v1l != 0 or v1t != 0
    [ set heading (theta - (atan v1l v1t)) ]

  ;; and do the same for other-particle
  set speed-of other-particle sqrt (((v2t * v2t) + (v2l * v2l)))
  if v2l != 0 or v2t != 0
    [ set heading-of other-particle (theta - (atan v2l v2t)) ]

  ;; PHASE 5: final updates

  ;; now recolor, since color is based on quantities that may have changed
  recolor ask other-particle [ recolor ]
end


to recolor
    ;if color-scheme = "red-green-blue" [ recolor-banded ]
    ;if color-scheme = "blue shades" [ recolor-shaded ]
    ;if color-scheme  = "one color" [ recolor-none ]
end


to recolor-banded  ;; particle procedure
  let avg-speed 1
  ;; avg-speed is assumed to be 0.5, since particles are assigned a random speed between 0 and 1
  ;; particle coloring procedures for visualizing speed with a color palette,
  ;; red are fast particles, blue slow, and green in between.

  ifelse speed < (0.5 * avg-speed) ;; at lower than 50% the average speed
  [
    set color blue       ;; slow particles colored blue
  ]
  [
    ifelse speed > (1.5 * avg-speed) ;; above 50% higher the average speed
      [ set color red ]        ;; fast particles colored blue
      [ set color green ]      ;; medium speed particles colored green
  ]

end


to recolor-shaded
  let avg-speed 1
 ;; avg-speed is assumed to be 0.5, since particles are assigned a random speed between 0 and 1
 ;; a particle shading gradient is applied to all particles less than speed 1.5,
 ;; the uppermost threshold speed to apply the shading gradient to.

  ifelse speed < (3 * avg-speed)
  [ set color (sky - 3.001) + (8 * speed / (3 * avg-speed)) ]
  [ set color (sky + 4.999)]
end

to recolor-none
  set color green - 1
end


;;;
;;; drawing procedures
;;;

to make-box
  ask patches with [ ((abs pxcor = box-edge) and (abs pycor <= box-edge)) or
                     ((abs pycor = box-edge) and (abs pxcor <= box-edge)) ]
    [ set pcolor yellow ]
end

;;creates some holes
to make-holes

  let x (- screen-edge-x + 1)
  let y (screen-edge-y - 1)

  ask patches [set pcolor 67]

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]
  
  set x (screen-edge-x - 1)
  set y (screen-edge-y - 1)

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]
  
  set x (- screen-edge-x + 1)
  set y (- screen-edge-y + 1)

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]
  
  set x (screen-edge-x - 1)
  set y (- screen-edge-y + 1)

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]
  
  set x (- screen-edge-x + 1)
  set y (1)

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]
  
  set x (screen-edge-x - 1)
  set y (1)

  ask patch-at x y
  [
    ask neighbors [ set pcolor black ]
    set pcolor black
  ]

end

;; creates some particles
to make-particles
  create-custom-particles 1
  [

    set speed Velocity
    set xcor main_ball_xcor
    set ycor main_ball_ycor
    set heading random-float main_ball_direction
    set color white
    
    
    set size main_ball_size
    ;if (randomize-sizes?) [set size (size - (random-float  largest-particle-size ))]
    ;if (size < smallest-particle-size) [set size smallest-particle-size]
    set mass (size * size) ;; set the mass proportional to the area of the particle
    ;random-position
    ;recolor
  ]
  
  let prev_xcor -4
  let prev_ycor (screen-edge-y - 2)
  let number_of_balls 5
  
;;Create other balls
repeat 5
[

  let prev_xcor_2 prev_xcor
  
  repeat number_of_balls
  [
  
    create-custom-particles 1
    [

      set speed 0
      set xcor prev_xcor_2
      set ycor prev_ycor
      set heading random-float 360
      set color red
    
    
      set size main_ball_size
      ;if (randomize-sizes?) [set size (size - (random-float  largest-particle-size ))]
      ;if (size < smallest-particle-size) [set size smallest-particle-size]
      set mass (size * size) ;; set the mass proportional to the area of the particle
      ;random-position
      ;recolor
      
      set prev_xcor_2 (prev_xcor_2 + 2)
    ]
   ]
   
   set number_of_balls (number_of_balls - 1)
   set prev_ycor (prev_ycor - 2)
   set prev_xcor (prev_xcor + 1)
]
end

; *** NetLogo Model Copyright Notice ***
;
; This model was created as part of the projects:
; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN
; CLASSROOMS and INTEGRATED SIMULATION AND MODELING ENVIRONMENT.
; The project gratefully acknowledges the support of the
; National Science Foundation (REPP & ROLE programs) -- grant numbers
; REC #9814682 and REC-0126227.
;
; Copyright 2005 by Uri Wilensky.  Updated 2005.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Uri Wilensky.
; Contact Uri Wilensky for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in academic publications, please use:
; Wilensky, U. (2005).  NetLogo GasLab Circular Particles model.
; http://ccl.northwestern.edu/netlogo/models/GasLabCircularParticles.
; Center for Connected Learning and Computer-Based Modeling,
; Northwestern University, Evanston, IL.
;
; In other publications, please use:
; Copyright 2005 Uri Wilensky.  All rights reserved.
; See http://ccl.northwestern.edu/netlogo/models/GasLabCircularParticles
; for terms of use.
;
; *** End of NetLogo Model Copyright Notice ***

Personal tools