summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBartosz Podrygajlo <[email protected]>2021-08-04 11:19:06 +0200
committerAmir Rajan <[email protected]>2021-09-06 05:39:58 -0500
commitaa8d3ac4bdccf522b8082a7fa7d595be2bd54b7d (patch)
tree2fc93acc110a40b7e97e3dd84a2e3096203968c8
parent26f20690be4da8f5091c720b1a25e32742c8c5fa (diff)
downloaddragonruby-game-toolkit-contrib-aa8d3ac4bdccf522b8082a7fa7d595be2bd54b7d.tar.gz
dragonruby-game-toolkit-contrib-aa8d3ac4bdccf522b8082a7fa7d595be2bd54b7d.zip
Samples: Use to_radians for deg-to-rad conversion
Replace the local implementation with the build-in Numeric.to_radians
-rw-r--r--samples/04_physics_and_collisions/08_bouncing_on_collision/app/block.rb22
1 files 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