Class: GamesParadise::Billiard::Ball
- Inherits:
-
Object
- Object
- GamesParadise::Billiard::Ball
- Defined in:
- lib/games_paradise/gui/gosu/billiard/ball.rb
Constant Summary collapse
- POCKET_RADIUS =
#
POCKET_RADIUS
#
17.0
- COLOUR_FOR_THE_CUE_BALL =
#
COLOUR_FOR_THE_CUE_BALL
Here you can specify the colour for the cue ball in use.
#
0xffFFDDAE
Instance Method Summary collapse
-
#check_collision(inst) ⇒ Object
# === check_collision.
-
#check_collision_line(x1, y1, x2, y2) ⇒ Object
# === check_collision_line ========================================================================= #.
-
#check_collision_pocket(x, y) ⇒ Object
# === check_collision_pocket ========================================================================= #.
-
#check_collision_point(x, y) ⇒ Object
# === check_collision_point ========================================================================= #.
-
#check_placement_collision ⇒ Object
# === check_placement_collision.
-
#checkMouseClick ⇒ Object
# === checkMouseClick.
-
#collision_path(x1, y1, x2, y2) ⇒ Object
# === collision_path.
-
#collision_response(i) ⇒ Object
# === collision_response ========================================================================= #.
-
#collision_wall_path(x1, y1, x2, y2, wall_index) ⇒ Object
# === collision_wall_path ========================================================================= #.
-
#destroy ⇒ Object
# === destroy ========================================================================= #.
-
#dir? ⇒ Boolean
(also: #dir)
# === dir? ========================================================================= #.
-
#do_play_the_cheer_sound ⇒ Object
# === do_play_the_cheer_sound.
-
#draw ⇒ Object
# === draw ========================================================================= #.
-
#get_kin ⇒ Object
# === get_kin ========================================================================= #.
-
#id? ⇒ Boolean
(also: #id)
# === id? ========================================================================= #.
-
#in_hole ⇒ Object
# === in_hole ========================================================================= #.
-
#initialize(window, x, y, dir, vel, rad, id, colour) ⇒ Ball
constructor
# === initialize ========================================================================= #.
-
#is_this_the_cue_ball? ⇒ Boolean
# === is_this_the_cue_ball? ========================================================================= #.
-
#mass? ⇒ Boolean
(also: #mass)
# === mass? ========================================================================= #.
-
#move ⇒ Object
# === move.
-
#new_velocity(m1, m2, v1, v2, c1, c2) ⇒ Object
# === new_velocity ========================================================================= #.
-
#point_direction(x1, y1, x2, y2) ⇒ Object
# === point_direction ========================================================================= #.
-
#radius? ⇒ Boolean
(also: #radius)
# === radius? ========================================================================= #.
-
#release ⇒ Object
# === release ========================================================================= #.
-
#reset ⇒ Object
# === reset (reset tag) ========================================================================= #.
-
#update ⇒ Object
# === update ========================================================================= #.
-
#vel_x? ⇒ Boolean
(also: #vel_x)
# === vel_x? ========================================================================= #.
-
#vel_y? ⇒ Boolean
(also: #vel_y)
# === vel_y? ========================================================================= #.
-
#x? ⇒ Boolean
(also: #x)
# === x? ========================================================================= #.
-
#y? ⇒ Boolean
(also: #y)
# === y? ========================================================================= #.
Constructor Details
#initialize(window, x, y, dir, vel, rad, id, colour) ⇒ Ball
#
initialize
#
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 35 def initialize( window, x, y, dir, vel, rad, id, colour ) @window, @x, @y, @dir, @radius, @id, @color = window, x, y, dir, rad, id, colour # ======================================================================= # # This defines the area of the ball, based on a circle. # ======================================================================= # @mass = 3.14 * (@radius ** 2) # A = π·r² @vel_x = ::Gosu.offset_x(@dir, vel) @vel_y = ::Gosu.offset_y(@dir, vel) # ======================================================================= # # === @collision_point_x # ======================================================================= # @collision_point_x = @x # ======================================================================= # # === @collision_point_y # ======================================================================= # @collision_point_y = @y # ======================================================================= # # Used only by the cue ball # ======================================================================= # @release_force = 0.02 @placement_collision = false # ======================================================================= # # Is the ball out of the game? (AKA did the ball # reach the pocket yet?). # ======================================================================= # @out = false reset end |
Instance Method Details
#check_collision(inst) ⇒ Object
#
check_collision
This method is only called once for each pair of balls.
#
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 332 def check_collision(inst) if @x + @radius + inst.radius > inst.x and @x < inst.x + @radius + inst.radius and @y + @radius + inst.radius > inst.y and @y < inst.y + @radius + inst.radius dist = Gosu.distance(inst.x, inst.y, @x, @y) if dist < (@radius + inst.radius) @collision_point_x = ((@x * inst.radius) + (inst.x * @radius))/(@radius + inst.radius) @collision_point_y = ((@y * inst.radius) + (inst.y * @radius))/(@radius + inst.radius) @collision_point = true new_vel_self = new_velocity( @mass, inst.mass, Vector[@vel_x, @vel_y], Vector[inst.vel_x, inst.vel_y], Vector[@x, @y], Vector[inst.x, inst.y] ) new_vel_inst = new_velocity( inst.mass, @mass, Vector[inst.vel_x, inst.vel_y], Vector[@vel_x, @vel_y], Vector[inst.x, inst.y], Vector[@x, @y] ) # CALCULATE THE HIT SOUND VOLUME ####### v1 = Vector[@vel_x, @vel_y] v2 = Vector[inst.vel_x, inst.vel_y] c1 = Vector[@x, @y] c2 = Vector[inst.x, inst.y] dv = v1 - v2 ### Vector dc = c1 - c2 ### Vector hit_force = dv.inner_product(dc) ### Number if hit_force < 0 # If the balls are moving towards each other # ================================================================= # # Play the following sound upon hitting a ball. # ================================================================= # HIT_SOUND.play(-hit_force * 1.00/100.00) # When hit_force = 6 : Volume = 1 : Which means full volume # puts hit_force end collision_response(new_vel_self) inst.collision_response(new_vel_inst) end end end |
#check_collision_line(x1, y1, x2, y2) ⇒ Object
#
check_collision_line
#
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 169 def check_collision_line(x1, y1, x2, y2) vector_x = x2 - x1 vector_y = y2 - y1 point_to_vector_x = @x - x1 point_to_vector_y = @y - y1 vector_product = vector_x * point_to_vector_x + vector_y * point_to_vector_y vec_length_squared = vector_x**2 + vector_y**2 projection_factor = vector_product / vec_length_squared if projection_factor > 0 and projection_factor < 1 ### If the projected point is on the line. Projection factor is between 0 and 1. projected_point_x = projection_factor * vector_x + x1 projected_point_y = projection_factor * vector_y + y1 dist_to_player = Gosu.distance(@x, @y, projected_point_x, projected_point_y) if dist_to_player < radius ## Collision with projected point! vec = new_velocity(0, 10, Vector[@vel_x, @vel_y], Vector[0, 0], Vector[@x, @y], Vector[projected_point_x, projected_point_y]) collision_response(vec) end end end |
#check_collision_pocket(x, y) ⇒ Object
#
check_collision_pocket
#
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 151 def check_collision_pocket(x, y) dist2 = (x - @x)**2+(y - @y)**2 # Optimisation if dist2 < POCKET_RADIUS**2 # Optimisation ### Collision! # if @color == COLOUR_FOR_THE_CUE_BALL # @x = $universe_width*3/4 # @y = $universe_height/2 # @vel_x = 0.0 # @vel_y = 0.0 # else self.in_hole # end end end |
#check_collision_point(x, y) ⇒ Object
#
check_collision_point
#
203 204 205 206 207 208 209 210 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 203 def check_collision_point(x, y) dist2 = (x - @x)**2 + (y - @y)**2 if dist2 < @radius**2 # Collision! vec = new_velocity(0, 10, Vector[@vel_x, @vel_y], Vector[0, 0], Vector[@x, @y], Vector[x, y]) collision_response(vec) end end |
#check_placement_collision ⇒ Object
#
check_placement_collision
This method is used by the cue ball only
#
413 414 415 416 417 418 419 420 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 413 def check_placement_collision if @x < 0+@radius or @x > $universe_width-@radius or @y < 0+@radius or @y > $universe_height-@radius @placement_collision = true end end |
#checkMouseClick ⇒ Object
#
checkMouseClick
This method is used in particular for relocating the cue ball via the mouse.
#
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 218 def checkMouseClick # First calculate the net-distance. dist = Gosu.distance( @window.mouse_x-$window_width/2+$camera_x, @window.mouse_y-$window_height/2+$camera_y, @x, @y ) if dist < @radius case @color when COLOUR_FOR_THE_CUE_BALL if @out == false # ================================================================= # # The next line is not completely correct because it should be # increased upon a mouse-release event. But it is easier to # handle for now via this variable, so we retain it. # ================================================================= # @the_cue_ball_was_hit_n_times += 1 @pulled = true else # The following clause handles replacing the cue ball. if @being_replaced if @placement_collision == false e 'The cue ball was placed down.' @being_replaced = false @out = false end else @being_replaced = true e 'being replaced' end end end end end |
#collision_path(x1, y1, x2, y2) ⇒ Object
#
collision_path
Basically the same as “check_collison_line” except the radius is different and there is no collision response
#
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 283 def collision_path(x1, y1, x2, y2) if @color != COLOUR_FOR_THE_CUE_BALL and @out == false vector_x = x2 - x1 vector_y = y2 - y1 point_to_vector_x = @x - x1 point_to_vector_y = @y - y1 vector_product = vector_x * point_to_vector_x + vector_y * point_to_vector_y vec_length_squared = vector_x**2 + vector_y**2 projection_factor = vector_product / vec_length_squared if projection_factor > 0 and projection_factor < 1 ### If the projected point is on the line. Projection factor is between 0 and 1. projected_point_x = projection_factor * vector_x + x1 projected_point_y = projection_factor * vector_y + y1 dist_to_player = Gosu.distance(@x, @y, projected_point_x, projected_point_y) if dist_to_player < radius*2 # this balls radius, and the cue balls radius. They are the same. @colliding = true dt = Math.sqrt((radius*2)**2 - dist_to_player**2) / Math.sqrt(vec_length_squared) # Intersection point nearest to A t1 = projection_factor - dt int_x = x1 + t1 * vector_x int_y = y1 + t1 * vector_y col_x = (@x + int_x) / 2 col_y = (@y + int_y) / 2 $path_blockers << [int_x, int_y, col_x, col_y, 'ball'] end end end end |
#collision_response(i) ⇒ Object
#
collision_response
#
382 383 384 385 386 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 382 def collision_response(i) # @colliding = true @vel_x = i[0] @vel_y = i[1] end |
#collision_wall_path(x1, y1, x2, y2, wall_index) ⇒ Object
#
collision_wall_path
#
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 525 def collision_wall_path( x1, y1, x2, y2, wall_index ) if @pulled # Only called for the cue-ball ##### COLLISION BETWEEN TWO LINE SEGMENTS ##### Credits goes to this guy : http://stackoverflow.com/a/1968345 x3 = @x y3 = @y dir = point_direction( @window.mouse_x, @window.mouse_y, @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y ) x4 = @x + Gosu.offset_x(dir, 900) y4 = @y + Gosu.offset_y(dir, 900) s1_x = x2 - x1 s1_y = y2 - y1 s2_x = x4 - x3 s2_y = y4 - y3 s = (-s1_y * (x1 - x3) + s1_x * (y1 - y3)) / (-s2_x * s1_y + s1_x * s2_y) t = ( s2_x * (y1 - y3) - s2_y * (x1 - x3)) / (-s2_x * s1_y + s1_x * s2_y) if s >= 0 and s <= 1 and t >= 0 and t <= 1 # Collision Detected int_x = x1 + (t * s1_x) int_y = y1 + (t * s1_y) # Which way does the wall face? # From that; calculate the predicted collision point on the wall case wall_index when 0, :top_left # Top left col_x = int_x col_y = int_y-11.0 when 1, :top_right # Top right col_x = int_x col_y = int_y-11.0 when 2, :bottom_left # Bottom left col_x = int_x col_y = int_y+11.0 when 3, :bottom_right # Bottom right col_x = int_x col_y = int_y+11.0 when 4, :left # Left col_x = int_x-11.0 col_y = int_y when 5, :right # Right col_x = int_x+11.0 col_y = int_y end $path_blockers << [ int_x, int_y, col_x, col_y, 'wall', wall_index ] else ### No collison :( end end end |
#destroy ⇒ Object
#
destroy
#
439 440 441 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 439 def destroy @window.destroy_ball(self) end |
#dir? ⇒ Boolean Also known as: dir
#
dir?
#
518 519 520 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 518 def dir? @dir end |
#do_play_the_cheer_sound ⇒ Object
#
do_play_the_cheer_sound
Invoke this method whenever you wish to play the cheer-sound.
#
483 484 485 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 483 def do_play_the_cheer_sound CHEER_SOUND.play(0.8) # This is the volume; 1 means full-volume. end |
#draw ⇒ Object
#
draw
#
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 686 def draw # ======================================================================= # # Put a shadow around the ball. # ======================================================================= # @window.circle_img.draw_rot( @x-3+$window_width/2-$camera_x, @y+9+$window_height/2-$camera_y, 1, @dir, 0.5, 0.5, 1.0 * (@radius/50.0), 1.0 * (@radius/50.0), 0x44000000 ) # The ball itself. if @being_replaced == false if @colliding == false @window.circle_img.draw_rot(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(@radius/50.0), 1.0*(@radius/50.0), @color) else @window.circle_img.draw_rot(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(@radius/50.0), 1.0*(@radius/50.0), 0xff00FF00) if @collision_point @window.circle_img.draw_rot(@collision_point_x+$window_width/2-$camera_x, @collision_point_y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(5.0/50.0), 1.0*(5.0/50.0), 0xff0000FF) end end else if @placement_collision @window.circle_img.draw_rot(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(@radius/50.0), 1.0*(@radius/50.0), 0xaaFF0000) else @window.circle_img.draw_rot(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(@radius/50.0), 1.0*(@radius/50.0), 0xaa00FF00) end end # ======================================================================= # # Show specific instructions to the play when the cue ball is out # of the field. # ======================================================================= # if @color == COLOUR_FOR_THE_CUE_BALL and @out @window.font.draw_text( 'The cue ball is out... Click the ball to replace it on the table', $universe_width/2-140+$window_width/2-$camera_x, -75+$window_height/2-$camera_y, 3, 1.0, 1.0, COLOUR_BLACK ) end # ======================================================================= # # Light reflection # ======================================================================= # @window.circle_img.draw_rot( @x+1+$window_width/2-$camera_x, @y-5+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(4.0/50.0), 1.0*(4.0/50.0), 0x66ffffff ) # ======================================================================= # # Numbers drawn on the ball. # ======================================================================= # if @color != COLOUR_FOR_THE_CUE_BALL @window.circle_img.draw_rot(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(6.3/50.0), 1.0*(6.3/50.0), 0xffffffff) if @id > 9 # Numbers above 9, aka 10, 11 and so forth. @window.font_small.draw_text( "#{@id}", @x-6+$window_width/2-$camera_x, @y-6+$window_height/2-$camera_y, 3, 1.0, 1.0, ::Gosu::COLOUR_BLACK ) else @window.font_small.draw_text( "#{@id}", @x-3+$window_width/2-$camera_x, @y-6+$window_height/2-$camera_y, 3, 1.0, 1.0, ::Gosu::COLOUR_BLACK ) end if @id > 8 # Refer to the stripe_img next: @window.stripe_img.draw_rot( @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 2, @dir, 0.5, 0.5, 1.0*(@radius/50.0), 1.0*(@radius/50.0), 0xffffffff ) end end # ======================================================================= # # The release line will be drawn next. # ======================================================================= # if @pulled # ===================================================================== # # Calculate the direction from the mouse cursor to the cue ball. # ===================================================================== # dir = point_direction(@window.mouse_x, @window.mouse_y, @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y) # ===================================================================== # # Red line to the mouse # ===================================================================== # if ::Gosu.distance(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, @window.mouse_x, @window.mouse_y) < 300 @window.draw_line( @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 0xffff0000, @window.mouse_x, @window.mouse_y, 0xffff0000, 2 ) else @window.draw_line(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 0xffff0000, @x+$window_width/2-$camera_x+ ::Gosu.offset_x(dir-180, 300), @y+$window_height/2-$camera_y+Gosu.offset_y(dir-180, 300), 0xffff0000, 2) end # Find the nearest collision point in $path_blockers d = 10000 for i in 0..$path_blockers.length-1 dist = Gosu.distance(@x, @y, $path_blockers[i][0], $path_blockers[i][1]) if dist < d closest_int = $path_blockers[i].dup d = dist end end if !closest_int.nil? case closest_int[4] when 'ball' # The cue ball is hitting another ball @window.draw_line(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 0xffffffff, closest_int[0]+$window_width/2-$camera_x, closest_int[1]+$window_height/2-$camera_y, 0xffffffff, 2) @window.circle_img.draw_rot(closest_int[2]+$window_width/2-$camera_x, closest_int[3]+$window_height/2-$camera_y, 3, @dir, 0.5, 0.5, 1.0*(3.5/50.0), 1.0*(3.5/50.0), 0xff0000FF) vel_vec = new_velocity( 10, 10, Vector[closest_int[0]-@x, closest_int[1]-@y], Vector[0, 0], Vector[closest_int[0], closest_int[1]], Vector[closest_int[2], closest_int[3]] ) predicted_response_x = closest_int[0] + vel_vec[0] predicted_response_y = closest_int[1] + vel_vec[1] dir = point_direction(closest_int[0], closest_int[1], predicted_response_x, predicted_response_y) @window.draw_line(closest_int[0]+$window_width/2-$camera_x, closest_int[1]+$window_height/2-$camera_y, 0xffffffff, closest_int[0]+Gosu.offset_x(dir, 40)+$window_width/2-$camera_x, closest_int[1]+Gosu.offset_y(dir, 40)+$window_height/2-$camera_y, 0xffffffff, 3) else # The cue ball is hitting a wall @window.draw_line(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 0xffffffff, closest_int[0]+$window_width/2-$camera_x, closest_int[1]+$window_height/2-$camera_y, 0xffffffff, 2) @window.circle_img.draw_rot(closest_int[2]+$window_width/2-$camera_x, closest_int[3]+$window_height/2-$camera_y, 3, @dir, 0.5, 0.5, 1.0*(3.5/50.0), 1.0*(3.5/50.0), 0xff0000FF) dir = self.point_direction(@window.mouse_x, @window.mouse_y, @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y) predicted_vel_x = Gosu.offset_x(dir, 40) predicted_vel_y = Gosu.offset_y(dir, 40) case closest_int[5] when 0 ### Top left predicted_response_x = closest_int[0] + predicted_vel_x predicted_response_y = closest_int[1] - predicted_vel_y ### vel_y gets reversed when 1 ### Top right predicted_response_x = closest_int[0] + predicted_vel_x predicted_response_y = closest_int[1] - predicted_vel_y ### vel_y gets reversed when 2 ### Bottom left predicted_response_x = closest_int[0] + predicted_vel_x predicted_response_y = closest_int[1] - predicted_vel_y ### vel_y gets reversed when 3 ### Bottom right predicted_response_x = closest_int[0] + predicted_vel_x predicted_response_y = closest_int[1] - predicted_vel_y ### vel_y gets reversed when 4 ### Left predicted_response_x = closest_int[0] - predicted_vel_x ### vel_x gets reversed predicted_response_y = closest_int[1] + predicted_vel_y when 5 ### Right predicted_response_x = closest_int[0] - predicted_vel_x ### vel_x gets reversed predicted_response_y = closest_int[1] + predicted_vel_y end @window.draw_line( closest_int[0]+$window_width/2-$camera_x, closest_int[1]+$window_height/2-$camera_y, 0xffffffff, predicted_response_x+$window_width/2-$camera_x, predicted_response_y+$window_height/2-$camera_y, 0xffffffff, 3 ) end else ## White line showing the path @window.draw_line(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, 0xffffffff, @x+$window_width/2-$camera_x+Gosu.offset_x(dir, 900), @y+$window_height/2-$camera_y+Gosu.offset_y(dir, 900), 0xffffffff, 2) end end end |
#get_kin ⇒ Object
#
get_kin
#
425 426 427 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 425 def get_kin return (@mass * (@vel_x ** 2 + @vel_y ** 2)) end |
#id? ⇒ Boolean Also known as: id
#
id?
#
511 512 513 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 511 def id? @id end |
#in_hole ⇒ Object
#
in_hole
#
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 453 def in_hole if is_this_the_cue_ball? # ===================================================================== # # This is the case when the white ball hits one of the holes. # ===================================================================== # @x = $universe_width/2 @y = -100 @vel_x = 0.0 @vel_y = 0.0 @out = true else # ===================================================================== # # Else one of the main balls is kicked into a hole by the white # cue ball. In this case we will play a cheering-crowd sound. # ===================================================================== # do_play_the_cheer_sound @x = 10 + @window.balls_in_hole*22.5 @y = -100 @vel_x = 0.0 @vel_y = 0.0 @out = true @window.ball_in_hole end end |
#is_this_the_cue_ball? ⇒ Boolean
#
is_this_the_cue_ball?
#
593 594 595 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 593 def is_this_the_cue_ball? @color == COLOUR_FOR_THE_CUE_BALL end |
#mass? ⇒ Boolean Also known as: mass
#
mass?
#
504 505 506 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 504 def mass? @mass end |
#move ⇒ Object
#
move
This method is the one that will move the cue ball on the game map.
#
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 609 def move @x += @vel_x @y += @vel_y # Check collision with walls # Wall to the right if @x > ($universe_width-@radius) and @vel_x > 0 and 26 < @y and @y < $universe_height-26 @vel_x = -@vel_x end # ======================================================================= # # Wall to the left # ======================================================================= # if @x < @radius and @vel_x < 0 and 26 < @y and @y < $universe_height-26 @vel_x = -@vel_x end # ======================================================================= # # Wall at the bottom # ======================================================================= # if @y > ($universe_height-@radius) and @vel_y > 0 if (26 < @x and @x < $universe_width/2-22) or ($universe_width/2+22 < @x and @x < $universe_width-26) @vel_y = -@vel_y end end # ======================================================================= # # Wall at the top # ======================================================================= # if @y < @radius and @vel_y < 0 if (26 < @x and @x < $universe_width/2-22) or ($universe_width/2+22 < @x and @x < $universe_width-26) @vel_y = -@vel_y end end # ======================================================================= # # Check collision with lines # ======================================================================= # for i in 0..$lines_array.length-1 check_collision_line($lines_array[i][0], $lines_array[i][1], $lines_array[i][2], $lines_array[i][3]) end # ======================================================================= # # Check collision with corners # ======================================================================= # for i in 0..$pocket_coords.length-1 check_collision_point($pocket_coords[i][0], $pocket_coords[i][1]) end # ======================================================================= # # Check collision with pockets # ======================================================================= # for i in 0..$pocket_holes.length-1 check_collision_pocket($pocket_holes[i][0], $pocket_holes[i][1]) end # ======================================================================= # # Resistance # ======================================================================= # if (@vel_x**2+@vel_y**2) > 0.5**2 @vel_x = @vel_x * @resistance @vel_y = @vel_y * @resistance else @vel_x = @vel_x * @resistance_strong @vel_y = @vel_y * @resistance_strong end end |
#new_velocity(m1, m2, v1, v2, c1, c2) ⇒ Object
#
new_velocity
#
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 391 def new_velocity(m1, m2, v1, v2, c1, c2) f = (2*m2)/(m1+m2) # Number --- f = 1 when both masses are the same dv = v1 - v2 # Vector dc = c1 - c2 # Vector v_new = v1 - f * (dv.inner_product(dc))/(dc.inner_product(dc)) * dc # Vector if dv.inner_product(dc) > 0 ### If the balls are NOT moving towards each other return v1 # Vector else return v_new # Vector end end |
#point_direction(x1, y1, x2, y2) ⇒ Object
#
point_direction
#
432 433 434 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 432 def point_direction(x1, y1, x2, y2) return ((Math.atan2(y2-y1, x2-x1) * (180/Math::PI)) + 450) % 360 end |
#radius? ⇒ Boolean Also known as: radius
#
radius?
#
497 498 499 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 497 def radius? @radius end |
#release ⇒ Object
#
release
#
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 257 def release if @pulled ## Direction from ball to mouse dir = point_direction(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, @window.mouse_x, @window.mouse_y) ## If the distance from mouse to ball is less than 300 pixels if Gosu.distance(@x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y, @window.mouse_x, @window.mouse_y) < 300 ## The distance from ball to mouse is proportional to the force @vel_x += -(@window.mouse_x-$window_width/2+$camera_x - @x) * @release_force @vel_y += -(@window.mouse_y-$window_height/2+$camera_y - @y) * @release_force else ## The ball gets pushed by a force asif the mouse was 300 pixels away. @vel_x += -(Gosu.offset_x(dir, 300)) * @release_force @vel_y += -(Gosu.offset_y(dir, 300)) * @release_force end @pulled = false end end |
#reset ⇒ Object
#
reset (reset tag)
#
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 77 def reset # ======================================================================= # # === @the_cue_ball_was_hit_n_times # # How many times the cue ball was hit via the stick. # ======================================================================= # @the_cue_ball_was_hit_n_times = 0 # ======================================================================= # # === @pulled # ======================================================================= # @pulled = false # ======================================================================= # # === @being_replaced # ======================================================================= # @being_replaced = false # ======================================================================= # # === @colliding # ======================================================================= # @colliding = false # ======================================================================= # # === @collision_point # # True if you should draw the collision point. # ======================================================================= # @collision_point = false # ======================================================================= # # === @resistance # ======================================================================= # @resistance = Math.sqrt(0.995) # ======================================================================= # # === @resistance_strong # ======================================================================= # @resistance_strong = Math.sqrt(0.96) end |
#update ⇒ Object
#
update
#
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 115 def update @colliding = false @collision_point = false if @being_replaced @x = @window.mouse_x-$window_width/2+$camera_x @y = @window.mouse_y-$window_height/2+$camera_y @placement_collision = false check_placement_collision else move # Call the move() method here. end # Direction from mouse to cue ball dir = point_direction( @window.mouse_x, @window.mouse_y, @x+$window_width/2-$camera_x, @y+$window_height/2-$camera_y ) if @pulled and @color == COLOUR_FOR_THE_CUE_BALL @window.check_path_collision( @x, @y, @x+::Gosu.offset_x(dir, 900), @y+::Gosu.offset_y(dir, 900) ) # p $path_blockers end end |
#vel_x? ⇒ Boolean Also known as: vel_x
#
vel_x?
#
679 680 681 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 679 def vel_x? @vel_x end |
#vel_y? ⇒ Boolean Also known as: vel_y
#
vel_y?
#
600 601 602 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 600 def vel_y? @vel_y end |
#x? ⇒ Boolean Also known as: x
#
x?
#
446 447 448 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 446 def x? @x end |
#y? ⇒ Boolean Also known as: y
#
y?
#
490 491 492 |
# File 'lib/games_paradise/gui/gosu/billiard/ball.rb', line 490 def y? @y end |