Skip to content

Instantly share code, notes, and snippets.

@mieki256 mieki256/anglemove.rb
Last active Mar 14, 2016

Embed
What would you like to do?
イージングを利用して角度を滑らかに変化させられるかDXRubyを使って実験
#!ruby -Ku
# -*- mode: ruby; coding: utf-8 -*-
# Last updated: <2016/03/14 19:43:16 +0900>
#
# 角度を滑らかに変化させられるかテスト
require 'dxruby'
# quadratic ease in out
# @param t [Number] 時間(0.0 - 1.0)
# @param b [Number] 基準値
# @param c [Number] 変化量
# @param d [Number] 1.0
# @return [Number] 結果
def ease_in_out_quad(t, b, c, d)
t /= (d / 2.0)
return (c / 2.0 * t * t + b) if t < 1.0
t -= 1.0
return -c / 2.0 * (t * ( t -2.0) - 1) + b
end
def deg2rad(ang)
return ang * Math::PI / 180.0
end
srand(0)
t = 0
v_begin = 0
v_duration = 1.0
v_change = rand(60) - 30
t_spd = rand(0.01..0.05)
Window.loop do
break if Input.keyPush?(K_ESCAPE)
# 現在の角度を取得
ang = ease_in_out_quad(t, v_begin, v_change, v_duration)
# 線で描画
cx = Window.width / 2
cy = Window.height / 2
px = 200 * Math.cos(deg2rad(ang)) + cx
py = 200 * Math.sin(deg2rad(ang)) + cy
Window.drawLine(cx, cy, px, py, C_WHITE)
# 時間を進める
t += t_spd
if t >= 1.0
# 目標値を超えたら変化量を再設定
t = 0
v_begin += v_change
v_change = rand(60) - 30
t_spd = rand(0.01..0.05)
end
end
@mieki256

This comment has been minimized.

Copy link
Owner Author

commented Mar 14, 2016

Screenshot

anglemove_rb_ss1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.