physicist 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd83597bea0363b5b29832d055ca424a40739170
4
+ data.tar.gz: 9d82f12a68cc2bc631a2fc336918643d7041c2e9
5
+ SHA512:
6
+ metadata.gz: f8e64f7f5f427b8d12e21675659389bf6cae97806d705ad7cd2d7c251360e0bdae8dd922b210b40344b771c9c1b5df3597f7d19af282afe5705c9ecf5978ad37
7
+ data.tar.gz: 79b9502573ac874cece9b0ac486ea4e2a617ebe6fc58b01ecd0915d21903feb30d225759199a2fd1b9729603476de9086e9a26e64ae8c1117471df289263fba1
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ /.bundle
2
+ /.yardoc/
3
+ /Gemfile.lock
4
+ /doc/
5
+ /pkg/
6
+ /vendor/cache/*.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "physicist Documentation" --protected
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2016-05-12
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'kramdown'
7
+ gem 'pry'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Joseph Weissman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # physicist
2
+
3
+ * [Homepage](https://rubygems.org/gems/physicist)
4
+ * [Documentation](http://rubydoc.info/gems/physicist/frames)
5
+ * [Email](mailto:jweissman1986 at gmail.com)
6
+
7
+ [![Code Climate GPA](https://codeclimate.com/github//physicist/badges/gpa.svg)](https://codeclimate.com/github//physicist)
8
+
9
+ ## Description
10
+
11
+ TODO: Description
12
+
13
+ ## Features
14
+
15
+ ## Examples
16
+
17
+ require 'physicist'
18
+
19
+ ## Requirements
20
+
21
+ ## Install
22
+
23
+ $ gem install physicist
24
+
25
+ ## Synopsis
26
+
27
+ $ physicist
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2016 Joseph Weissman
32
+
33
+ See {file:LICENSE.txt} for details.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler/setup'
7
+ rescue LoadError => e
8
+ abort e.message
9
+ end
10
+
11
+ require 'rake'
12
+
13
+
14
+ require 'rubygems/tasks'
15
+ Gem::Tasks.new
16
+
17
+ require 'rspec/core/rake_task'
18
+ RSpec::Core::RakeTask.new
19
+
20
+ task :test => :spec
21
+ task :default => :spec
22
+
23
+ require 'yard'
24
+ YARD::Rake::YardocTask.new
25
+ task :doc => :yard
26
+
27
+ require 'cucumber/rake/task'
28
+
29
+ Cucumber::Rake::Task.new do |t|
30
+ t.cucumber_opts = %w[--format pretty]
31
+ end
data/bin/physicist ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root = File.expand_path(File.join(File.dirname(__FILE__),'..'))
4
+ if File.directory?(File.join(root,'.git'))
5
+ Dir.chdir(root) do
6
+ begin
7
+ require 'bundler/setup'
8
+ rescue LoadError => e
9
+ warn e.message
10
+ warn "Run `gem install bundler` to install Bundler"
11
+ exit -1
12
+ end
13
+ end
14
+ end
data/features/.gitkeep ADDED
File without changes
@@ -0,0 +1 @@
1
+ Feature: Blah blah blah
File without changes
@@ -0,0 +1 @@
1
+ @wip
data/gemspec.yml ADDED
@@ -0,0 +1,16 @@
1
+ name: physicist
2
+ summary: "a tiny 2-d physics engine for ruby"
3
+ description: "a little physics library"
4
+ license: MIT
5
+ authors: Joseph Weissman
6
+ email: jweissman1986@gmail.com
7
+ homepage: https://rubygems.org/gems/physicist
8
+
9
+ development_dependencies:
10
+ bundler: ~> 1.10
11
+ codeclimate-test-reporter: ~> 0.1
12
+ cucumber: ~> 0.10.2
13
+ rake: ~> 10.0
14
+ rspec: ~> 3.0
15
+ rubygems-tasks: ~> 0.2
16
+ yard: ~> 0.8
@@ -0,0 +1 @@
1
+ # a tiny dedalus app for visually checking physicist flow?
@@ -0,0 +1,4 @@
1
+ module Physicist
2
+ # physicist version
3
+ VERSION = "0.1.0"
4
+ end
data/lib/physicist.rb ADDED
@@ -0,0 +1,120 @@
1
+ require 'physicist/version'
2
+
3
+ module Physicist
4
+ class SimpleBody
5
+ attr_reader :position, :dimensions
6
+
7
+ def initialize(position:,dimensions:)
8
+ @position = position
9
+ @dimensions = dimensions
10
+ end
11
+ end
12
+
13
+ class Body
14
+ attr_reader :position, :velocity, :t0, :dimensions
15
+
16
+ def initialize(position:, velocity:, t0:, dimensions:)
17
+ @position = position
18
+ @velocity = velocity
19
+ @dimensions = dimensions
20
+ @t0 = t0
21
+ end
22
+
23
+ def width
24
+ dimensions[0]
25
+ end
26
+
27
+ def height
28
+ dimensions[1]
29
+ end
30
+
31
+ def at(t, obstacles:[])
32
+ x0,y0 = *position
33
+ vx0,vy0 = *velocity
34
+
35
+ dt = t - t0
36
+
37
+ vx = vx0 # - dry friction ?
38
+ vy = vy0 + (gravity * dt)
39
+
40
+ next_y_obstacle = next_obstacle_on_y_axis(vy,t,obstacles:obstacles)
41
+
42
+ if next_y_obstacle
43
+ distance_to_next_y_obstacle =
44
+ if vy > 0
45
+ ((y0+height) - next_y_obstacle.position[1]).abs
46
+ else
47
+ (y0 - next_y_obstacle.position[1]).abs
48
+ end
49
+ # if vy > 0
50
+ # distance_to_next_y_obstacle -= height
51
+ # end
52
+
53
+ distance_travelled_in_y_axis_if_no_obstacles = (vy * dt)
54
+
55
+ # require 'pry'
56
+ # binding.pry
57
+
58
+ if distance_travelled_in_y_axis_if_no_obstacles < distance_to_next_y_obstacle
59
+ # the no-obstacles within relevant distance case
60
+ # # TODO handle other coordinate (x)
61
+ x = x0 + (vx * dt)
62
+ y = y0 + (vy * dt)
63
+
64
+ else
65
+ # TODO handle other coordinate here too...
66
+ x = x0 + (vx * dt)
67
+ y = y0 + distance_to_next_y_obstacle # - height
68
+ vy = 0
69
+ end
70
+ else
71
+ x = x0 + (vx * dt)
72
+ y = y0 + (vy * dt)
73
+ end
74
+
75
+ Body.new(
76
+ position: [x,y],
77
+ velocity: [vx,vy],
78
+ dimensions: dimensions,
79
+ t0: t
80
+ )
81
+ end
82
+
83
+ def next_obstacle_on_y_axis(vy,t,obstacles:)
84
+ x0,y0 = *position
85
+
86
+ obstacles_along_axis = obstacles.select do |obstacle|
87
+ ox,_ = *obstacle.position
88
+ ow,_ = *obstacle.dimensions
89
+ ox <= x0 + width && x0 <= ox + ow
90
+ end
91
+
92
+ obstacles_in_direction_of_movement = if vy > 0
93
+ obstacles_along_axis.select do |obstacle|
94
+ _,oy = *obstacle.position
95
+ # _,oh = *obstacle.dimensions
96
+
97
+ oy >= y0 + height # && y0 < oy + oh
98
+ end
99
+ elsif vy < 0
100
+ obstacles_along_axis.select do |obstacle|
101
+ _,oy = *obstacle.position
102
+ _,oh = *obstacle.dimensions
103
+
104
+ oy + oh <= y0 # + height # && y0 > oy + oh
105
+ end
106
+ end
107
+
108
+
109
+ obstacles_in_direction_of_movement.min_by do |obstacle|
110
+ _,oy = *obstacle.position
111
+ # distance to me
112
+ (y0 - oy).abs
113
+ end
114
+ end
115
+
116
+ def gravity
117
+ 9.8
118
+ end
119
+ end
120
+ end
data/physicist.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gemspec = YAML.load_file('gemspec.yml')
7
+
8
+ gem.name = gemspec.fetch('name')
9
+ gem.version = gemspec.fetch('version') do
10
+ lib_dir = File.join(File.dirname(__FILE__),'lib')
11
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
12
+
13
+ require 'physicist/version'
14
+ Physicist::VERSION
15
+ end
16
+
17
+ gem.summary = gemspec['summary']
18
+ gem.description = gemspec['description']
19
+ gem.licenses = Array(gemspec['license'])
20
+ gem.authors = Array(gemspec['authors'])
21
+ gem.email = gemspec['email']
22
+ gem.homepage = gemspec['homepage']
23
+
24
+ glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
+
26
+ gem.files = `git ls-files`.split($/)
27
+ gem.files = glob[gemspec['files']] if gemspec['files']
28
+
29
+ gem.executables = gemspec.fetch('executables') do
30
+ glob['bin/*'].map { |path| File.basename(path) }
31
+ end
32
+ gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.'
33
+
34
+ gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb']
35
+ gem.test_files = glob[gemspec['test_files'] || '{test/{**/}*_test.rb']
36
+ gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}']
37
+
38
+ gem.require_paths = Array(gemspec.fetch('require_paths') {
39
+ %w[ext lib].select { |dir| File.directory?(dir) }
40
+ })
41
+
42
+ gem.requirements = Array(gemspec['requirements'])
43
+ gem.required_ruby_version = gemspec['required_ruby_version']
44
+ gem.required_rubygems_version = gemspec['required_rubygems_version']
45
+ gem.post_install_message = gemspec['post_install_message']
46
+
47
+ split = lambda { |string| string.split(/,\s*/) }
48
+
49
+ if gemspec['dependencies']
50
+ gemspec['dependencies'].each do |name,versions|
51
+ gem.add_dependency(name,split[versions])
52
+ end
53
+ end
54
+
55
+ if gemspec['development_dependencies']
56
+ gemspec['development_dependencies'].each do |name,versions|
57
+ gem.add_development_dependency(name,split[versions])
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require 'physicist'
3
+
4
+ describe Physicist do
5
+ it "should have a VERSION constant" do
6
+ expect(subject.const_get('VERSION')).to_not be_empty
7
+ end
8
+ end
9
+
10
+ describe Body do
11
+ subject(:body) do
12
+ Body.new(
13
+ position: position,
14
+ velocity: velocity,
15
+ dimensions: dimensions,
16
+ t0: t0
17
+ )
18
+ end
19
+
20
+ let(:x0) { body.position[0] }
21
+ let(:y0) { body.position[1] }
22
+ let(:vx0) { body.velocity[0] }
23
+ let(:vy0) { body.velocity[1] }
24
+ let(:width) { body.dimensions[0] }
25
+ let(:height) { body.dimensions[1] }
26
+
27
+ let(:gravity) { body.gravity }
28
+
29
+ context 'attributes' do
30
+ let(:position) { 'the_position' }
31
+ let(:velocity) { 'the_velocity' }
32
+ let(:dimensions) { 'the_dimensions' }
33
+ let(:t0) { 'the_time' }
34
+
35
+ it 'should have dimensions' do
36
+ expect(body.dimensions).to eq('the_dimensions')
37
+ end
38
+
39
+ it 'should have a position' do
40
+ expect(body.position).to eq('the_position')
41
+ end
42
+
43
+ it 'should have a velocity' do
44
+ expect(body.velocity).to eq('the_velocity')
45
+ end
46
+
47
+ it 'should have a start time' do
48
+ expect(body.t0).to eq('the_time')
49
+ end
50
+ end
51
+
52
+ context 'instance methods' do
53
+ let(:position) { [0,10] }
54
+ let(:velocity) { [0,0] }
55
+ let(:dimensions) { [2,2] }
56
+
57
+ let(:t0) { Time.local(1990) }
58
+
59
+ describe "#at" do
60
+ # step forward one second (time unit for velocities)
61
+ let(:t) { t0 + 1 }
62
+
63
+ it 'should return a body with updated t0 = t' do
64
+ expect(body.at(t).t0).to eq(t)
65
+ end
66
+
67
+ it 'should increment position by velocity' do
68
+ x,y = *body.at(t).position
69
+
70
+ aggregate_failures "increments both axes" do
71
+ expect(x).to eq(vx0+x0)
72
+ expect(y).to eq(vy0+y0+gravity)
73
+ end
74
+ end
75
+
76
+ it 'should apply gravity' do
77
+ _,vy = *body.at(t).velocity
78
+
79
+ expect(vy).to eq(vy0+gravity)
80
+ end
81
+
82
+ describe 'not pushing bodies through impediments' do
83
+ let(:body_at_t) do
84
+ body.at(t, obstacles: obstacles)
85
+ end
86
+
87
+
88
+ context "with an obstacle directly beneath the body" do
89
+ let(:obstacles) do
90
+ [
91
+ SimpleBody.new(position: [ x0, y0 + height ], dimensions: [ width, height ])
92
+ ]
93
+ end
94
+
95
+ it 'should stop vertical movement entirely' do
96
+ _,vy = *body_at_t.velocity
97
+ _,y = *body_at_t.position
98
+ expect(vy).to eq(0)
99
+ expect(y0).to eq(y)
100
+ end
101
+ end
102
+
103
+ context "with an obstacle 9.8 units beneath the body" do
104
+ let(:obstacles) do
105
+ [
106
+ SimpleBody.new(
107
+ position: [x0, y0 + height + 9.8],
108
+ dimensions: [width,height]
109
+ )
110
+ ]
111
+ end
112
+
113
+ it 'should stop vertical movement after 1s' do
114
+ _,vy = *body_at_t.velocity
115
+ _,y = *body_at_t.position
116
+ expect(y0 + 9.8).to eq(y)
117
+ expect(vy).to eq(0)
118
+ end
119
+ end
120
+
121
+ context "with no obstacles" do
122
+ let(:obstacles) { [] }
123
+
124
+ it 'should be in freefall' do
125
+ _,vy = *body_at_t.velocity
126
+ _,y = *body_at_t.position
127
+ expect(y0 + 9.8).to eq(y)
128
+ expect(vy).to eq(9.8)
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'physicist/version'
3
+
4
+ include Physicist
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: physicist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Weissman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cucumber
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubygems-tasks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description: a little physics library
112
+ email: jweissman1986@gmail.com
113
+ executables:
114
+ - physicist
115
+ extensions: []
116
+ extra_rdoc_files:
117
+ - ChangeLog.md
118
+ - LICENSE.txt
119
+ - README.md
120
+ files:
121
+ - ".document"
122
+ - ".gitignore"
123
+ - ".rspec"
124
+ - ".yardopts"
125
+ - ChangeLog.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/physicist
131
+ - features/.gitkeep
132
+ - features/physicist.feature
133
+ - features/step_definitions/.gitkeep
134
+ - features/step_definitions/physicist_steps.rb
135
+ - gemspec.yml
136
+ - lib/physicist.rb
137
+ - lib/physicist/laboratory.rb
138
+ - lib/physicist/version.rb
139
+ - physicist.gemspec
140
+ - spec/physicist_spec.rb
141
+ - spec/spec_helper.rb
142
+ homepage: https://rubygems.org/gems/physicist
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.4.5.1
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: a tiny 2-d physics engine for ruby
166
+ test_files: []