Draw boid as a triangle and rotate from angle

This commit is contained in:
Joseph Ferano 2024-09-28 20:25:45 +07:00
parent 04adcd4532
commit c2b5cb96f8

View File

@ -17,6 +17,9 @@ SCREEN_HEIGHT : i32 = 1000
BW := f32(SCREEN_WIDTH) BW := f32(SCREEN_WIDTH)
BH := f32(SCREEN_HEIGHT) BH := f32(SCREEN_HEIGHT)
BOID_W :f32 = 15
BOID_H :f32 = 30
Boid :: struct { Boid :: struct {
pos: Vec2, pos: Vec2,
angle: f32, angle: f32,
@ -53,6 +56,14 @@ init_state :: proc() -> ^GameState {
get_rand_angle :: proc(min: i32, max: i32) -> f32 { return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD } get_rand_angle :: proc(min: i32, max: i32) -> f32 { return f32(rl.GetRandomValue(min, max)) * rl.DEG2RAD }
get_boid_shape :: proc(boid: Boid, points: ^[4]Vec2) {
points[0] = rl.Vector2Rotate({ BOID_H*0.5, 0}, boid.angle) + boid.pos
points[1] = rl.Vector2Rotate({-BOID_H*0.5, -BOID_W*0.5}, boid.angle) + boid.pos
points[2] = rl.Vector2Rotate({-BOID_H*0.5*0.5, 0}, boid.angle) + boid.pos
points[3] = rl.Vector2Rotate({-BOID_H*0.5, BOID_W*0.5}, boid.angle) + boid.pos
}
player_input :: proc(using s: ^GameState) { player_input :: proc(using s: ^GameState) {
horizontal: f32 horizontal: f32
vertical: f32 vertical: f32
@ -73,14 +84,21 @@ update :: proc(using s: ^GameState) {
steer := desired - boid.vel steer := desired - boid.vel
steer = rl.Vector2ClampValue(steer, 0, 0.3) steer = rl.Vector2ClampValue(steer, 0, 0.3)
boid.vel += steer boid.vel += steer
boid.angle = math.atan2(boid.vel.y, boid.vel.x)
boid.pos += boid.vel boid.pos += boid.vel
} }
} }
} }
draw :: proc(using s: ^GameState) { draw :: proc(using s: ^GameState) {
boid_shape := [4]Vec2{}
for boid in boids { for boid in boids {
rl.DrawCircleV(boid.pos, 15, rl.RED) get_boid_shape(boid, &boid_shape)
rl.DrawLineEx(boid_shape[0], boid_shape[1], 2, rl.BLACK)
rl.DrawLineEx(boid_shape[1], boid_shape[2], 2, rl.BLACK)
rl.DrawLineEx(boid_shape[2], boid_shape[3], 2, rl.BLACK)
rl.DrawLineEx(boid_shape[3], boid_shape[0], 2, rl.BLACK)
} }
} }