From aa8d3ac4bdccf522b8082a7fa7d595be2bd54b7d Mon Sep 17 00:00:00 2001 From: Bartosz Podrygajlo Date: Wed, 4 Aug 2021 11:19:06 +0200 Subject: Samples: Use to_radians for deg-to-rad conversion Replace the local implementation with the build-in Numeric.to_radians --- .../08_bouncing_on_collision/app/block.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb b/samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb index 9254ee4..fa2f3a5 100644 --- a/samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb +++ b/samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb @@ -1,5 +1,3 @@ -DEGREES_TO_RADIANS = Math::PI / 180 - class Block def initialize(x, y, block_size, rotation) @x = x @@ -10,15 +8,15 @@ class Block #The repel velocity? @velocity = {x: 2, y: 0} - horizontal_offset = (3 * block_size) * Math.cos(rotation * DEGREES_TO_RADIANS) - vertical_offset = block_size * Math.sin(rotation * DEGREES_TO_RADIANS) + horizontal_offset = (3 * block_size) * Math.cos(rotation.to_radians) + vertical_offset = block_size * Math.sin(rotation.to_radians) if rotation >= 0 theta = 90 - rotation #The line doesn't visually line up exactly with the edge of the sprite, so artificially move it a bit modifier = 5 - x_offset = modifier * Math.cos(theta * DEGREES_TO_RADIANS) - y_offset = modifier * Math.sin(theta * DEGREES_TO_RADIANS) + x_offset = modifier * Math.cos(theta.to_radians) + y_offset = modifier * Math.sin(theta.to_radians) @x1 = @x - x_offset @y1 = @y + y_offset @x2 = @x1 + horizontal_offset @@ -27,8 +25,8 @@ class Block @imaginary_line = [ @x1, @y1, @x2, @y2 ] else theta = 90 + rotation - x_offset = @block_size * Math.cos(theta * DEGREES_TO_RADIANS) - y_offset = @block_size * Math.sin(theta * DEGREES_TO_RADIANS) + x_offset = @block_size * Math.cos(theta.to_radians) + y_offset = @block_size * Math.sin(theta.to_radians) @x1 = @x + x_offset @y1 = @y + y_offset + 19 @x2 = @x1 + horizontal_offset @@ -105,8 +103,8 @@ class Block slope = (@y2 - @y1) / (@x2 - @x1) #Create a unit vector and tilt it (@rotation) number of degrees - x = -Math.cos(@rotation * DEGREES_TO_RADIANS) - y = Math.sin(@rotation * DEGREES_TO_RADIANS) + x = -Math.cos(@rotation.to_radians) + y = Math.sin(@rotation.to_radians) #Find the vector that is perpendicular to the slope perpVect = { x: x, y: y } @@ -120,7 +118,7 @@ class Block velocityMag = (args.state.ball.velocity.x**2 + args.state.ball.velocity.y**2)**0.5 # the current velocity magnitude of the ball theta_ball = Math.atan2(args.state.ball.velocity.y, args.state.ball.velocity.x) #the angle of the ball's velocity - theta_repel = (180 * DEGREES_TO_RADIANS) - theta_ball + (@rotation * DEGREES_TO_RADIANS) + theta_repel = (180.to_radians) - theta_ball + (@rotation.to_radians) fbx = velocityMag * Math.cos(theta_ball) #the x component of the ball's velocity fby = velocityMag * Math.sin(theta_ball) #the y component of the ball's velocity @@ -146,7 +144,7 @@ class Block end #Add the sine component of gravity back in (X component) - gravity_x = 4 * Math.sin(@rotation * DEGREES_TO_RADIANS) + gravity_x = 4 * Math.sin(@rotation.to_radians) xnew += gravity_x args.state.ball.velocity.x = -xnew -- cgit v1.2.3