John this code works for me. I.e. positioning a cylinder (bond) between two
endpoints (atoms).
Many, many, many thanks to the folks at Tripod from whose examples this code
is for the greater part taken!
Hope it helps you.
Cheers.
public BondEntry(Point3f startAtom, Point3f endAtom, float r, Appearance
appear) {
radius = r ;
height = (float) startAtom.distance( endAtom ) ;
//System.out.println( "(BondEntry) height="+height ) ;
xCoor = (startAtom.x + endAtom.x) / 2f ;
yCoor = (startAtom.y + endAtom.y) / 2f ;
zCoor = (startAtom.z + endAtom.z) / 2f ;
bondGroup = new BranchGroup() ;
bondGroup.setCapability( BranchGroup.ALLOW_CHILDREN_WRITE );
bondGroup.setCapability( BranchGroup.ALLOW_CHILDREN_READ );
bondGroup.setCapability( BranchGroup.ALLOW_DETACH );
Vector3f rel = new Vector3f( (float) (endAtom.x-startAtom.x),
(float) (endAtom.y-startAtom.y), (float) (endAtom.z-startAtom.z));
double xrot = calcAngle( new Vector3f(0.0f, 1.0f, 0.0f), rel);
Vector3f proj = new Vector3f(rel.x, 0.0f, rel.z);
float yrot = calcSign(new Vector3f(1.0f, 0.0f, 0.0f), proj)*
calcAngle(new Vector3f(0.0f, 0.0f, 1.0f ), proj);
//System.out.println( "xrot="+xrot+" yrot="+yrot ) ;
Transform3D rot = new Transform3D();
rot.rotX(xrot);
Transform3D bond_position = new Transform3D() ;
bond_position.rotY(yrot);
bond_position.mul(rot);
bond_position.setTranslation( new Vector3f( xCoor, yCoor, zCoor ) );
//bond_position.setScale(new
Vector3d(1.0,(double)(height/2.0f),1.0));
TransformGroup transformGroup = new TransformGroup(bond_position) ;
transformGroup.setCapability( TransformGroup.ALLOW_TRANSFORM_READ) ;
transformGroup.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE)
;
//System.out.println("(BondEntry) AngleX = "+new Double( angleX )+",
AngleY = "+new Double( angleY )+", AngleZ = "+new Double( angleZ ) );
Cylinder bond_cyl = new Cylinder( radius, height ) ;
bond_cyl.setAppearance( appear ) ;
transformGroup.addChild( bond_cyl ) ;
bondGroup.addChild( transformGroup ) ;
}
protected static float calcAngle(Vector3f coor, Vector3f other){
double scalarProduct = (double)coor.dot(other);
//System.out.println( "scalar product="+scalarProduct ) ;
if (scalarProduct == 0.0f) {
//System.out.println("acos "+Math.acos(0.0f));
return (float)Math.PI/2.0f;
}
scalarProduct = scalarProduct/(coor.length()*other.length());
return (float)Math.acos(scalarProduct);
}
protected static float calcSign(Vector3f coor, Vector3f other){
double sp = (double)coor.dot(other);
if (sp == 0.0f)
return 0.0f;
return (float)(sp/Math.abs(sp));
}
public BranchGroup getBondBG() {
return bondGroup ;
}
public float getX() { return xCoor ; }
public float getY() { return yCoor ; }
public float getZ() { return zCoor ; }
public void setX(float x) { xCoor = x ; }
public void setY(float y) { yCoor = y ; }
public void setZ(float z) { zCoor = z ; }
public void recomputeTrans() {
Transform3D bond_position = new Transform3D() ;
TransformGroup positionGroup = (TransformGroup)
ondGroup.getChild( 0 ) ;
positionGroup.getTransform( bond_position ) ;
bond_position.setTranslation( new Vector3f(xCoor, yCoor, zCoor) ) ;
positionGroup.setTransform( bond_position ) ;
}
}
-----Original Message-----
From: Discussion list for Java 3D API
[mailto:[EMAIL PROTECTED]]On Behalf Of John Nelson
Sent: Friday, October 05, 2001 7:27 PM
To: [EMAIL PROTECTED]
Subject: Re: [JAVA3D] This doesn't work quite right...
Ok, here's another newbie question...
Can I draw 2D objects within my 3D-based scene? I'd like to draw cartesian
coordinates among my objects... and of course those grid lines aren't really
3D objects but rather drawing artifacts within a 3D scene... Still I'd like
the grid to obey (at the very least) camera angles so that when I look at
the
objects in 3-space, the grid lines and cartesian lines change.
Can I merge Java 2D into 3D in this way?
-- John
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".