use of spacegraph.space3d.phys.Body3D in project narchy by automenta.
the class ForceDirected method repel.
private static void repel(Collidable x, Collidable y, float speed, float maxDist) {
SimpleSpatial xp = ((SimpleSpatial) x.data());
if (xp == null)
return;
SimpleSpatial yp = ((SimpleSpatial) y.data());
if (yp == null)
return;
v3 delta = v();
delta.sub(xp.transform(), yp.transform());
float len = delta.normalize();
len -= (xp.radius() + yp.radius());
if (len < 0)
len = 0;
else if (len >= maxDist)
return;
float s = speed / (1 + (len * len));
v3 v = v(delta.x * s, delta.y * s, delta.z * s);
((Body3D) x).velAdd(v);
v.negate();
((Body3D) y).velAdd(v);
}
use of spacegraph.space3d.phys.Body3D in project narchy by automenta.
the class ForceDirected method attract.
protected static void attract(Collidable x, Collidable y, float speed, float idealDist) {
SimpleSpatial xp = ((SimpleSpatial) x.data());
SimpleSpatial yp = ((SimpleSpatial) y.data());
v3 delta = v();
delta.sub(yp.transform(), xp.transform());
float lenSq = delta.lengthSquared();
if (!Float.isFinite(lenSq))
return;
if (lenSq < idealDist * idealDist)
return;
delta.normalize();
// constant speed
// delta.scale( speed );
// speed proportional to length
float len = (float) Math.sqrt(lenSq);
delta.scale(Math.min(len, len * speed));
((Body3D) x).velAdd(delta);
// delta2.scale(-(speed * (yp.mass() /* + yp.mass()*/) ) * len );
delta.scale(-1);
((Body3D) y).velAdd(delta);
}
use of spacegraph.space3d.phys.Body3D in project narchy by automenta.
the class ConceptWidget method newBody.
@Override
public Body3D newBody(boolean collidesWithOthersLikeThis) {
Body3D x = super.newBody(collidesWithOthersLikeThis);
final float initDistanceEpsilon = 50f;
// place in a random direction
x.transform.set(r(initDistanceEpsilon), r(initDistanceEpsilon), r(initDistanceEpsilon));
// impulse in a random direction
final float initImpulseEpsilon = 0.25f;
x.impulse(v(r(initImpulseEpsilon), r(initImpulseEpsilon), r(initImpulseEpsilon)));
return x;
}
use of spacegraph.space3d.phys.Body3D in project narchy by automenta.
the class RoverMaze1 method main.
public static void main(String[] args) {
Rover r = new Rover(new NARS().get()) {
@Override
protected void create(Dynamics3D world) {
SimpleSpatial torso;
add(torso = new SimpleSpatial("torso") {
@Override
protected CollisionShape newShape() {
// return new CylinderShape(v(1f, 0.1f, 1f));
return new BoxShape(v(1.6f, 0.1f, 1f));
}
@Override
public float mass() {
return 40f;
}
});
SimpleSpatial neck;
add(neck = new SimpleSpatial("neck") {
@Override
protected CollisionShape newShape() {
// return new TetrahedronShapeEx(v(0,10,0), v(10,0,0), v(10,10,0), v(0,0,10));
return new CylinderShape(v(0.25f, 0.75f, 0.25f));
}
@Override
protected Body3D create(Dynamics3D world) {
torso.body.clearForces();
Body3D n = super.create(world);
HingeConstraint p = new HingeConstraint(torso.body, body, v(0, 0.2f, 0), v(0, -1f, 0), v(1, 0, 0), v(1, 0, 0));
p.setLimit(-1.0f, 1.0f);
add(p);
return n;
}
@Override
public float mass() {
return 10f;
}
});
neck.shapeColor[0] = 1f;
neck.shapeColor[1] = 0.1f;
neck.shapeColor[2] = 0.5f;
neck.shapeColor[3] = 1f;
RetinaGrid rg = new RetinaGrid("cam1", v(), v(0, 0, 1), v(0.1f, 0, 0), v(0, 0.1f, 0), 6, 6, 4f) {
@Override
protected Body3D create(Dynamics3D world) {
Body3D l = super.create(world);
// move(0,-1,0);
// body.clearForces();
l.clearForces();
HingeConstraint p = new HingeConstraint(neck.body, body, v(0, 0.6f, 0), v(0, -0.6f, 0), v(0, 1, 0), v(0, 1, 0));
p.setLimit(-0.75f, 0.75f);
// Point2PointConstraint p = new Point2PointConstraint(body, torso.body, v(2, 0, 0), v(-2, 0, 0));
// p.impulseClamp = 0.01f;
// //p.damping = 0.5f;
// p.tau = 0.01f;
add(p);
return l;
}
};
add(rg);
}
};
// new SpaceGraph<>(
// new Maze("x", 20, 20),
// r
// );//.setGravity(v(0, 0, -5)).show(1000, 1000);
}
use of spacegraph.space3d.phys.Body3D in project narchy by automenta.
the class OrbMouse method mousePick.
@Nullable
ClosestRay mousePick(int x, int y) {
// float top = 1f;
// float bottom = -1f;
// float nearPlane = 1f;
float tanFov = (space.top - space.bottom) * 0.5f / space.zNear;
float fov = 2f * (float) Math.atan(tanFov);
v3 rayFrom = new v3(space.camPos);
v3 rayForward = new v3(space.camFwd);
rayForward.scale(space.zFar);
// v3 rightOffset = new v3();
v3 vertical = new v3(space.camUp);
v3 hor = new v3();
// TODO: check: hor = rayForward.cross(vertical);
hor.cross(rayForward, vertical);
hor.normalize();
// TODO: check: vertical = hor.cross(rayForward);
vertical.cross(hor, rayForward);
vertical.normalize();
float tanfov = (float) Math.tan(0.5f * fov);
float ww = space.getWidth();
float hh = space.getHeight();
float aspect = hh / ww;
hor.scale(2f * space.zFar * tanfov);
vertical.scale(2f * space.zFar * tanfov);
if (aspect < 1f) {
hor.scale(1f / aspect);
} else {
vertical.scale(aspect);
}
v3 rayToCenter = new v3();
rayToCenter.add(rayFrom, rayForward);
v3 dHor = new v3(hor);
dHor.scale(1f / ww);
v3 dVert = new v3(vertical);
dVert.scale(1f / hh);
v3 tmp1 = new v3();
v3 tmp2 = new v3();
tmp1.scale(0.5f, hor);
tmp2.scale(0.5f, vertical);
v3 rayTo = new v3();
rayTo.sub(rayToCenter, tmp1);
rayTo.add(tmp2);
tmp1.scale(x, dHor);
tmp2.scale(y, dVert);
rayTo.add(tmp1);
rayTo.sub(tmp2);
ClosestRay r = new ClosestRay(space.camPos, rayTo);
space.dyn.rayTest(space.camPos, rayTo, r, simplexSolver);
if (rayCallback.hasHit()) {
Body3D body = Body3D.ifDynamic(rayCallback.collidable);
if (body != null && (!(body.isStaticObject() || body.isKinematicObject()))) {
pickedBody = body;
hitPoint = r.hitPointWorld;
}
}
return r;
}
Aggregations