Courses/CS 461/Winter 2006/Jeff Bailey/Homework 1

From CSWiki

Jump to: navigation, search

Contents


[edit] Demonstration

Demonstrated in class 14 Jan, 2006


[edit] Example

This example was mostly a result of my playing with various positional functions within NetLogo. Basically it is just a bunch of balls contained within the window. Rather than use the heading and forward to handle movement, I decided to play around with the trigonometric functions instead.

Collisions in this version cause death of all objects involved. I intended to handle elastic ball collisions but I got confused about the coordinate system and spent a lot of effort trying to normalize it. Basically four hours of bad math and stupidity on my part. It turns out that the coordinate system is just fine and I will likely attempt the ball collisions at some point later.

The collision algorithm itself is trivial and can be thought of as a rotated wall collision. They key point is to identify the tangent line in order to normalize everything. To get the average line, we find the line perpendicular to the line formed by the positions of each turtle. This is found by calculating theta = arctan (x1-x2)/(y1-y2). The wall is the line perpendicular to this. We then normalize this line (rotating the entire world by -theta) and recompute the horizontal and vertical velocities. This information gives us the new position of the ball relative to the normal line and then everything is rotated back into position and we go on our merry way.

A couple of minor problems with this model which raises the question of how to best handle debugging (in particular, stepping)

[edit] Screen Shot

Image:bounce.png

[edit] Source

turtles-own [
  vel-x
  vel-y
  pos-x
  pos-y
]

to setup
  clear-all
  setup-turtles
  setup-patches
end

to setup-turtles
  set-default-shape turtles "circle"
  create-turtles balls
  ask turtles [
    let theta (random 360)
    set vel-x speed * (cos theta)
    set vel-y speed * (sin theta)   
    set pos-x random screen-size-x - (screen-edge-x)
    set pos-y random screen-size-y - (screen-edge-y)
    setxy pos-x pos-y
  ]
end

to setup-patches
  ask patches [
    set pcolor black
  ]
end

to go
  throw
  do-plots
  if count turtles <= 0 [
    stop
  ]    
end
 
to throw
  ask turtles [
    let new-x pos-x + vel-x
    let new-y pos-y + vel-y
 
    ; bounce off left/right walls
    if abs (new-x) >= abs (screen-edge-x) [
      set vel-x (- vel-x)
    ]

    ; bounce off top/bottom walls
    if abs (new-y) >= abs (screen-edge-y) [
      set vel-y (- vel-y)
    ]

    ; collisions
    if collisions [
      if count other-turtles-here > 0 [
        ask turtles-here [
           die
        ]
      ]
    ]      

    set pos-x pos-x + vel-x
    set pos-y pos-y + vel-y

    setxy pos-x pos-y
  ]
end
  
to do-plots
  set-current-plot "Collisions"
  plot balls - count turtles
end

[edit] Download

Bounce.zip

Personal tools