From 161595b0413aa7221299781116a72c1a115c79c3 Mon Sep 17 00:00:00 2001 From: Larpon Date: Tue, 27 Dec 2022 14:19:08 +0100 Subject: [PATCH] examples: use `math.vec` in examples/sokol/particles (#16780) --- .../particles/modules/particle/particle.v | 16 ++-- .../sokol/particles/modules/particle/system.v | 10 +-- .../particles/modules/particle/vec2/v.mod | 0 .../particles/modules/particle/vec2/vec2.v | 89 ------------------- 4 files changed, 13 insertions(+), 102 deletions(-) delete mode 100644 examples/sokol/particles/modules/particle/vec2/v.mod delete mode 100644 examples/sokol/particles/modules/particle/vec2/vec2.v diff --git a/examples/sokol/particles/modules/particle/particle.v b/examples/sokol/particles/modules/particle/particle.v index 5025b13087..307b80f0c5 100644 --- a/examples/sokol/particles/modules/particle/particle.v +++ b/examples/sokol/particles/modules/particle/particle.v @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT license file distributed with this software package module particle -import particle.vec2 +import math.vec import sokol.sgl const ( @@ -11,11 +11,11 @@ const ( ) // * Module public -pub fn new(location vec2.Vec2) &Particle { +pub fn new(location vec.Vec2[f64]) &Particle { p := &Particle{ location: location - velocity: vec2.Vec2{0, 0} - acceleration: vec2.Vec2{0, 0} + velocity: vec.Vec2[f64]{0, 0} + acceleration: vec.Vec2[f64]{0, 0} color: particle.default_v_color life_time: particle.default_life_time life_time_init: particle.default_life_time @@ -30,9 +30,9 @@ fn remap(v f64, min f64, max f64, new_min f64, new_max f64) f64 { // Particle pub struct Particle { mut: - location vec2.Vec2 - velocity vec2.Vec2 - acceleration vec2.Vec2 + location vec.Vec2[f64] + velocity vec.Vec2[f64] + acceleration vec.Vec2[f64] color Color life_time f64 life_time_init f64 @@ -40,7 +40,7 @@ mut: pub fn (mut p Particle) update(dt f64) { mut acc := p.acceleration - acc.multiply_f64(dt) + acc.multiply_scalar(dt) p.velocity = p.velocity.add(acc) p.location = p.location.add(p.velocity) lt := p.life_time - (1000 * dt) diff --git a/examples/sokol/particles/modules/particle/system.v b/examples/sokol/particles/modules/particle/system.v index dc19ddfc2e..dba4081976 100644 --- a/examples/sokol/particles/modules/particle/system.v +++ b/examples/sokol/particles/modules/particle/system.v @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT license file distributed with this software package module particle -import particle.vec2 +import math.vec import rand import sokol.sgl @@ -22,7 +22,7 @@ pub fn (mut s System) init(sc SystemConfig) { unsafe { s.pool.flags.set(.noslices | .noshrink) } unsafe { s.bin.flags.set(.noslices | .noshrink) } for i := 0; i < sc.pool; i++ { - p := new(vec2.Vec2{f32(s.width) * 0.5, f32(s.height) * 0.5}) + p := new(vec.Vec2[f64]{f32(s.width) * 0.5, f32(s.height) * 0.5}) s.bin << p } } @@ -69,16 +69,16 @@ pub fn (mut s System) reset() { pub fn (mut s System) explode(x f32, y f32) { mut reserve := 500 - center := vec2.Vec2{x, y} + center := vec.Vec2[f64]{x, y} mut p := &Particle(0) mut moved := 0 for i := 0; i < s.bin.len && reserve > 0; i++ { p = s.bin[i] p.reset() p.location.from(center) - p.acceleration = vec2.Vec2{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, + p.acceleration = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or { -0.5 }} - p.velocity = vec2.Vec2{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, + p.velocity = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or { -0.5 }} p.life_time = rand.f64_in_range(500, 2000) or { 500 } s.pool << p diff --git a/examples/sokol/particles/modules/particle/vec2/v.mod b/examples/sokol/particles/modules/particle/vec2/v.mod deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/examples/sokol/particles/modules/particle/vec2/vec2.v b/examples/sokol/particles/modules/particle/vec2/vec2.v deleted file mode 100644 index ec41485662..0000000000 --- a/examples/sokol/particles/modules/particle/vec2/vec2.v +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. -// Use of this source code is governed by an MIT license file distributed with this software package -module vec2 - -pub struct Vec2 { -pub mut: - x f64 - y f64 -} - -pub fn (mut v Vec2) zero() { - v.x = 0.0 - v.y = 0.0 -} - -pub fn (mut v Vec2) from(src Vec2) { - v.x = src.x - v.y = src.y -} - -// * Addition -// + operator overload. Adds two vectors -pub fn (v1 Vec2) + (v2 Vec2) Vec2 { - return Vec2{v1.x + v2.x, v1.y + v2.y} -} - -pub fn (v Vec2) add(vector Vec2) Vec2 { - return Vec2{v.x + vector.x, v.y + vector.y} -} - -pub fn (v Vec2) add_f64(scalar f64) Vec2 { - return Vec2{v.x + scalar, v.y + scalar} -} - -pub fn (mut v Vec2) plus(vector Vec2) { - v.x += vector.x - v.y += vector.y -} - -pub fn (mut v Vec2) plus_f64(scalar f64) { - v.x += scalar - v.y += scalar -} - -// * Subtraction -pub fn (v1 Vec2) - (v2 Vec2) Vec2 { - return Vec2{v1.x - v2.x, v1.y - v2.y} -} - -pub fn (v Vec2) sub(vector Vec2) Vec2 { - return Vec2{v.x - vector.x, v.y - vector.y} -} - -pub fn (v Vec2) sub_f64(scalar f64) Vec2 { - return Vec2{v.x - scalar, v.y - scalar} -} - -pub fn (mut v Vec2) subtract(vector Vec2) { - v.x -= vector.x - v.y -= vector.y -} - -pub fn (mut v Vec2) subtract_f64(scalar f64) { - v.x -= scalar - v.y -= scalar -} - -// * Multiplication -pub fn (v1 Vec2) * (v2 Vec2) Vec2 { - return Vec2{v1.x * v2.x, v1.y * v2.y} -} - -pub fn (v Vec2) mul(vector Vec2) Vec2 { - return Vec2{v.x * vector.x, v.y * vector.y} -} - -pub fn (v Vec2) mul_f64(scalar f64) Vec2 { - return Vec2{v.x * scalar, v.y * scalar} -} - -pub fn (mut v Vec2) multiply(vector Vec2) { - v.x *= vector.x - v.y *= vector.y -} - -pub fn (mut v Vec2) multiply_f64(scalar f64) { - v.x *= scalar - v.y *= scalar -}