2004-10-01 14:10:30 +00:00
|
|
|
|
/* softbody.c
|
|
|
|
|
*
|
|
|
|
|
* $Id:
|
|
|
|
|
*
|
|
|
|
|
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version. The Blender
|
|
|
|
|
* Foundation also sells licenses for use in proprietary software under
|
|
|
|
|
* the Blender License. See http://www.blender.org/BL/ for information
|
|
|
|
|
* about this.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) Blender Foundation
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
|
*
|
|
|
|
|
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
|
|
|
|
*/
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/*
|
|
|
|
|
******
|
|
|
|
|
variables on the UI for now
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
float mediafrict; friction to env
|
|
|
|
|
float nodemass; softbody mass of *vertex*
|
|
|
|
|
float grav; softbody amount of gravitaion to apply
|
|
|
|
|
|
|
|
|
|
float goalspring; softbody goal springs
|
|
|
|
|
float goalfrict; softbody goal springs friction
|
|
|
|
|
float mingoal; quick limits for goal
|
|
|
|
|
float maxgoal;
|
|
|
|
|
|
|
|
|
|
float inspring; softbody inner springs
|
|
|
|
|
float infrict; softbody inner springs friction
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
|
|
|
|
*****
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <stdlib.h>
|
2005-01-13 13:58:29 +00:00
|
|
|
|
#include <string.h>
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
/* types */
|
2005-04-02 19:52:32 +00:00
|
|
|
|
#include "DNA_curve_types.h"
|
2004-10-01 14:10:30 +00:00
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2005-04-02 19:52:32 +00:00
|
|
|
|
#include "DNA_lattice_types.h"
|
2004-10-01 14:10:30 +00:00
|
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_blenlib.h"
|
|
|
|
|
#include "BLI_arithb.h"
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
#include "BKE_displist.h"
|
2005-04-04 18:09:47 +00:00
|
|
|
|
#include "BKE_effect.h"
|
2004-10-01 14:10:30 +00:00
|
|
|
|
#include "BKE_global.h"
|
2005-04-02 13:57:23 +00:00
|
|
|
|
#include "BKE_object.h"
|
2004-10-01 14:10:30 +00:00
|
|
|
|
#include "BKE_softbody.h"
|
2005-04-02 13:57:23 +00:00
|
|
|
|
#include "BKE_utildefines.h"
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
#include "BIF_editdeform.h"
|
2005-01-13 13:58:29 +00:00
|
|
|
|
|
|
|
|
|
extern bDeformGroup *get_named_vertexgroup(Object *ob, char *name);
|
|
|
|
|
extern int get_defgroup_num (Object *ob, bDeformGroup *dg);
|
|
|
|
|
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* ********** soft body engine ******* */
|
2005-01-12 22:28:13 +00:00
|
|
|
|
#define SOFTGOALSNAP 0.999f
|
|
|
|
|
// if bp-> goal is above make it a *forced follow original* and skip all ODE stuff for this bp
|
|
|
|
|
// removes *unnecessary* stiffnes from ODE system
|
|
|
|
|
#define HEUNWARNLIMIT 1 // 50 would be fine i think for detecting severe *stiff* stuff
|
|
|
|
|
|
|
|
|
|
float SoftHeunTol = 1.0f; // humm .. this should be calculated from sb parameters and sizes
|
|
|
|
|
float rescale_grav_to_framerate = 1.0f; // since unit of g is [m/sec^2] we need translation from frames to physics time
|
|
|
|
|
float rescale_friction_to_framerate = 1.0f; // since unit of drag is [kg/sec] we need translation from frames to physics time
|
|
|
|
|
|
2005-03-09 19:45:59 +00:00
|
|
|
|
/* local prototypes */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static void softbody_scale_time(float steptime);
|
|
|
|
|
static int get_scalar_from_named_vertexgroup(Object *ob, char *name, int vertID, float *target);
|
|
|
|
|
static void free_softbody_intern(SoftBody *sb);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static void softbody_scale_time(float steptime)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
rescale_grav_to_framerate = steptime*steptime;
|
|
|
|
|
rescale_friction_to_framerate = steptime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static int count_mesh_quads(Mesh *me)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
int a,result = 0;
|
|
|
|
|
MFace *mface= me->mface;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if(mface) {
|
|
|
|
|
for(a=me->totface; a>0; a--, mface++) {
|
|
|
|
|
if(mface->v4) result++;
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static void add_mesh_quad_diag_springs(Object *ob)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
MFace *mface= me->mface;
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
BodySpring *bs, *bs_new;
|
|
|
|
|
int a ;
|
|
|
|
|
|
|
|
|
|
if (ob->soft){
|
2005-04-02 13:57:23 +00:00
|
|
|
|
int nofquads;
|
|
|
|
|
|
|
|
|
|
nofquads = count_mesh_quads(me);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if (nofquads) {
|
|
|
|
|
/* resize spring-array to hold additional quad springs */
|
|
|
|
|
bs_new= MEM_callocN( (ob->soft->totspring + nofquads *2 )*sizeof(BodySpring), "bodyspring");
|
|
|
|
|
memcpy(bs_new,ob->soft->bspring,(ob->soft->totspring )*sizeof(BodySpring));
|
2005-04-02 19:52:32 +00:00
|
|
|
|
|
|
|
|
|
if(ob->soft->bspring)
|
|
|
|
|
MEM_freeN(ob->soft->bspring); /* do this before reassigning the pointer or have a 1st class memory leak */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
ob->soft->bspring = bs_new;
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* fill the tail */
|
|
|
|
|
a = 0;
|
|
|
|
|
bs = bs_new+ob->soft->totspring;
|
|
|
|
|
bp= ob->soft->bpoint;
|
|
|
|
|
if(mface ) {
|
|
|
|
|
for(a=me->totface; a>0; a--, mface++) {
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(mface->v4) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
bs->v1= mface->v1;
|
|
|
|
|
bs->v2= mface->v3;
|
|
|
|
|
bs->strength= 1.0;
|
|
|
|
|
bs->len= VecLenf( (bp+bs->v1)->origS, (bp+bs->v2)->origS);
|
|
|
|
|
bs++;
|
|
|
|
|
bs->v1= mface->v2;
|
|
|
|
|
bs->v2= mface->v4;
|
|
|
|
|
bs->strength= 1.0;
|
|
|
|
|
bs->len= VecLenf( (bp+bs->v1)->origS, (bp+bs->v2)->origS);
|
|
|
|
|
bs++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* now we can announce new springs */
|
|
|
|
|
ob->soft->totspring += nofquads *2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static void add_bp_springlist(BodyPoint *bp,int springID)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
int *newlist;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if (bp->springs == NULL) {
|
|
|
|
|
bp->springs = MEM_callocN( sizeof(int), "bpsprings");
|
|
|
|
|
bp->springs[0] = springID;
|
|
|
|
|
bp->nofsprings = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
bp->nofsprings++;
|
|
|
|
|
newlist = MEM_callocN(bp->nofsprings * sizeof(int), "bpsprings");
|
|
|
|
|
memcpy(newlist,bp->springs,(bp->nofsprings-1)* sizeof(int));
|
|
|
|
|
MEM_freeN(bp->springs);
|
|
|
|
|
bp->springs = newlist;
|
|
|
|
|
bp->springs[bp->nofsprings-1] = springID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* do this once when sb is build
|
|
|
|
|
it is O(N^2) so scanning for springs every iteration is too expensive
|
|
|
|
|
*/
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static void build_bps_springlist(Object *ob)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
BodySpring *bs;
|
|
|
|
|
int a,b;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if (sb==NULL) return; // paranoya check
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
/* scan for attached inner springs */
|
|
|
|
|
for(b=sb->totspring, bs= sb->bspring; b>0; b--, bs++) {
|
|
|
|
|
if (( (sb->totpoint-a) == bs->v1) ){
|
|
|
|
|
add_bp_springlist(bp,sb->totspring -b);
|
|
|
|
|
}
|
|
|
|
|
if (( (sb->totpoint-a) == bs->v2) ){
|
|
|
|
|
add_bp_springlist(bp,sb->totspring -b);
|
|
|
|
|
}
|
|
|
|
|
}//for springs
|
2005-04-02 13:57:23 +00:00
|
|
|
|
// if (bp->nofsprings) printf(" node %d has %d spring links\n",a,bp->nofsprings);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}//for bp
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* creates new softbody if didn't exist yet, makes new points and springs arrays */
|
|
|
|
|
/* called in mesh_to_softbody */
|
|
|
|
|
static void renew_softbody(Object *ob, int totpoint, int totspring)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftBody *sb;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(ob->soft==NULL) ob->soft= sbNew();
|
|
|
|
|
else free_softbody_intern(ob->soft);
|
|
|
|
|
sb= ob->soft;
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
if(totpoint) {
|
|
|
|
|
sb->totpoint= totpoint;
|
|
|
|
|
sb->totspring= totspring;
|
|
|
|
|
|
|
|
|
|
sb->bpoint= MEM_mallocN( totpoint*sizeof(BodyPoint), "bodypoint");
|
|
|
|
|
if(totspring)
|
|
|
|
|
sb->bspring= MEM_mallocN( totspring*sizeof(BodySpring), "bodyspring");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* only frees internal data */
|
|
|
|
|
static void free_softbody_intern(SoftBody *sb)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
|
|
|
|
if(sb) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
int a;
|
|
|
|
|
BodyPoint *bp;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if(sb->bpoint){
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
/* free spring list */
|
|
|
|
|
if (bp->springs != NULL) {
|
|
|
|
|
MEM_freeN(bp->springs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MEM_freeN(sb->bpoint);
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
if(sb->bspring) MEM_freeN(sb->bspring);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
sb->totpoint= sb->totspring= 0;
|
|
|
|
|
sb->bpoint= NULL;
|
|
|
|
|
sb->bspring= NULL;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* ************ dynamics ********** */
|
|
|
|
|
|
|
|
|
|
/* aye this belongs to arith.c */
|
2005-03-09 19:45:59 +00:00
|
|
|
|
static void Vec3PlusStVec(float *v, float s, float *v1)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
{
|
|
|
|
|
v[0] += s*v1[0];
|
|
|
|
|
v[1] += s*v1[1];
|
|
|
|
|
v[2] += s*v1[2];
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-04 18:09:47 +00:00
|
|
|
|
#define USES_FIELD 1
|
|
|
|
|
#define USES_DEFLECT 2
|
|
|
|
|
static int is_there_deflection(unsigned int layer)
|
|
|
|
|
{
|
|
|
|
|
Base *base;
|
|
|
|
|
int retval= 0;
|
|
|
|
|
|
|
|
|
|
for(base = G.scene->base.first; base; base= base->next) {
|
|
|
|
|
if( (base->lay & layer) && base->object->pd) {
|
|
|
|
|
if(base->object->pd->forcefield) retval |= USES_FIELD;
|
|
|
|
|
if(base->object->pd->deflect) retval |= USES_DEFLECT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
static void softbody_calc_forces(Object *ob, float forcetime)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
2005-01-12 22:28:13 +00:00
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
BodyPoint *bproot;
|
|
|
|
|
BodySpring *bs;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
float iks, ks, kd, gravity, actspringlen, forcefactor, sd[3];
|
2005-04-04 18:09:47 +00:00
|
|
|
|
int a, b, do_effector;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* clear forces */
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
bp->force[0]= bp->force[1]= bp->force[2]= 0.0;
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
gravity = sb->grav * rescale_grav_to_framerate;
|
2005-04-04 18:09:47 +00:00
|
|
|
|
/* check! */
|
|
|
|
|
do_effector= is_there_deflection(ob->lay);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
iks = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
|
2005-01-12 22:28:13 +00:00
|
|
|
|
bproot= sb->bpoint; /* need this for proper spring addressing */
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if(bp->goal < SOFTGOALSNAP){ // ommit this bp when i snaps
|
|
|
|
|
float auxvect[3]; // aux unit vector
|
|
|
|
|
float velgoal[3];
|
|
|
|
|
float absvel =0, projvel= 0;
|
|
|
|
|
|
|
|
|
|
/* do goal stuff */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(ob->softflag & OB_SB_GOAL) {
|
|
|
|
|
/* true elastic goal */
|
|
|
|
|
VecSubf(auxvect,bp->origT,bp->pos);
|
|
|
|
|
ks = 1.0f/(1.0f- bp->goal*sb->goalspring)-1.0f ;
|
|
|
|
|
bp->force[0]= ks*(auxvect[0]);
|
|
|
|
|
bp->force[1]= ks*(auxvect[1]);
|
|
|
|
|
bp->force[2]= ks*(auxvect[2]);
|
|
|
|
|
/* calulate damping forces generated by goals*/
|
|
|
|
|
VecSubf(velgoal,bp->origS, bp->origE);
|
|
|
|
|
kd = sb->goalfrict * rescale_friction_to_framerate ;
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
if (forcetime > 0.0 ) { // make sure friction does not become rocket motor on time reversal
|
2005-04-02 13:57:23 +00:00
|
|
|
|
bp->force[0]-= kd * (velgoal[0] + bp->vec[0]);
|
|
|
|
|
bp->force[1]-= kd * (velgoal[1] + bp->vec[1]);
|
|
|
|
|
bp->force[2]-= kd * (velgoal[2] + bp->vec[2]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
bp->force[0]-= kd * (velgoal[0] - bp->vec[0]);
|
|
|
|
|
bp->force[1]-= kd * (velgoal[1] - bp->vec[1]);
|
|
|
|
|
bp->force[2]-= kd * (velgoal[2] - bp->vec[2]);
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
/* done goal stuff */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* gravitation */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
bp->force[2]-= gravity*sb->nodemass; /* individual mass of node here */
|
2005-04-05 20:28:32 +00:00
|
|
|
|
|
|
|
|
|
/* particle field & vortex */
|
2005-04-04 18:09:47 +00:00
|
|
|
|
if(do_effector & USES_FIELD) {
|
|
|
|
|
float force[3]= {0.0f, 0.0f, 0.0f};
|
|
|
|
|
float speed[3]= {0.0f, 0.0f, 0.0f};
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
pdDoEffector(bp->pos, force, speed, (float)G.scene->r.cfra, ob->lay,PE_WIND_AS_SPEED);
|
|
|
|
|
// note: now we have wind as motion of media, so we can do anisotropic stuff here,
|
|
|
|
|
// if we had vertex normals here(BM)
|
2005-04-04 18:09:47 +00:00
|
|
|
|
/* apply force */
|
|
|
|
|
// VecMulf(force, rescale_grav_to_framerate); <- didn't work, made value far too low!
|
2005-04-05 20:28:32 +00:00
|
|
|
|
VecMulf(force,25.0f* rescale_friction_to_framerate);
|
2005-04-04 18:09:47 +00:00
|
|
|
|
VECADD(bp->force, bp->force, force);
|
|
|
|
|
/* apply speed. note; deflector can give 'speed' only.... */
|
2005-04-05 20:28:32 +00:00
|
|
|
|
/* nooo! we never alter free variables :bp->vec bp->pos in here !
|
|
|
|
|
* this will ruin adative stepsize AHA heun! (BM)
|
|
|
|
|
* VECADD(bp->vec, bp->vec, speed);
|
|
|
|
|
*/
|
|
|
|
|
/* friction in moving media */
|
|
|
|
|
kd= sb->mediafrict* rescale_friction_to_framerate;
|
2005-04-06 10:29:04 +00:00
|
|
|
|
bp->force[0] -= kd * (bp->vec[0] + speed[0]/rescale_friction_to_framerate);
|
|
|
|
|
bp->force[1] -= kd * (bp->vec[1] + speed[1]/rescale_friction_to_framerate);
|
|
|
|
|
bp->force[2] -= kd * (bp->vec[2] + speed[2]/rescale_friction_to_framerate);
|
2005-04-05 20:28:32 +00:00
|
|
|
|
/* now we'll have nice centrifugal effect for vortex */
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* friction in media (not) moving*/
|
|
|
|
|
kd= sb->mediafrict* rescale_friction_to_framerate;
|
|
|
|
|
/* assume it to be proportional to actual velocity */
|
|
|
|
|
bp->force[0]-= bp->vec[0]*kd;
|
|
|
|
|
bp->force[1]-= bp->vec[1]*kd;
|
|
|
|
|
bp->force[2]-= bp->vec[2]*kd;
|
|
|
|
|
/* friction in media done */
|
2005-04-04 18:09:47 +00:00
|
|
|
|
}
|
2005-04-05 20:28:32 +00:00
|
|
|
|
|
|
|
|
|
/*other forces*/
|
|
|
|
|
/* this is the place where other forces can be added
|
|
|
|
|
yes, constraints and collision stuff should go here too (read baraff papers on that!)
|
|
|
|
|
*/
|
2005-04-04 18:09:47 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/* copied from particles... doesn't work really! */
|
|
|
|
|
if(do_effector & USES_DEFLECT) {
|
|
|
|
|
int deflected;
|
|
|
|
|
int last_ob = -1;
|
|
|
|
|
int last_fc = -1;
|
|
|
|
|
int same_fc = 0;
|
|
|
|
|
float last[3], lastv[3];
|
|
|
|
|
int finish_defs = 1;
|
|
|
|
|
int def_count = 0;
|
|
|
|
|
|
|
|
|
|
VecSubf(last, bp->pos, bp->vec);
|
|
|
|
|
VECCOPY(lastv, bp->vec);
|
|
|
|
|
|
|
|
|
|
while (finish_defs) {
|
|
|
|
|
deflected= pdDoDeflection(last, bp->pos, lastv,
|
|
|
|
|
bp->vec, dtime, bp->force, 0,
|
|
|
|
|
G.scene->r.cfra, ob->lay, &last_ob, &last_fc, &same_fc);
|
|
|
|
|
if (deflected) {
|
|
|
|
|
def_count = def_count + 1;
|
|
|
|
|
//deflection = 1;
|
|
|
|
|
if (def_count==10) finish_defs = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
finish_defs = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/*other forces done*/
|
|
|
|
|
|
|
|
|
|
/* nice things could be done with anisotropic friction
|
|
|
|
|
like wind/air resistance in normal direction
|
|
|
|
|
--> having a piece of cloth sailing down
|
|
|
|
|
but this needs to have a *valid* vertex normal
|
|
|
|
|
*valid* means to be calulated on time axis
|
|
|
|
|
hrms .. may be a rough one could be used as well .. let's see
|
|
|
|
|
*/
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(ob->softflag & OB_SB_EDGES) {
|
|
|
|
|
if (1){ /* big mesh optimization */
|
|
|
|
|
/* run over attached inner spring list */
|
|
|
|
|
if (sb->bspring){ // spring list exists at all ?
|
|
|
|
|
for(b=bp->nofsprings;b>0;b--){
|
|
|
|
|
bs = sb->bspring + bp->springs[b-1];
|
|
|
|
|
if (( (sb->totpoint-a) == bs->v1) ){
|
|
|
|
|
actspringlen= VecLenf( (bproot+bs->v2)->pos, bp->pos);
|
|
|
|
|
VecSubf(sd,(bproot+bs->v2)->pos, bp->pos);
|
|
|
|
|
Normalise(sd);
|
|
|
|
|
|
|
|
|
|
// friction stuff V1
|
|
|
|
|
VecSubf(velgoal,bp->vec,(bproot+bs->v2)->vec);
|
|
|
|
|
kd = sb->infrict * rescale_friction_to_framerate ;
|
|
|
|
|
absvel = Normalise(velgoal);
|
|
|
|
|
projvel = ABS(Inpf(sd,velgoal));
|
|
|
|
|
kd *= absvel * projvel;
|
|
|
|
|
Vec3PlusStVec(bp->force,-kd,velgoal);
|
|
|
|
|
|
|
|
|
|
if(bs->len > 0.0) /* check for degenerated springs */
|
|
|
|
|
forcefactor = (bs->len - actspringlen)/bs->len * iks;
|
|
|
|
|
else
|
|
|
|
|
forcefactor = actspringlen * iks;
|
|
|
|
|
|
|
|
|
|
Vec3PlusStVec(bp->force,-forcefactor,sd);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (( (sb->totpoint-a) == bs->v2) ){
|
|
|
|
|
actspringlen= VecLenf( (bproot+bs->v1)->pos, bp->pos);
|
|
|
|
|
VecSubf(sd,bp->pos,(bproot+bs->v1)->pos);
|
|
|
|
|
Normalise(sd);
|
|
|
|
|
|
|
|
|
|
// friction stuff V2
|
|
|
|
|
VecSubf(velgoal,bp->vec,(bproot+bs->v1)->vec);
|
|
|
|
|
kd = sb->infrict * rescale_friction_to_framerate ;
|
|
|
|
|
absvel = Normalise(velgoal);
|
|
|
|
|
projvel = ABS(Inpf(sd,velgoal));
|
|
|
|
|
kd *= absvel * projvel;
|
|
|
|
|
Vec3PlusStVec(bp->force,-kd,velgoal);
|
|
|
|
|
|
|
|
|
|
if(bs->len > 0.0)
|
|
|
|
|
forcefactor = (bs->len - actspringlen)/bs->len * iks;
|
|
|
|
|
else
|
|
|
|
|
forcefactor = actspringlen * iks;
|
|
|
|
|
Vec3PlusStVec(bp->force,+forcefactor,sd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} //if spring list exists at all ?
|
|
|
|
|
}
|
|
|
|
|
else{ // this branch is not completly uptaded for friction stuff
|
|
|
|
|
/* scan for attached inner springs makes it a O(N^2) thing = bad !*/
|
|
|
|
|
/* obsolete .. but if someone wants to try the effect :) */
|
|
|
|
|
for(b=sb->totspring, bs= sb->bspring; b>0; b--, bs++) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if (( (sb->totpoint-a) == bs->v1) ){
|
|
|
|
|
actspringlen= VecLenf( (bproot+bs->v2)->pos, bp->pos);
|
|
|
|
|
VecSubf(sd,(bproot+bs->v2)->pos, bp->pos);
|
|
|
|
|
Normalise(sd);
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if(bs->len > 0.0) /* check for degenerated springs */
|
|
|
|
|
forcefactor = (bs->len - actspringlen)/bs->len * iks;
|
|
|
|
|
else
|
|
|
|
|
forcefactor = actspringlen * iks;
|
|
|
|
|
Vec3PlusStVec(bp->force,-forcefactor,sd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (( (sb->totpoint-a) == bs->v2) ){
|
|
|
|
|
actspringlen= VecLenf( (bproot+bs->v1)->pos, bp->pos);
|
|
|
|
|
VecSubf(sd,bp->pos,(bproot+bs->v1)->pos);
|
|
|
|
|
Normalise(sd);
|
|
|
|
|
|
|
|
|
|
if(bs->len > 0.0)
|
|
|
|
|
forcefactor = (bs->len - actspringlen)/bs->len * iks;
|
|
|
|
|
else
|
|
|
|
|
forcefactor = actspringlen * iks;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
Vec3PlusStVec(bp->force,+forcefactor,sd);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
}// no snap
|
|
|
|
|
}//for
|
|
|
|
|
}// if use edges
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
static void softbody_apply_forces(Object *ob, float forcetime, int mode, float *err)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* time evolution */
|
|
|
|
|
/* actually does an explicit euler step mode == 0 */
|
|
|
|
|
/* or heun ~ 2nd order runge-kutta steps, mode 1,2 */
|
2004-10-01 14:10:30 +00:00
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
|
|
|
|
BodyPoint *bp;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
float dx[3],dv[3];
|
|
|
|
|
float timeovermass;
|
|
|
|
|
float maxerr = 0.0;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
int a;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
if (sb->nodemass > 0.09999f) timeovermass = forcetime/sb->nodemass;
|
|
|
|
|
else timeovermass = forcetime/0.09999f;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if(bp->goal < SOFTGOALSNAP){
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
/* so here is (v)' = a(cceleration) = sum(F_springs)/m + gravitation + some friction forces + more forces*/
|
|
|
|
|
/* the ( ... )' operator denotes derivate respective time
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* the euler step for velocity then becomes */
|
|
|
|
|
/* v(t + dt) = v(t) + a(t) * dt */
|
|
|
|
|
bp->force[0]*= timeovermass; /* individual mass of node here */
|
|
|
|
|
bp->force[1]*= timeovermass;
|
|
|
|
|
bp->force[2]*= timeovermass;
|
|
|
|
|
/* some nasty if's to have heun in here too */
|
|
|
|
|
VECCOPY(dv,bp->force);
|
|
|
|
|
if (mode == 1){
|
2005-04-02 13:57:23 +00:00
|
|
|
|
VECCOPY(bp->prevvec, bp->vec);
|
|
|
|
|
VECCOPY(bp->prevdv, dv);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
if (mode ==2){
|
|
|
|
|
/* be optimistic and execute step */
|
|
|
|
|
bp->vec[0] = bp->prevvec[0] + 0.5f * (dv[0] + bp->prevdv[0]);
|
|
|
|
|
bp->vec[1] = bp->prevvec[1] + 0.5f * (dv[1] + bp->prevdv[1]);
|
|
|
|
|
bp->vec[2] = bp->prevvec[2] + 0.5f * (dv[2] + bp->prevdv[2]);
|
|
|
|
|
/* compare euler to heun to estimate error for step sizing */
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dv[0] - bp->prevdv[0]));
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dv[1] - bp->prevdv[1]));
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dv[2] - bp->prevdv[2]));
|
|
|
|
|
}
|
|
|
|
|
else {VECADD(bp->vec, bp->vec, bp->force);}
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
/* so here is (x)'= v(elocity) */
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* the euler step for location then becomes */
|
|
|
|
|
/* x(t + dt) = x(t) + v(t) * dt */
|
|
|
|
|
|
|
|
|
|
VECCOPY(dx,bp->vec);
|
2005-04-05 20:28:32 +00:00
|
|
|
|
dx[0]*=forcetime ;
|
|
|
|
|
dx[1]*=forcetime ;
|
|
|
|
|
dx[2]*=forcetime ;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
|
|
|
|
/* again some nasty if's to have heun in here too */
|
|
|
|
|
if (mode ==1){
|
|
|
|
|
VECCOPY(bp->prevpos,bp->pos);
|
|
|
|
|
VECCOPY(bp->prevdx ,dx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode ==2){
|
|
|
|
|
bp->pos[0] = bp->prevpos[0] + 0.5f * ( dx[0] + bp->prevdx[0]);
|
|
|
|
|
bp->pos[1] = bp->prevpos[1] + 0.5f * ( dx[1] + bp->prevdx[1]);
|
|
|
|
|
bp->pos[2] = bp->prevpos[2] + 0.5f* ( dx[2] + bp->prevdx[2]);
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dx[0] - bp->prevdx[0]));
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dx[1] - bp->prevdx[1]));
|
|
|
|
|
maxerr = MAX2(maxerr,ABS(dx[2] - bp->prevdx[2]));
|
|
|
|
|
}
|
|
|
|
|
else { VECADD(bp->pos, bp->pos, dx);}
|
|
|
|
|
|
|
|
|
|
}//snap
|
|
|
|
|
} //for
|
|
|
|
|
if (err){ /* so step size will be controlled by biggest difference in slope */
|
|
|
|
|
*err = maxerr;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* used by heun when it overshoots */
|
|
|
|
|
static void softbody_restore_prev_step(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
|
|
|
|
BodyPoint *bp;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
int a;
|
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
2005-04-02 13:57:23 +00:00
|
|
|
|
VECCOPY(bp->vec, bp->prevvec);
|
|
|
|
|
VECCOPY(bp->pos, bp->prevpos);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
|
|
|
|
static void softbody_apply_goalsnap(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
int a;
|
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
if (bp->goal >= SOFTGOALSNAP){
|
|
|
|
|
VECCOPY(bp->prevpos,bp->pos);
|
|
|
|
|
VECCOPY(bp->pos,bp->origT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* unused */
|
|
|
|
|
#if 0
|
2005-01-12 22:28:13 +00:00
|
|
|
|
static void softbody_force_goal(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
SoftBody *sb= ob->soft; // is supposed to be there
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
int a;
|
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
VECCOPY(bp->pos,bp->origT);
|
|
|
|
|
bp->vec[0] = bp->origE[0] - bp->origS[0];
|
|
|
|
|
bp->vec[1] = bp->origE[1] - bp->origS[1];
|
|
|
|
|
bp->vec[2] = bp->origE[2] - bp->origS[2];
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
#endif
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* expects full initialized softbody */
|
2005-01-12 22:28:13 +00:00
|
|
|
|
static void interpolate_exciter(Object *ob, int timescale, int time)
|
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftBody *sb= ob->soft;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
float f;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
int a;
|
|
|
|
|
|
|
|
|
|
// note: i removed Mesh usage here, softbody should remain generic! (ton)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
f = (float)time/(float)timescale;
|
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
bp->origT[0] = bp->origS[0] + f*(bp->origE[0] - bp->origS[0]);
|
|
|
|
|
bp->origT[1] = bp->origS[1] + f*(bp->origE[1] - bp->origS[1]);
|
|
|
|
|
bp->origT[2] = bp->origS[2] + f*(bp->origE[2] - bp->origS[2]);
|
|
|
|
|
if (bp->goal >= SOFTGOALSNAP){
|
|
|
|
|
bp->vec[0] = bp->origE[0] - bp->origS[0];
|
|
|
|
|
bp->vec[1] = bp->origE[1] - bp->origS[1];
|
|
|
|
|
bp->vec[2] = bp->origE[2] - bp->origS[2];
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ob->softflag & OB_SB_EDGES) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* hrms .. do springs alter their lenght ?
|
|
|
|
|
bs= ob->soft->bspring;
|
|
|
|
|
bp= ob->soft->bpoint;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
for(a=0; (a<me->totedge && a < ob->soft->totspring ); a++, bs++) {
|
|
|
|
|
bs->len= VecLenf( (bp+bs->v1)->origT, (bp+bs->v2)->origT);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
*/
|
2005-04-02 13:57:23 +00:00
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
|
|
|
|
/* ************ convertors ********** */
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* for each object type we need;
|
|
|
|
|
- xxxx_to_softbody(Object *ob) : a full (new) copy
|
|
|
|
|
- xxxx_update_softbody(Object *ob) : update refreshes current positions
|
|
|
|
|
- softbody_to_xxxx(Object *ob) : after simulation, copy vertex locations back
|
|
|
|
|
*/
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
/* helper call */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static int object_has_edges(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
if(ob->type==OB_MESH) {
|
|
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
if(me->medge) return 1;
|
|
|
|
|
}
|
|
|
|
|
else if(ob->type==OB_LATTICE) {
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
/* helper call */
|
|
|
|
|
static void set_body_point(Object *ob, BodyPoint *bp, float *vec)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
VECCOPY(bp->pos, vec);
|
|
|
|
|
Mat4MulVecfl(ob->obmat, bp->pos); // yep, sofbody is global coords
|
|
|
|
|
VECCOPY(bp->origS, bp->pos);
|
|
|
|
|
VECCOPY(bp->origE, bp->pos);
|
|
|
|
|
VECCOPY(bp->origT, bp->pos);
|
|
|
|
|
|
|
|
|
|
bp->vec[0]= bp->vec[1]= bp->vec[2]= 0.0;
|
|
|
|
|
bp->weight= 1.0;
|
|
|
|
|
bp->goal= ob->soft->defgoal;
|
|
|
|
|
|
|
|
|
|
bp->nofsprings= 0;
|
|
|
|
|
bp->springs= NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* copy original (new) situation in softbody, as result of matrices or deform */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* is assumed to enter function with ob->soft, but can be without points */
|
2004-10-01 14:10:30 +00:00
|
|
|
|
static void mesh_update_softbody(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
MVert *mvert= me->mvert;
|
2005-03-09 19:45:59 +00:00
|
|
|
|
/* MEdge *medge= me->medge; */ /*unused*/
|
2004-10-01 14:10:30 +00:00
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
int a;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
/* possible after a file read... */
|
|
|
|
|
if(ob->soft->totpoint!=me->totvert) sbObjectToSoftbody(ob);
|
|
|
|
|
|
|
|
|
|
if(me->totvert) {
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
|
|
|
|
bp= ob->soft->bpoint;
|
|
|
|
|
for(a=0; a<me->totvert; a++, mvert++, bp++) {
|
2005-01-12 22:28:13 +00:00
|
|
|
|
VECCOPY(bp->origS, bp->origE);
|
|
|
|
|
VECCOPY(bp->origE, mvert->co);
|
|
|
|
|
Mat4MulVecfl(ob->obmat, bp->origE);
|
|
|
|
|
VECCOPY(bp->origT, bp->origE);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if(ob->softflag & OB_SB_EDGES) {
|
|
|
|
|
|
|
|
|
|
/* happens when in UI edges was set */
|
|
|
|
|
if(ob->soft->bspring==NULL)
|
|
|
|
|
if(object_has_edges(ob)) sbObjectToSoftbody(ob);
|
|
|
|
|
|
2005-04-03 16:57:16 +00:00
|
|
|
|
/* hrms .. do springs alter their lenght ? (yes, mesh keys would (ton))
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(medge) {
|
|
|
|
|
bs= ob->soft->bspring;
|
|
|
|
|
bp= ob->soft->bpoint;
|
|
|
|
|
for(a=0; (a<me->totedge && a < ob->soft->totspring ); a++, medge++, bs++) {
|
|
|
|
|
bs->len= VecLenf( (bp+bs->v1)->origE, (bp+bs->v2)->origE);
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
*/
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
static int get_scalar_from_named_vertexgroup(Object *ob, char *name, int vertID, float *target)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* result 0 on success, else indicates error number
|
|
|
|
|
-- kind of *inverse* result defintion,
|
|
|
|
|
-- but this way we can signal error condition to caller
|
|
|
|
|
-- and yes this function must not be here but in a *vertex group module*
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
bDeformGroup *locGroup = NULL;
|
|
|
|
|
MDeformVert *dv;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
int i, groupindex;
|
|
|
|
|
|
2005-01-13 13:58:29 +00:00
|
|
|
|
locGroup = get_named_vertexgroup(ob,name);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if(locGroup){
|
|
|
|
|
/* retrieve index for that group */
|
2005-01-13 13:58:29 +00:00
|
|
|
|
groupindex = get_defgroup_num(ob,locGroup);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* spot the vert in deform vert list at mesh */
|
|
|
|
|
/* todo (coder paranoya) what if ob->data is not a mesh .. */
|
|
|
|
|
/* hrms.. would like to have the same for lattices anyhoo */
|
2005-01-13 13:58:29 +00:00
|
|
|
|
if (((Mesh *)ob->data)->dvert) {
|
|
|
|
|
dv = ((Mesh*)ob->data)->dvert + vertID;
|
|
|
|
|
/* Lets see if this vert is in the weight group */
|
|
|
|
|
for (i=0; i<dv->totweight; i++){
|
|
|
|
|
if (dv->dw[i].def_nr == groupindex){
|
2005-04-02 15:55:15 +00:00
|
|
|
|
*target= dv->dw[i].weight; /* got it ! */
|
2005-01-13 13:58:29 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
2005-01-13 13:58:29 +00:00
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
return 2;
|
|
|
|
|
}/*if(locGroup)*/
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* makes totally fresh start situation */
|
|
|
|
|
static void mesh_to_softbody(Object *ob)
|
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftBody *sb;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
MVert *mvert= me->mvert;
|
|
|
|
|
MEdge *medge= me->medge;
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
BodySpring *bs;
|
2005-04-02 19:52:32 +00:00
|
|
|
|
float goalfac;
|
2005-04-03 20:13:10 +00:00
|
|
|
|
int a, totedge;
|
|
|
|
|
|
|
|
|
|
if (ob->softflag & OB_SB_EDGES) totedge= me->totedge;
|
|
|
|
|
else totedge= 0;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* renew ends with ob->soft with points and edges, also checks & makes ob->soft */
|
2005-04-03 20:13:10 +00:00
|
|
|
|
renew_softbody(ob, me->totvert, totedge);
|
2005-04-02 19:52:32 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* we always make body points */
|
|
|
|
|
sb= ob->soft;
|
|
|
|
|
bp= sb->bpoint;
|
2005-04-02 19:52:32 +00:00
|
|
|
|
goalfac= ABS(sb->maxgoal - sb->mingoal);
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
for(a=me->totvert; a>0; a--, mvert++, bp++) {
|
2005-04-02 15:55:15 +00:00
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
set_body_point(ob, bp, mvert->co);
|
2005-04-02 15:55:15 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if (1) { /* switch to vg scalars*/
|
|
|
|
|
/* get scalar values needed *per vertex* from vertex group functions,
|
|
|
|
|
so we can *paint* them nicly ..
|
|
|
|
|
they are normalized [0.0..1.0] so may be we need amplitude for scale
|
|
|
|
|
which can be done by caller
|
|
|
|
|
but still .. i'd like it to go this way
|
|
|
|
|
*/
|
|
|
|
|
int error;
|
|
|
|
|
char name[32] = "SOFTGOAL";
|
|
|
|
|
float temp;
|
|
|
|
|
|
2005-04-02 15:55:15 +00:00
|
|
|
|
error = get_scalar_from_named_vertexgroup(ob, name, me->totvert - a, &temp);
|
2005-04-02 19:52:32 +00:00
|
|
|
|
if (!error) {
|
|
|
|
|
bp->goal = temp;
|
|
|
|
|
|
|
|
|
|
/* works assuming goal is <0.0, 1.0> */
|
2005-04-03 20:57:31 +00:00
|
|
|
|
bp->goal= sb->mingoal + bp->goal*goalfac;
|
2005-04-02 19:52:32 +00:00
|
|
|
|
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* a little ad hoc changing the goal control to be less *sharp* */
|
2005-04-02 19:52:32 +00:00
|
|
|
|
bp->goal = (float)pow(bp->goal, 4.0f);
|
2005-04-02 15:55:15 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* to proove the concept
|
|
|
|
|
this would enable per vertex *mass painting*
|
|
|
|
|
strcpy(name,"SOFTMASS");
|
|
|
|
|
error = get_scalar_from_named_vertexgroup(ob,name,me->totvert - a,&temp);
|
|
|
|
|
if (!error) bp->mass = temp * ob->rangeofmass;
|
|
|
|
|
*/
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
} /* switch to vg scalars */
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* but we only optionally add body edge springs */
|
|
|
|
|
if (ob->softflag & OB_SB_EDGES) {
|
2004-10-01 14:10:30 +00:00
|
|
|
|
if(medge) {
|
2005-04-02 13:57:23 +00:00
|
|
|
|
bs= sb->bspring;
|
|
|
|
|
bp= sb->bpoint;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
for(a=me->totedge; a>0; a--, medge++, bs++) {
|
|
|
|
|
bs->v1= medge->v1;
|
|
|
|
|
bs->v2= medge->v2;
|
|
|
|
|
bs->strength= 1.0;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
bs->len= VecLenf( (bp+bs->v1)->origS, (bp+bs->v2)->origS);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-03 20:13:10 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-03 20:13:10 +00:00
|
|
|
|
/* insert *diagonal* springs in quads if desired */
|
|
|
|
|
if (ob->softflag & OB_SB_QUADS) {
|
|
|
|
|
add_mesh_quad_diag_springs(ob);
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
2005-04-03 20:13:10 +00:00
|
|
|
|
build_bps_springlist(ob); /* big mesh optimization */
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-02 15:55:15 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* copies current sofbody position in mesh, so do this within modifier stacks! */
|
|
|
|
|
static void softbody_to_mesh(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
MVert *mvert;
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
int a;
|
|
|
|
|
|
|
|
|
|
bp= ob->soft->bpoint;
|
|
|
|
|
mvert= me->mvert;
|
|
|
|
|
for(a=me->totvert; a>0; a--, mvert++, bp++) {
|
|
|
|
|
VECCOPY(mvert->co, bp->pos);
|
|
|
|
|
Mat4MulVecfl(ob->imat, mvert->co); // softbody is in global coords
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* makes totally fresh start situation */
|
|
|
|
|
static void lattice_to_softbody(Object *ob)
|
|
|
|
|
{
|
2005-04-02 19:52:32 +00:00
|
|
|
|
SoftBody *sb;
|
|
|
|
|
Lattice *lt= ob->data;
|
|
|
|
|
BodyPoint *bop;
|
|
|
|
|
BPoint *bp;
|
|
|
|
|
int a, totvert;
|
|
|
|
|
|
|
|
|
|
totvert= lt->pntsu*lt->pntsv*lt->pntsw;
|
|
|
|
|
|
|
|
|
|
/* renew ends with ob->soft with points and edges, also checks & makes ob->soft */
|
|
|
|
|
renew_softbody(ob, totvert, 0);
|
|
|
|
|
|
|
|
|
|
/* we always make body points */
|
|
|
|
|
sb= ob->soft;
|
|
|
|
|
|
|
|
|
|
for(a= totvert, bp= lt->def, bop= sb->bpoint; a>0; a--, bp++, bop++) {
|
|
|
|
|
set_body_point(ob, bop, bp->vec);
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* copies current sofbody position */
|
|
|
|
|
static void softbody_to_lattice(Object *ob)
|
|
|
|
|
{
|
2005-04-02 19:52:32 +00:00
|
|
|
|
SoftBody *sb;
|
|
|
|
|
Lattice *lt= ob->data;
|
|
|
|
|
BodyPoint *bop;
|
|
|
|
|
BPoint *bp;
|
|
|
|
|
int a, totvert;
|
|
|
|
|
|
|
|
|
|
totvert= lt->pntsu*lt->pntsv*lt->pntsw;
|
|
|
|
|
sb= ob->soft;
|
|
|
|
|
|
|
|
|
|
for(a= totvert, bp= lt->def, bop= sb->bpoint; a>0; a--, bp++, bop++) {
|
|
|
|
|
VECCOPY(bp->vec, bop->pos);
|
|
|
|
|
Mat4MulVecfl(ob->imat, bp->vec); // softbody is in global coords
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
/* copy original (new) situation in softbody, as result of matrices or deform */
|
|
|
|
|
/* is assumed to enter function with ob->soft, but can be without points */
|
|
|
|
|
static void lattice_update_softbody(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
Lattice *lt= ob->data;
|
|
|
|
|
BodyPoint *bop;
|
|
|
|
|
BPoint *bp;
|
|
|
|
|
int a, totvert;
|
|
|
|
|
|
|
|
|
|
totvert= lt->pntsu*lt->pntsv*lt->pntsw;
|
|
|
|
|
|
|
|
|
|
/* possible after a file read... */
|
|
|
|
|
if(ob->soft->totpoint!=totvert) sbObjectToSoftbody(ob);
|
|
|
|
|
|
|
|
|
|
for(a= totvert, bp= lt->def, bop= ob->soft->bpoint; a>0; a--, bp++, bop++) {
|
|
|
|
|
VECCOPY(bop->origS, bop->origE);
|
|
|
|
|
VECCOPY(bop->origE, bp->vec);
|
|
|
|
|
Mat4MulVecfl(ob->obmat, bop->origE);
|
|
|
|
|
VECCOPY(bop->origT, bop->origE);
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* copies softbody result back in object */
|
|
|
|
|
/* only used in sbObjectStep() */
|
|
|
|
|
static void softbody_to_object(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(ob->soft==NULL) return;
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
/* inverse matrix is not uptodate... */
|
|
|
|
|
Mat4Invert(ob->imat, ob->obmat);
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
switch(ob->type) {
|
2005-04-02 19:52:32 +00:00
|
|
|
|
case OB_MESH:
|
|
|
|
|
softbody_to_mesh(ob);
|
|
|
|
|
break;
|
|
|
|
|
case OB_LATTICE:
|
|
|
|
|
softbody_to_lattice(ob);
|
|
|
|
|
break;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
|
|
|
|
/* copy original (new) situation in softbody, as result of matrices or deform */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* used in sbObjectStep() and sbObjectReset() */
|
|
|
|
|
/* assumes to have ob->soft, but can be entered without points */
|
|
|
|
|
static void object_update_softbody(Object *ob)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
switch(ob->type) {
|
2005-04-02 19:52:32 +00:00
|
|
|
|
case OB_MESH:
|
|
|
|
|
mesh_update_softbody(ob);
|
|
|
|
|
break;
|
|
|
|
|
case OB_LATTICE:
|
|
|
|
|
lattice_update_softbody(ob);
|
|
|
|
|
break;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ************ Object level, exported functions *************** */
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* allocates and initializes general main data */
|
|
|
|
|
SoftBody *sbNew(void)
|
|
|
|
|
{
|
|
|
|
|
SoftBody *sb;
|
|
|
|
|
|
|
|
|
|
sb= MEM_callocN(sizeof(SoftBody), "softbody");
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
sb->mediafrict= 0.5;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
sb->nodemass= 1.0;
|
|
|
|
|
sb->grav= 0.0;
|
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
sb->goalspring= 0.5;
|
|
|
|
|
sb->goalfrict= 0.0;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
sb->mingoal= 0.0;
|
|
|
|
|
sb->maxgoal= 1.0;
|
2005-04-02 19:52:32 +00:00
|
|
|
|
sb->defgoal= 0.7;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-04-02 19:52:32 +00:00
|
|
|
|
sb->inspring= 0.5;
|
|
|
|
|
sb->infrict= 0.5;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* frees all */
|
|
|
|
|
void sbFree(SoftBody *sb)
|
|
|
|
|
{
|
|
|
|
|
free_softbody_intern(sb);
|
|
|
|
|
MEM_freeN(sb);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* makes totally fresh start situation */
|
2005-04-02 13:57:23 +00:00
|
|
|
|
void sbObjectToSoftbody(Object *ob)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
switch(ob->type) {
|
|
|
|
|
case OB_MESH:
|
|
|
|
|
mesh_to_softbody(ob);
|
|
|
|
|
break;
|
|
|
|
|
case OB_LATTICE:
|
|
|
|
|
lattice_to_softbody(ob);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if(ob->soft) ob->soft->ctime= bsystem_time(ob, NULL, (float)G.scene->r.cfra, 0.0);
|
2005-04-02 15:55:15 +00:00
|
|
|
|
ob->softflag &= ~OB_SB_REDO;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* reset all motion */
|
|
|
|
|
void sbObjectReset(Object *ob)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftBody *sb= ob->soft;
|
|
|
|
|
BodyPoint *bp;
|
|
|
|
|
int a;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(sb==NULL) return;
|
|
|
|
|
sb->ctime= bsystem_time(ob, NULL, (float)G.scene->r.cfra, 0.0);
|
|
|
|
|
|
|
|
|
|
object_update_softbody(ob);
|
|
|
|
|
|
|
|
|
|
for(a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
|
|
|
|
|
// origS is previous timestep
|
|
|
|
|
VECCOPY(bp->origS, bp->origE);
|
|
|
|
|
VECCOPY(bp->pos, bp->origE);
|
2005-04-03 16:57:16 +00:00
|
|
|
|
VECCOPY(bp->origT, bp->origE);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
bp->vec[0]= bp->vec[1]= bp->vec[2]= 0.0f;
|
2005-04-03 16:57:16 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
// no idea about the Heun stuff! (ton)
|
2005-04-04 18:09:47 +00:00
|
|
|
|
VECCOPY(bp->prevpos, bp->pos);
|
2005-04-03 16:57:16 +00:00
|
|
|
|
VECCOPY(bp->prevvec, bp->vec);
|
|
|
|
|
VECCOPY(bp->prevdx, bp->vec);
|
|
|
|
|
VECCOPY(bp->prevdv, bp->vec);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* simulates one step. framenr is in frames */
|
|
|
|
|
/* copies result back to object, displist */
|
|
|
|
|
void sbObjectStep(Object *ob, float framenr)
|
2004-10-01 14:10:30 +00:00
|
|
|
|
{
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftBody *sb;
|
2005-04-04 18:09:47 +00:00
|
|
|
|
Base *base;
|
2004-10-01 14:10:30 +00:00
|
|
|
|
float dtime;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
int timescale,t;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
float ctime, forcetime;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
float err;
|
|
|
|
|
|
2005-04-02 15:55:15 +00:00
|
|
|
|
/* remake softbody if: */
|
|
|
|
|
if( (ob->softflag & OB_SB_REDO) || // signal after weightpainting
|
|
|
|
|
(ob->soft==NULL) || // just to be nice we allow full init
|
|
|
|
|
(ob->soft->totpoint==0) ) // after reading new file, or acceptable as signal to refresh
|
|
|
|
|
sbObjectToSoftbody(ob);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
sb= ob->soft;
|
2005-04-02 19:52:32 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* still no points? go away */
|
|
|
|
|
if(sb->totpoint==0) return;
|
|
|
|
|
|
2005-04-04 18:09:47 +00:00
|
|
|
|
/* reset deflector cache, sumohandle is free, but its still sorta abuse... (ton) */
|
|
|
|
|
for(base= G.scene->base.first; base; base= base->next) {
|
|
|
|
|
base->object->sumohandle= NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* checking time: */
|
|
|
|
|
ctime= bsystem_time(ob, NULL, framenr, 0.0);
|
2005-04-05 20:28:32 +00:00
|
|
|
|
softbody_scale_time(1.0f/G.scene->r.frs_sec); // translate frames/sec and lenghts unit to SI system
|
2005-04-02 13:57:23 +00:00
|
|
|
|
dtime= ctime - sb->ctime;
|
|
|
|
|
// bail out for negative or for large steps
|
2005-04-05 20:28:32 +00:00
|
|
|
|
if(dtime<0.0 || dtime >= 9.9*G.scene->r.framelen) { // note: what is G.scene->r.framelen for ? (BM)
|
2005-04-02 13:57:23 +00:00
|
|
|
|
sbObjectReset(ob);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* the simulator */
|
|
|
|
|
|
2005-04-05 20:28:32 +00:00
|
|
|
|
if(dtime > 0.0) { // note: what does this mean now? (ton)
|
|
|
|
|
//answer (BM) :
|
|
|
|
|
//dtime is still in [frames]
|
|
|
|
|
//we made sure dtime is >= 0.0
|
|
|
|
|
//but still need to handle dtime == 0.0 -> just return sb as is, just to be nice
|
2004-10-01 14:10:30 +00:00
|
|
|
|
object_update_softbody(ob);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if (TRUE) { // RSOL1 always true now (ton)
|
2005-01-12 22:28:13 +00:00
|
|
|
|
/* special case of 2nd order Runge-Kutta type AKA Heun */
|
2005-04-05 20:28:32 +00:00
|
|
|
|
float timedone =0.0; // how far did we get without violating error condition
|
|
|
|
|
/* loops = counter for emergency brake
|
2005-01-12 22:28:13 +00:00
|
|
|
|
* we don't want to lock up the system if physics fail
|
|
|
|
|
*/
|
|
|
|
|
int loops =0 ;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
SoftHeunTol = sb->rklimit; // humm .. this should be calculated from sb parameters and sizes
|
2005-01-12 22:28:13 +00:00
|
|
|
|
|
|
|
|
|
forcetime = dtime; /* hope for integrating in one step */
|
|
|
|
|
while ( (ABS(timedone) < ABS(dtime)) && (loops < 2000) )
|
|
|
|
|
{
|
2005-04-05 20:28:32 +00:00
|
|
|
|
if (ABS(dtime) > 9.0 ){
|
2005-04-02 13:57:23 +00:00
|
|
|
|
if(G.f & G_DEBUG) printf("SB_STEPSIZE \n");
|
2005-01-12 22:28:13 +00:00
|
|
|
|
break; // sorry but i must assume goal movement can't be interpolated any more
|
|
|
|
|
}
|
|
|
|
|
//set goals in time
|
|
|
|
|
interpolate_exciter(ob,200,(int)(200.0*(timedone/dtime)));
|
|
|
|
|
// do predictive euler step
|
2005-04-02 13:57:23 +00:00
|
|
|
|
softbody_calc_forces(ob, forcetime);
|
|
|
|
|
softbody_apply_forces(ob, forcetime, 1, NULL);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
// crop new slope values to do averaged slope step
|
2005-04-02 13:57:23 +00:00
|
|
|
|
softbody_calc_forces(ob, forcetime);
|
|
|
|
|
softbody_apply_forces(ob, forcetime, 2, &err);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
softbody_apply_goalsnap(ob);
|
|
|
|
|
|
|
|
|
|
if (err > SoftHeunTol){ // error needs to be scaled to some quantity
|
|
|
|
|
softbody_restore_prev_step(ob);
|
|
|
|
|
forcetime /= 2.0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float newtime = forcetime * 1.1f; // hope for 1.1 times better conditions in next step
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-01-12 22:28:13 +00:00
|
|
|
|
if (err > SoftHeunTol/2.0){ // stay with this stepsize unless err really small
|
|
|
|
|
newtime = forcetime;
|
|
|
|
|
}
|
|
|
|
|
timedone += forcetime;
|
|
|
|
|
if (forcetime > 0.0)
|
2005-04-02 13:57:23 +00:00
|
|
|
|
forcetime = MIN2(dtime - timedone,newtime);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
else
|
2005-04-02 13:57:23 +00:00
|
|
|
|
forcetime = MAX2(dtime - timedone,newtime);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
loops++;
|
|
|
|
|
}
|
|
|
|
|
// move snapped to final position
|
2005-04-02 13:57:23 +00:00
|
|
|
|
interpolate_exciter(ob, 2, 2);
|
2005-01-12 22:28:13 +00:00
|
|
|
|
softbody_apply_goalsnap(ob);
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
if(G.f & G_DEBUG) {
|
|
|
|
|
if (loops > HEUNWARNLIMIT) /* monitor high loop counts say 1000 after testing */
|
|
|
|
|
printf("%d heun integration loops/frame \n",loops);
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
2005-04-06 10:29:04 +00:00
|
|
|
|
else{
|
|
|
|
|
/* do brute force explicit euler */
|
|
|
|
|
/* inner intagration loop */
|
|
|
|
|
/* */
|
|
|
|
|
// loop n times so that n*h = duration of one frame := 1
|
|
|
|
|
// x(t+h) = x(t) + h*v(t);
|
|
|
|
|
// v(t+h) = v(t) + h*f(x(t),t);
|
|
|
|
|
timescale = (int)(sb->rklimit * ABS(dtime));
|
|
|
|
|
for(t=1 ; t <= timescale; t++) {
|
|
|
|
|
if (ABS(dtime) > 15 ) break;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
|
2005-04-06 10:29:04 +00:00
|
|
|
|
/* the *goal* mesh must use the n*h timing too !
|
|
|
|
|
use *cheap* linear intepolation for that */
|
|
|
|
|
interpolate_exciter(ob,timescale,t);
|
|
|
|
|
if (timescale > 0 ) {
|
|
|
|
|
forcetime = dtime/timescale;
|
|
|
|
|
|
|
|
|
|
/* does not fit the concept sloving ODEs :) */
|
|
|
|
|
/* softbody_apply_goal(ob,forcetime ); */
|
|
|
|
|
|
|
|
|
|
/* explicit Euler integration */
|
|
|
|
|
/* we are not controling a nuclear power plant!
|
|
|
|
|
so rought *almost* physical behaviour is acceptable.
|
|
|
|
|
in cases of *mild* stiffnes cranking up timscale -> decreasing stepsize *h*
|
|
|
|
|
avoids instability */
|
|
|
|
|
softbody_calc_forces(ob,forcetime);
|
|
|
|
|
softbody_apply_forces(ob,forcetime,0, NULL);
|
|
|
|
|
softbody_apply_goalsnap(ob);
|
|
|
|
|
|
|
|
|
|
// if (0){
|
2005-04-02 13:57:23 +00:00
|
|
|
|
/* ok here comes the <20>berhammer
|
|
|
|
|
use a semi implicit euler integration to tackle *all* stiff conditions
|
|
|
|
|
but i doubt the cost/benifit holds for most of the cases
|
|
|
|
|
-- to be coded*/
|
2005-04-06 10:29:04 +00:00
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
2005-01-12 22:28:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
/* and apply to vertices */
|
2005-01-12 22:28:13 +00:00
|
|
|
|
softbody_to_object(ob);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
|
2005-04-02 13:57:23 +00:00
|
|
|
|
sb->ctime= ctime;
|
2005-01-12 22:28:13 +00:00
|
|
|
|
} // if(ABS(dtime) > 0.0)
|
|
|
|
|
else {
|
2005-04-02 13:57:23 +00:00
|
|
|
|
// rule : you have asked for the current state of the softobject
|
2005-04-05 20:28:32 +00:00
|
|
|
|
// since dtime= ctime - ob->soft->ctime== 0.0;
|
2005-04-02 13:57:23 +00:00
|
|
|
|
// and we were not notifified about any other time changes
|
|
|
|
|
// so here it is !
|
|
|
|
|
softbody_to_object(ob);
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
2005-04-04 18:09:47 +00:00
|
|
|
|
|
|
|
|
|
/* reset deflector cache */
|
|
|
|
|
for(base= G.scene->base.first; base; base= base->next) {
|
|
|
|
|
if(base->object->sumohandle) {
|
|
|
|
|
MEM_freeN(base->object->sumohandle);
|
|
|
|
|
base->object->sumohandle= NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-01 14:10:30 +00:00
|
|
|
|
}
|
|
|
|
|
|