1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: use math.vec in examples/sokol/particles (#16780)

This commit is contained in:
Larpon 2022-12-27 14:19:08 +01:00 committed by GitHub
parent c5c7b3a054
commit 161595b041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 102 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
}