draw skateboard

This commit is contained in:
Simon From Jakobsen 2026-03-30 15:52:23 +02:00 committed by Mikkel Troels Kongsted
parent eb32577ae0
commit ad332fb309
3 changed files with 81 additions and 16 deletions

View File

@ -72,6 +72,14 @@ impl Mul<f64> for V3 {
} }
} }
impl Mul<Self> for V3 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2)
}
}
impl Add<&Self> for V3 { impl Add<&Self> for V3 {
type Output = Self; type Output = Self;

View File

@ -1,6 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::{ use std::{
f64::consts::PI,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
time::Duration, time::Duration,
}; };
@ -58,7 +59,7 @@ impl<R: Renderer> engine::Game<R> for Game {
fn render(&mut self, r: &mut R) { fn render(&mut self, r: &mut R) {
let mut scene = Scene::new(); let mut scene = Scene::new();
for object in &mut self.objects { for object in &self.objects {
object.render(&mut scene); object.render(&mut scene);
} }
scene.render(r, V3(0.0, 0.0, -1.0)); scene.render(r, V3(0.0, 0.0, -1.0));
@ -73,19 +74,63 @@ struct Object {
} }
enum ObjectKind { enum ObjectKind {
Player { pos: V3, vel: V3 }, SkateBoard { pos: V3, vel: V3, rot: V3 },
Obstacle { pos: V3, vel: V3 }, Obstacle { pos: V3, vel: V3 },
Ground { pos: V3, vel_z: f64 }, Ground { pos: V3, vel_z: f64 },
} }
struct MultiShapeShape {
shape: Shape,
offset: V3,
}
struct MultiShape {
shapes: Vec<MultiShapeShape>,
}
impl MultiShape {
pub fn new(shapes: Vec<MultiShapeShape>) -> Self {
Self { shapes }
}
pub fn rotate(mut self, rot: V3) -> Self {
for shape in &mut self.shapes {
shape.shape = shape
.shape
.translate(shape.offset)
.rotate(rot)
.translate(shape.offset * -1.0);
}
self
}
pub fn translate(mut self, offset: V3) -> Self {
for shape in &mut self.shapes {
shape.shape = shape.shape.translate(offset);
}
self
}
pub fn draw(self, pos: V3, scene: &mut Scene, outline_color: Color, fill_color: Color) {
for shape in self.shapes {
scene.draw_shape(
pos,
&shape.shape.translate(shape.offset),
outline_color,
fill_color,
);
}
}
}
impl Object { impl Object {
fn update(&mut self, delta_time: Duration) { fn update(&mut self, delta_time: Duration) {
match &mut self.kind { match &mut self.kind {
ObjectKind::Player { pos, vel } => { ObjectKind::SkateBoard { pos, vel, rot } => {
*pos += *vel * delta_time.as_secs_f64(); *pos += *vel * delta_time.as_secs_f64();
if pos.0 >= 2.1 { if pos.0 >= 2.1 {
pos.0 = -2.0; pos.0 = -2.0;
} }
// rot.0 += delta_time.as_secs_f64() * PI * 1.0;
rot.1 += delta_time.as_secs_f64() * PI * 1.0;
// rot.2 += delta_time.as_secs_f64() * PI * 2.0;
} }
ObjectKind::Obstacle { pos, vel } => { ObjectKind::Obstacle { pos, vel } => {
*pos += *vel * delta_time.as_secs_f64(); *pos += *vel * delta_time.as_secs_f64();
@ -96,19 +141,30 @@ impl Object {
} }
} }
fn render(&mut self, scene: &mut Scene) { fn render(&self, scene: &mut Scene) {
match &mut self.kind { match self.kind {
ObjectKind::Player { pos, .. } => { ObjectKind::SkateBoard { pos, rot, .. } => {
scene.draw_shape( let board = MultiShape::new(vec![
*pos, MultiShapeShape {
&Shape::new_cube(V3(0.2, 0.2, 0.2)), shape: Shape::new_cube(V3(0.2, 0.01, 0.05)),
Color::Green, offset: V3(0.0, 0.0, 0.0),
Color::Black, },
); MultiShapeShape {
shape: Shape::new_cube(V3(0.02, 0.02, 0.05)),
offset: V3(0.04, -0.02, 0.0),
},
MultiShapeShape {
shape: Shape::new_cube(V3(0.02, 0.02, 0.05)),
offset: V3(0.14, -0.02, 0.0),
},
])
.translate(V3(-0.1, -0.005, -0.0025))
.rotate(rot);
board.draw(pos, scene, Color::Green, Color::Black);
} }
ObjectKind::Obstacle { pos, .. } => { ObjectKind::Obstacle { pos, .. } => {
scene.draw_shape( scene.draw_shape(
*pos, pos,
&Shape::new_cube(V3(0.2, 0.1, 0.2)), &Shape::new_cube(V3(0.2, 0.1, 0.2)),
Color::Green, Color::Green,
Color::Black, Color::Black,
@ -135,13 +191,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let event_queue = Arc::new(Mutex::new(EventQueue::new())); let event_queue = Arc::new(Mutex::new(EventQueue::new()));
let mut game = Game::new(event_queue.clone()); let mut game = Game::new(event_queue.clone());
std::thread::spawn(move || { std::thread::spawn(move || {
let server = Server::bind("10.133.51.127:42069"); let server = Server::bind("10.133.51.127:5000");
server.start(event_queue); server.start(event_queue);
}); });
let mut objects: Vec<ObjectKind> = Vec::new(); let mut objects: Vec<ObjectKind> = Vec::new();
objects.push(ObjectKind::Player { objects.push(ObjectKind::SkateBoard {
pos: V3(-0.3, -0.25, 0.0), pos: V3(0.0, -0.1, 0.0),
vel: V3(0.0, 0.0, 0.0), vel: V3(0.0, 0.0, 0.0),
rot: V3(0.0, 0.0, 0.0),
}); });
objects.push(ObjectKind::Ground { objects.push(ObjectKind::Ground {
pos: V3(0.0, -0.3, -0.5), pos: V3(0.0, -0.3, -0.5),

View File