/*
 * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
*/

#ifndef RAT_PHSYICS_SPRING
#define RAT_PHSYICS_SPRING

// springs don't fit with the other constraints, because those have rigid
// constraining properties, but springs resolve over time by their nature,
// so we have enough accuracy to resolve springs in just one iteration per
// step.  We simply apply real impulse (offset to anchor) to each body based
// on the simple spring-mass model.  It's a simple application of Hooke's
// law.

#include "common.h"
#include "body.h"
#include "vector.h"

// three states; specified by a byte
#define RAT_SPRING_ACTIVITY_NEUTRAL 0x00 // 00000000
#define RAT_SPRING_ACTIVITY_PULLING 0xF0 // 11110000
#define RAT_SPRING_ACTIVITY_PUSHING 0x0F // 00001111

typedef struct rat_spring
{
	rat_body *body_a;
	rat_body *body_b;

	int island_marker;
	
	vector2 anch_a,anch_b;
	rat_real k,rest_length;
	rat_real damping;

	unsigned char activity; // used in drawing; is it pulling or pushing?
} rat_spring;

#ifdef __cplusplus
extern "C" {
#endif

rat_spring *rat_spring_create(rat_body *body_a,rat_body *body_b,
	vector2 anch_a,vector2 anch_b,rat_real k,rat_real rest_length,
	rat_real damping);
void rat_spring_destroy(rat_spring *spring);

void rat_spring_update_global_anchor(rat_spring *spring,vector2 new_anch_b);
void rat_spring_integrate(rat_spring *spring,rat_real inv_timestep,rat_real solve_coef);

#ifdef __cplusplus
}
#endif

#endif
