Skip to content

Instantly share code, notes, and snippets.

@jsimmons
Created April 13, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsimmons/88bd1dd8d237baf48329fcd04b334631 to your computer and use it in GitHub Desktop.
Save jsimmons/88bd1dd8d237baf48329fcd04b334631 to your computer and use it in GitHub Desktop.
type PartType = u32;
trait Part {
fn GetPartType() -> PartType;
}
//#[Part("trns", width(4), chunk(256), align(64))]
struct PositionPart {
position_x: f32,
position_y: f32,
position_z: f32,
}
//#[Part("ornt", width(4), chunk(256), align(64))]
struct OrientationPart {
orientation_b: f32,
orientation_c: f32,
orientation_d: f32,
orientation_a: f32
}
// becomes
impl Part for PositionPart {
fn GetPartType() -> PartType {
0x74726e73 // "trns"
}
}
impl Part for OrientationPart {
fn GetPartType() -> PartType {
0x6f726e74 // "ornt"
}
}
#[repr(align(64))]
struct PositionPart4 {
position_x: [f32; 4],
position_y: [f32; 4],
position_z: [f32; 4],
}
#[repr(align(64))]
struct OrientationPart4 {
orientation_b: [f32; 4],
orientation_c: [f32; 4],
orientation_d: [f32; 4],
orientation_a: [f32; 4]
}
struct PositionPartChunk {
parts: [PositionPart4; 256]
}
struct OrientationPartChunk {
parts: [OrientationPart4; 256]
}
// For a bunch of entities that have position and orientation they would have
// this archtype
pub struct Archtype {
parts: Vec<PartType>, // [0] = PositionPart::GetPartType() -> "trns", [1] = OrientationPart::GetPartType() -> "ornt"
part_0: Vec<PositionPartChunk>,
part_1: Vec<OrientationPartChunk>,
}
struct UpdatePositionsTask {
speed: f32,
}
impl UpdatePositionsTask {
fn Process(&self, count: u32, orientations: &OrientationPartChunk, positions: &mut PositionPartChunk) {
for i in 0..count {
let x = positions.parts[(i >> 2) as usize].position_x[(i & 3) as usize];
let y = positions.parts[(i >> 2) as usize].position_y[(i & 3) as usize];
let z = positions.parts[(i >> 2) as usize].position_z[(i & 3) as usize];
let b = orientations.parts[(i >> 2) as usize].orientation_b[(i & 3) as usize];
let c = orientations.parts[(i >> 2) as usize].orientation_c[(i & 3) as usize];
let d = orientations.parts[(i >> 2) as usize].orientation_d[(i & 3) as usize];
let a = orientations.parts[(i >> 2) as usize].orientation_a[(i & 3) as usize];
let dir_x: f32 = self.speed;
let dir_y: f32 = 0.0;
let dir_z: f32 = 0.0;
let t_x = 2.0 * (c * z - y * d);
let t_y = 2.0 * (d * x - z * b);
let t_z = 2.0 * (b * y - x * c);
let dir_x = dir_x + a * t_x + (c * t_z - t_y * d);
let dir_y = dir_y + a * t_y + (d * t_x - t_z * b);
let dir_z = dir_z + a * t_z + (b * t_y - t_x * c);
positions.parts[(i >> 2) as usize].position_x[(i & 3) as usize] = x + dir_x;
positions.parts[(i >> 2) as usize].position_y[(i & 3) as usize] = y + dir_y;
positions.parts[(i >> 2) as usize].position_z[(i & 3) as usize] = z + dir_z;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment