Hey guys!
My character shall have a ponytail that reacts to wind, gravity etc. separately from the character.
I tried and easy verlet integration but it doesn’t well.
https://dl.dropboxusercontent.com/u/13341521/pony.jpg
Left how I want it to be,
right how it is with my verlet integration.
Any Ideas how I can solve this?
This is my hair Class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Entity;
import java.awt.Graphics2D;
import java.awt.Stroke;
/**
*
* @author Nils
*/
public class Hair extends Entity
{
private HairJoint[] hairJoints;
private HairMesh[] hairMeshes;
private int cols;
private int rows;
private double w;
public Hair(int rows, int cols, double x, double y, double weight)
{
this.w = weight;
this.x = x;
this.y = y;
this.cols = cols;
this.rows = rows;
hairJoints = new HairJoint[rows * cols];
hairMeshes = new HairMesh[(cols - 1) * rows + (rows - 1) * cols];
int i = 0;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
hairJoints[r * cols + c] = new HairJoint(x, y+=rows*rows*.5);
if (c > 0)
{
hairMeshes[i++] = new HairMesh(hairJoints[r * cols + c - 1],
hairJoints[r * cols + c]);
}
if (r > 0)
{
hairMeshes[i++] = new HairMesh(hairJoints[r * cols + c], hairJoints[(r
- 1) * cols + c]);
}
}
}
}
public void setPosition(double x, double y)
{
hairJoints[0].x = x;
hairJoints[0].y = y;
}
@Override
public void render(Graphics2D g)
{
for (int i = 0; i < hairMeshes.length; i++)
{
hairMeshes[i].render(g);
}
}
@Override
public void process()
{
x += velX;
y += velY;
int t = hairJoints.length;
int i;
hairJoints[0].setPos(x, y);
double weight = w;
for (i = cols;i < t;i++)
{
hairJoints[i].y+= w/= 1.81;
hairJoints[i].process();
}
t = hairMeshes.length;
for(int stiff = 0;stiff < 6;stiff++)
{
for (i = 0;i < t;i++)
{
hairMeshes[i].process();
}
}
}
private class HairJoint extends Entity
{
private double x;
private double y;
private double oldX;
private double oldY;
public HairJoint(double x, double y)
{
setPos(x, y);
}
public void setPos(double x, double y)
{
this.x = oldX = x;
this.y = oldY = y;
}
@Override
public void render(Graphics2D g)
{
}
@Override
public void process()
{
double tempX = x;
double tempY = y;
x += x - oldX;
y += y - oldY;
oldX = tempX;
oldY = tempY;
}
}
private class HairMesh extends Entity
{
private HairJoint jointA;
private HairJoint jointB;
private double hypotenuse;
public HairMesh(HairJoint a, HairJoint b)
{
jointA = a;
jointB = b;
double dx = a.x - b.x;
double dy = a.y - b.y;
hypotenuse = Math.sqrt(dx * dx + dy * dy);
}
@Override
public void render(Graphics2D g)
{
g.drawLine((int) (jointA.x), (int) (jointA.y), (int) (jointB.x),
(int) (jointB.y));
}
@Override
public void process()
{
double dx = jointB.x - jointA.x;
double dy = jointB.y - jointA.y;
double h = Math.sqrt(dx * dx + dy * dy);
double diff = hypotenuse - h;
double offX = (diff * dx / h) *.2;
double offY = (diff * dy / h)* .2;
jointA.x -= offX;
jointA.y -= offY;
jointB.x += offX;
jointB.y += offY;
}
}
}