vrshoot
diff libs/psys/rndval.c @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/psys/rndval.c Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,116 @@ 1.4 +#include <stdlib.h> 1.5 +#include "rndval.h" 1.6 + 1.7 +int psys_init_anm_rnd(struct psys_anm_rnd *r) 1.8 +{ 1.9 + if(psys_init_track(&r->value) == -1) { 1.10 + return -1; 1.11 + } 1.12 + if(psys_init_track(&r->range) == -1) { 1.13 + psys_destroy_track(&r->value); 1.14 + return -1; 1.15 + } 1.16 + return 0; 1.17 +} 1.18 + 1.19 +void psys_destroy_anm_rnd(struct psys_anm_rnd *r) 1.20 +{ 1.21 + psys_destroy_track(&r->value); 1.22 + psys_destroy_track(&r->range); 1.23 +} 1.24 + 1.25 +int psys_init_anm_rnd3(struct psys_anm_rnd3 *r) 1.26 +{ 1.27 + if(psys_init_track3(&r->value) == -1) { 1.28 + return -1; 1.29 + } 1.30 + if(psys_init_track3(&r->range) == -1) { 1.31 + psys_destroy_track3(&r->value); 1.32 + return -1; 1.33 + } 1.34 + return 0; 1.35 +} 1.36 + 1.37 +void psys_destroy_anm_rnd3(struct psys_anm_rnd3 *r) 1.38 +{ 1.39 + psys_destroy_track3(&r->value); 1.40 + psys_destroy_track3(&r->range); 1.41 +} 1.42 + 1.43 +void psys_copy_anm_rnd(struct psys_anm_rnd *dest, const struct psys_anm_rnd *src) 1.44 +{ 1.45 + psys_copy_track(&dest->value, &src->value); 1.46 + psys_copy_track(&dest->range, &src->range); 1.47 +} 1.48 + 1.49 +void psys_copy_anm_rnd3(struct psys_anm_rnd3 *dest, const struct psys_anm_rnd3 *src) 1.50 +{ 1.51 + psys_copy_track3(&dest->value, &src->value); 1.52 + psys_copy_track3(&dest->range, &src->range); 1.53 +} 1.54 + 1.55 +void psys_set_rnd(struct psys_rnd *r, float val, float range) 1.56 +{ 1.57 + r->value = val; 1.58 + r->range = range; 1.59 +} 1.60 + 1.61 +void psys_set_rnd3(struct psys_rnd3 *r, vec3_t val, vec3_t range) 1.62 +{ 1.63 + r->value = val; 1.64 + r->range = range; 1.65 +} 1.66 + 1.67 +void psys_set_anm_rnd(struct psys_anm_rnd *r, anm_time_t tm, float val, float range) 1.68 +{ 1.69 + psys_set_value(&r->value, tm, val); 1.70 + psys_set_value(&r->range, tm, range); 1.71 +} 1.72 + 1.73 +void psys_set_anm_rnd3(struct psys_anm_rnd3 *r, anm_time_t tm, vec3_t val, vec3_t range) 1.74 +{ 1.75 + psys_set_value3(&r->value, tm, val); 1.76 + psys_set_value3(&r->range, tm, range); 1.77 +} 1.78 + 1.79 + 1.80 +float psys_eval_rnd(struct psys_rnd *r) 1.81 +{ 1.82 + return r->value + r->range * (float)rand() / (float)RAND_MAX - 0.5 * r->range; 1.83 +} 1.84 + 1.85 +vec3_t psys_eval_rnd3(struct psys_rnd3 *r) 1.86 +{ 1.87 + vec3_t res; 1.88 + res.x = r->value.x + r->range.x * (float)rand() / (float)RAND_MAX - 0.5 * r->range.x; 1.89 + res.y = r->value.y + r->range.y * (float)rand() / (float)RAND_MAX - 0.5 * r->range.y; 1.90 + res.z = r->value.z + r->range.z * (float)rand() / (float)RAND_MAX - 0.5 * r->range.z; 1.91 + return res; 1.92 +} 1.93 + 1.94 + 1.95 +float psys_eval_anm_rnd(struct psys_anm_rnd *r, anm_time_t tm) 1.96 +{ 1.97 + struct psys_rnd tmp; 1.98 + if(tm == ANM_TIME_INVAL) { 1.99 + tmp.value = psys_get_cur_value(&r->value); 1.100 + tmp.range = psys_get_cur_value(&r->range); 1.101 + } else { 1.102 + tmp.value = psys_get_value(&r->value, tm); 1.103 + tmp.range = psys_get_value(&r->range, tm); 1.104 + } 1.105 + return psys_eval_rnd(&tmp); 1.106 +} 1.107 + 1.108 +vec3_t psys_eval_anm_rnd3(struct psys_anm_rnd3 *r, anm_time_t tm) 1.109 +{ 1.110 + struct psys_rnd3 tmp; 1.111 + if(tm == ANM_TIME_INVAL) { 1.112 + tmp.value = psys_get_cur_value3(&r->value); 1.113 + tmp.range = psys_get_cur_value3(&r->range); 1.114 + } else { 1.115 + tmp.value = psys_get_value3(&r->value, tm); 1.116 + tmp.range = psys_get_value3(&r->range, tm); 1.117 + } 1.118 + return psys_eval_rnd3(&tmp); 1.119 +}