Skip to content

Instantly share code, notes, and snippets.

@xenobrain
xenobrain / boingy2.rb
Last active April 18, 2023 10:13
Boingy2
RAD2DEG = 180.0 / Math::PI
TIME_STEP = 0.2 / 60 # delta time isn't required in DragonRuby but it really handy for tuning and debugging physics
COLLISION_BIAS = 0.05 # adds some energy into the collision to get objects to separate. tune this in steps of 0.01
COLLISION_SLOP = 0.1 # amount shapes are allowed to overlap without triggering correction. helps avoid position jitter
COLLISION_ITERATIONS = 10 # how many times to run the solver. a good range is between 5 and 15
def tick args
args.gtk.reset if args.inputs.keyboard.r
# Create some circles
@xenobrain
xenobrain / simple_boingy.rb
Last active April 14, 2023 13:08
Simple Bouncing
def tick args
# Create some circles
args.state.circles ||= 10.map_with_ys 2 do |x, y|
radius = 32
diameter = radius * 2
# for static objects use mass = Float::INFINITY
mass = Math::PI * radius * radius
path = 'sprites/circle/blue.png'
offset_x = (1280 - ((diameter + radius) * 10)) / 2
@xenobrain
xenobrain / circles.rb
Last active October 12, 2023 14:42
circles for dragonruby
module GTK
class Runtime
def draw_circle c
radius = c.radius.to_i || 0
xc = c.x.to_i + radius
yc = c.y.to_i + radius
t = c.thickness || 1
r = c.r || 0
g = c.g || 0
b = c.b || 0
@xenobrain
xenobrain / sat.rb
Last active October 24, 2024 18:41
SAT
def create_vertices rect
x = rect.x; y = rect.y
w = rect.w; h = rect.h
cx = x + w * 0.5; cy = y + h * 0.5
sin = Math.sin (rect.angle || 0.0).to_radians; cos = Math.cos (rect.angle || 0.0).to_radians
[
[(x - cx) * cos - (y + h - cy) * sin + cx, (x - cx) * sin + (y + h - cy) * cos + cy], # Top Left
[(x + w - cx) * cos - (y + h - cy) * sin + cx, (x + w - cx) * sin + (y + h - cy) * cos + cy], # Top Right
[(x + w - cx) * cos - (y - cy) * sin + cx, (x + w - cx) * sin + (y - cy) * cos + cy], # Bottom Right
[(x - cx) * cos - (y - cy) * sin + cx, (x - cx) * sin + (y - cy) * cos + cy] # Bottom Left
DEG2RAD=Math::PI/180
RAD2DEG=180/Math::PI
class Vector2
attr_accessor(:x,:y)
def initialize(x=0.0,y=0.0) @x=x;@y=y; end
def serialize; {x:@x,y:@y} end
def inspect; serialize.to_s end
def to_a; serialize.values end
def to_s; serialize.to_s end
SCALE = 175.0
LOD = 0.005
MAX_Z_FAR = 600.0
MIN_Z_FAR = 175.0
MIN_SCREEN_W = 70
MIN_SCREEN_H = 60
MAX_SCREEN_W = 360
MAX_SCREEN_H = 120
DRS_FRAME_WINDOW = 10
Z_FAR_ADJUST_SPEED = 1
@xenobrain
xenobrain / hex_game.rb
Last active January 5, 2023 16:07
starting point for a hex based game
include MatrixFunctions
# Math constants
K_INV_6 = 1.0 / 6.0
K_2_OVER_3 = 2.0 / 3.0
K_2_PI = Math::PI * 2.0
K_SQRT_3 = Math.sqrt 3.0
K_NEG_1_OVER_3 = -1.0 / 3.0
K_DEG2RAD = Math::PI / 180.0
K_RAD2DEG = 360.0 / (2.0 * Math::PI)
args.gtk.method(:write_file).parameters.map(&:last).map { |p| p.to_s }
@xenobrain
xenobrain / vec2_monkeypatch.rb
Created November 16, 2022 23:22
DragonRuby MatrixFunctions monkey patch
module Vec2Extensions
def + other
return super other if other.is_a? Vec2
vec2 x + other, y + other if other.is_a? Numeric
end
def - other
return vec2 x - other.x, y - other.y if other.is_a? Vec2
vec2 x - other, y - other if other.is_a? Numeric
end
@xenobrain
xenobrain / CMakeLists.txt
Last active January 30, 2022 06:31
CMakeRC + stb_image + SDL_Renderer
cmake_minimum_required(VERSION 3.21)
include(CMakeRC.cmake)
project(test)
file(GLOB_RECURSE ENGINE_SOURCES "${CMAKE_SOURCE_DIR}/engine/*.cpp")
file(GLOB_RECURSE GAME_SOURCES "${CMAKE_SOURCE_DIR}/game/*.cpp")
# Resource compiler
file(GLOB_RECURSE ASSETS "${CMAKE_SOURCE_DIR}/assets/*.*")
cmrc_add_resource_library(game-resources ALIAS game::resources NAMESPACE resources ${ASSETS})