Skip to content

Commit ae32b4a

Browse files
committed
Simplify orbiting hazard code
1 parent e42fa2d commit ae32b4a

File tree

3 files changed

+29
-56
lines changed

3 files changed

+29
-56
lines changed

src/game/objects/hazard.c

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include <math.h>
77
#include <stdlib.h>
88

9+
static fixedpt const dest_threshold = fixedpt_fromint(5);
10+
static fixedpt const speed_cap = fixedpt_fromint(3);
11+
static int const recip_accel_scale = 16; // higher values = less acceleration
12+
913
int orb_almost_there(vec2f a, vec2f b) {
10-
vec2f dir = vec2f_sub(a, b);
11-
fixedpt t = fixedpt_fromint(2); // threshold
12-
return (dir.fx >= -t && dir.fx <= t && dir.fy >= -t && dir.fy <= t);
14+
return vec2f_distsqr(a, b) < fixedpt_xmul(dest_threshold, dest_threshold);
1315
}
1416

1517
void hazard_tick(object *obj) {
@@ -32,19 +34,12 @@ void hazard_tick(object *obj) {
3234
// XXX come up with a better equation to randomize the destination
3335
obj->orbit_pos = obj->pos;
3436
obj->orbit_pos_vary = vec2f_createf(0, 0);
35-
fixedpt mag;
3637
int limit = 10;
3738
do {
3839
obj->orbit_dest = vec2f_createf(random_fixedpt(&obj->gs->rand, fixedpt_fromint(320)),
3940
random_fixedpt(&obj->gs->rand, fixedpt_fromint(200)));
40-
obj->orbit_dest_dir = vec2f_sub(obj->orbit_dest, obj->orbit_pos);
41-
mag = vec2f_magsqr(obj->orbit_dest_dir);
4241
limit--;
43-
} while(mag < fixedpt_fromint(80 * 80) && limit > 0);
44-
45-
mag = fixedpt_sqrt(mag);
46-
obj->orbit_dest_dir.fx = fixedpt_div(obj->orbit_dest_dir.fx, mag);
47-
obj->orbit_dest_dir.fy = fixedpt_div(obj->orbit_dest_dir.fy, mag);
42+
} while(limit > 0 && vec2f_distsqr(obj->orbit_dest, obj->orbit_pos) < fixedpt_fromint(80 * 80));
4843
}
4944
}
5045
}
@@ -87,21 +82,6 @@ vec2f generate_destination(object *obj) {
8782
return new;
8883
}
8984

