vec3 collide_sphere_sphere(struct sphere_hitbox player, struct sphere_hitbox sphere) { if (vec3_distance(player.pos, sphere.pos) >= player.r + sphere.r) { return player.pos; } return add_vec3(sphere.pos, mult_vec3(normalize(sub(player.pos, sphere.pos)), player.r + sphere.r)); }
// ************************************************************ // get hit result with a ray. RTHitResult RTPlane::GetHitResult(RTRay *r) { /* Ray = r->vStart + t * r->vDir and vNormal dot (vOrigin - vPointOnPlane) = 0. So substitute the ray equation r(t) in for x and solve for t: vNormal dot (vOrigin - ray(t)) = 0 vNormal dot (vOrigin - vStart - vDir*t) = 0 vNormal dot (vOrigin - vStart) = (vNormal dot vDir) * t t = [ vNormal dot (vOrigin - vStart) ] / (vNormal dot vDir) */ Vec3 vDirToOrigin = vPos.Sub(r->vStart); vDirToOrigin.Normalize(); Vec3 vDirNormalized = r->vDir.GetNormalized(); GLfloat numerator = vec3_dot(&vNormal, &vDirToOrigin); GLfloat denominator = vec3_dot(&vNormal, &vDirNormalized); GLfloat t = (GLfloat) (numerator / denominator); RTHitResult hr; hr.Clear(); // if line wasn't parallel or directly on the plane, if(t >= 0) { // find point hit hr.vPointHit = r->PointAtTValue(t); // if point hit was at a reasonable distance if(vec3_distance(&r->vStart, &hr.vPointHit) > 30) { hr.bHit = false; return hr; } else { // return a hit result containing the point, normal, etc. hr.bHit = true; hr.t = t; hr.vNormalHit = vNormal; hr.matHit = mat; } } return hr; }
void RailTrailEffect::tick() { if (Options::animation_level <= 0) return; static const float span = 0.5f; // space between particle layers static const float spin_span = PI / 8; const float dist = vec3_distance(this->end, this->start); const float step = span / (dist); Vec3 fwd = vec3_sub(this->end, this->start); //this->start = vec3_add(this->start, vec3_scalar_mult(vec3_normalize(this->start), 0.005f)); // RIGHT SETTINGS WOULD LOOK GOOD ON MOB IMPACTS ---> voxel_explode(this->end, /*min*/ 4, /*max*/ 14, /*size*/ 0.2f, /*force*/ 0.1f, COLOR_GREEN); float theta, phi; vec3_to_angles(fwd, &theta, &phi); float curr_spin = 0.0f; //float curr_spin = (PI/16) * this->ttl; for (float fl=0.0f; fl<=1.0f; fl+=step) { Vec3 curr = vec3_interpolate(this->start, this->end, fl); //float r = 0.17f; // quadratic radius float r = 0.08f; // quadratic radius Vec3 spiral = vec3_init( r * cosf(curr_spin), 0, r * sinf(curr_spin) ); Vec3 spiral2 = vec3_init( r * cosf(curr_spin + PI), 0, r * sinf(curr_spin + PI) ); spiral = vec3_euler_rotation(spiral, theta-0.5f, 0, phi-0.5f); spiral2 = vec3_euler_rotation(spiral2, theta-0.5f, 0, phi-0.5f); spiral = vec3_add(curr, spiral); spiral2 = vec3_add(curr, spiral2); spiral = translate_position(spiral); spiral2 = translate_position(spiral2); //float anim_scale = float(Options::animation_level)/3.0f; //n = anim_scale*float(n); Particle::Shrapnel *s; s = Particle::create_shrapnel(spiral, /*vel*/ vec3_init(0,0,0)); if (s == NULL) return; //s->ttl = randrange(8, 15); //s->scale = 0.1f; //s->texture_index = 54; s->ttl = randrange(2, 4); //s->ttl = 2; s->scale = 0.06f; s->texture_index = 22; s = Particle::create_shrapnel(spiral2, /*vel*/ vec3_init(0,0,0)); if (s == NULL) return; //s->ttl = randrange(8, 15); //s->scale = 0.1f; s->texture_index = 54; s->ttl = randrange(2, 4); //s->ttl = 2; s->scale = 0.06f; s->texture_index = 22; //draw_quad(curr, span, theta, phi); curr_spin += spin_span; if (curr_spin >= PI*2) curr_spin = 0.0f; } }