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+
913int 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
1517void 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-
10585void 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
0 commit comments