90-
void accelerate_orbit(object *obj) {
91-
fixedpt x_dist = obj->pos.fx - obj->orbit_dest.fx;
92-
fixedpt y_dist = obj->pos.fy - obj->orbit_dest.fy;
93-
fixedpt bigger = x_dist > y_dist ? x_dist : y_dist;
94-
if(fixedpt_abs(bigger) > fixedpt_fromint(20)) {
95-
bigger *= -1;
96-
}
97-
if(obj->vel.fx < fixedpt_fromint(1)) {
98-
obj->vel.fx += fixedpt_xdiv(x_dist, bigger * 10);
99-
}
100-
if(obj->vel.fy < fixedpt_fromint(1)) {
101-
obj->vel.fy += fixedpt_xdiv(y_dist, bigger * 10);
102-
}
103-
}
104-
10585
void hazard_move(object *obj) {
10686
if(obj->orbit) {
10787
/*
@@ -132,36 +112,21 @@ void hazard_move(object *obj) {
132112

133113
// accelerate_orbit(obj);
134114

135-
fixedpt x_dist = obj->pos.fx - obj->orbit_dest.fx;
136-
fixedpt y_dist = obj->pos.fy - obj->orbit_dest.fy;
137-
fixedpt bigger = fabsf(y_dist);
138-
if(fabsf(x_dist) > fabsf(y_dist)) {
139-
bigger = x_dist;
140-
}
115+
fixedpt x_dist = obj->orbit_dest.fx - obj->pos.fx;
116+
fixedpt y_dist = obj->orbit_dest.fy - obj->pos.fy;
117+
fixedpt bigger = fixedpt_max2(fixedpt_abs(x_dist), fixedpt_abs(y_dist));
141118

142-
if(obj->orbit_dest.fx > obj->pos.fx) {
143-
if(obj->vel.fx < fixedpt_fromint(1)) {
144-
log_debug("accel +%f", fixedpt_tofloat(fixedpt_xdiv(x_dist, bigger * 10)));
145-
obj->vel.fx += fixedpt_xdiv(x_dist, bigger * 10);
146-
}
147-
}
148-
if(obj->orbit_dest.fx < obj->pos.fx) {
149-
if(obj->vel.fx < fixedpt_fromint(1)) {
150-
log_debug("accel -%f", fixedpt_tofloat(fixedpt_xdiv(x_dist, bigger * 10)));
151-
obj->vel.fx -= x_dist / (bigger * 10);
152-
}
153-
}
154-
if(obj->orbit_dest.fy > obj->pos.fy) {
155-
if(obj->vel.fy < fixedpt_fromint(1)) {
156-
log_debug("accel +%f", fixedpt_tofloat(fixedpt_xdiv(y_dist, bigger * 10)));
157-
obj->vel.fy += fixedpt_xdiv(y_dist, bigger * 10);
158-
}
159-
}
160-
if(obj->orbit_dest.fy < obj->pos.fy) {
161-
if(obj->vel.fy < fixedpt_fromint(1)) {
162-
log_debug("accel -%f", fixedpt_tofloat(fixedpt_xdiv(y_dist, bigger * 10)));
163-
obj->vel.fy -= fixedpt_xdiv(y_dist, bigger * 10);
164-
}
119+
vec2f accel = vec2f_createf(fixedpt_xdiv(x_dist, bigger) / recip_accel_scale,
120+
fixedpt_xdiv(y_dist, bigger) / recip_accel_scale);
121+
122+
// log_debug("dist %f %f", fixedpt_tofloat(x_dist), fixedpt_tofloat(y_dist));
123+
// log_debug("accel %f %f", fixedpt_tofloat(accel.fx), fixedpt_tofloat(accel.fy));
124+
125+
obj->vel = vec2f_add(obj->vel, accel);
126+
127+
fixedpt speed = vec2f_mag(obj->vel);
128+
if(speed > speed_cap) {
129+
obj->vel = vec2f_div(obj->vel, fixedpt_xdiv(speed, speed_cap));
165130
}
166131

167132
// Make this object orbit around the center of the arena

src/game/protos/object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ struct object_t {
9090
int8_t orbit;
9191
float orbit_tick;
9292
vec2f orbit_dest;
93-
vec2f orbit_dest_dir;
9493
vec2f orbit_pos;
9594
vec2f orbit_pos_vary;
9695

src/utils/vec.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ static inline vec2f vec2f_mult(vec2f a, vec2f b) {
5050
return a;
5151
}
5252

53+
static inline vec2f vec2f_div(vec2f a, fixedpt div) {
54+
a.fx = fixedpt_xdiv(a.fx, div);
55+
a.fy = fixedpt_xdiv(a.fy, div);
56+
return a;
57+
}
58+
5359
static inline vec2i vec2f_to_i(vec2f f) {
5460
vec2i i;
5561
i.x = fixedpt_toint(f.fx);
@@ -85,6 +91,9 @@ static inline fixedpt vec2f_distsqr(vec2f a, vec2f b) {
8591
static inline fixedpt vec2f_dist(vec2f a, vec2f b) {
8692
return vec2f_mag(vec2f_sub(a, b));
8793
}
94+
static inline fixedpt vec2f_manhattan_dist(vec2f a, vec2f b) {
95+
return fixedpt_abs(a.fx - b.fx) + fixedpt_abs(a.fy - b.fy);
96+
}
8897

8998
static inline vec2i vec2i_create(int x, int y) {
9099
vec2i v;

0 commit comments

Comments
 (0)