Skip to content

Commit ffc798f

Browse files
committed
make COM cylinder in Body configurable in rendering
1 parent 064b2f4 commit ffc798f

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

docs/src/examples/swing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Next, we create the full swing assembly
102102
body_left = BodyShape(m=0.1, r = [0, 0, w])
103103
body_right = BodyShape(m=0.1, r = [0, 0, -w])
104104
105-
body = Body(m=6, isroot=true, r_cm = [w/2, -w/2, w/2], vel_from_R=true)
105+
body = Body(m=6, isroot=true, r_cm = [w/2, -w/2, w/2], vel_from_R=true, cylinder_radius=0.01)
106106
107107
damper = Damper(d=0.5)
108108
end

ext/Render.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,8 @@ function render!(scene, ::typeof(Body), sys, sol, t)
242242
color = get_color(sys, sol, :purple)
243243
r_cm = get_fun(sol, collect(sys.r_cm))
244244
framefun = get_frame_fun(sol, sys.frame_a)
245-
radius = try
246-
sol(sol.t[1], idxs=sys.radius)
247-
catch
248-
0.05f0
249-
end
245+
radius = sol(sol.t[1], idxs=sys.radius) |> Float32
246+
cylinder_radius = sol(sol.t[1], idxs=sys.cylinder_radius) |> Float32
250247
thing = @lift begin # Sphere
251248
Ta = framefun($t)
252249
coords = (Ta*[r_cm($t); 1])[1:3] # TODO: make use of a proper transformation library instead of rolling own?
@@ -257,7 +254,7 @@ function render!(scene, ::typeof(Body), sys, sol, t)
257254

258255
iszero(r_cm(sol.t[1])) && (return true)
259256

260-
thing = @lift begin # Cylinder
257+
thing2 = @lift begin # Cylinder
261258
Ta = framefun($t)
262259

263260
r_cmt = r_cm($t) # r_cm is the center of the sphere in frame a
@@ -274,10 +271,9 @@ function render!(scene, ::typeof(Body), sys, sol, t)
274271
extremity = tip
275272
origin = point
276273

277-
radius = 0.05f0
278-
Makie.GeometryBasics.Cylinder(origin, extremity, radius)
274+
Makie.GeometryBasics.Cylinder(origin, extremity, cylinder_radius)
279275
end
280-
mesh!(scene, thing; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
276+
mesh!(scene, thing2; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
281277
true
282278
end
283279

src/components.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Representing a body with 3 translational and 3 rotational degrees-of-freedom.
220220
221221
# Rendering options
222222
- `radius`: Radius of the joint in animations
223+
- `cylinder_radius`: Radius of the cylinder from frame to COM in animations (only drawn if `r_cm` is non-zero). Defaults to `radius/2`
223224
- `color`: Color of the joint in animations, a vector of length 4 with values between [0, 1] providing RGBA values
224225
"""
225226
@component function Body(; name, m = 1, r_cm = [0, 0, 0],
@@ -238,6 +239,7 @@ Representing a body with 3 translational and 3 rotational degrees-of-freedom.
238239
v_0 = 0,
239240
w_a = 0,
240241
radius = 0.05,
242+
cylinder_radius = radius/2,
241243
air_resistance = 0.0,
242244
color = [1,0,0,1],
243245
quat=false,)
@@ -269,6 +271,9 @@ Representing a body with 3 translational and 3 rotational degrees-of-freedom.
269271
@parameters radius=radius [
270272
description = "Radius of the body in animations",
271273
]
274+
@parameters cylinder_radius=cylinder_radius [
275+
description = "Radius of the cylinder from frame to COM in animations",
276+
]
272277
@parameters color[1:4] = color [description = "Color of the body in animations (RGBA)"]
273278
# @parameters I[1:3, 1:3]=I [description="inertia tensor"]
274279

@@ -345,7 +350,7 @@ Representing a body with 3 translational and 3 rotational degrees-of-freedom.
345350
# pars = [m;r_cm;radius;I_11;I_22;I_33;I_21;I_31;I_32;color]
346351

347352
sys = ODESystem(eqs, t; name=:nothing, metadata = Dict(:isroot => isroot), systems = [frame_a])
348-
add_params(sys, [radius; color]; name)
353+
add_params(sys, [radius; cylinder_radius; color]; name)
349354
end
350355

351356

0 commit comments

Comments
 (0)