Molecular Geometry 3D Rotation

Hi,

I’m using P3D to create 3D models of organic molecules. Currently, I’m working on a tetrahedral structure, where the angles formed by all of the bonds are 109.5 degrees (see 2nd diagram). In this case, I have the gray carbon atom bonded to a white hydrogen atom.

Expectation:
stupid 1 image limit

Below is a diagram with all the math I did, to ensure that the hydrogen atom will be positioned correctly relative to the carbon atom so that they appear bonded.

Instead, I get a gap as shown below.

stupid 1 image limit

There must be something wrong with my calculations or rotations I think but I don’t know where.

My code for reference (the rotations are in the float arrays, corresponding to x, y, and z):

``````float angle = 109.5F;
float k = Atom.RADIUS + Atom.LENGTH * 2 + Atom.HYDROGEN_RADIUS;
//radius is Carbon radius, length is the  length of bond
atoms.add(new Atom(Atom.CARBON, x, y, 0, new float[]{0, -angle, angle - 90}, false));
atoms.add(new Atom(Atom.HYDROGEN, x - k * sin(radians(angle - 90)), y + k * sin(radians(angle - 90)), k * cos(radians(angle - 90)), new float[]{0, 180 - angle, -(angle - 90)}, false));
``````

2 Likes

EDIT: Ok, so I know the problem, however due to me not studying trig in a while I forget how to go about solving it. But I will point out the error in the mean time in case you or someone else wants to jump in who already knows how to solve it. So when you are calculating the point of the second sphere the distance should be exactly k away, however with this test code, where I assumed k is 20:

``````float k = 20;
float angle = 109.5F;
PVector origin = new PVector(0,0,0);
PVector point = new PVector(0 - k * cos(radians(angle - 90)), 0 + k * sin(radians(angle - 90)),0);
println(origin.dist(point));
``````

the distance we get is 21.084848 when it should be 20.0, just a wee bit over which explains the gap. The problem lies in the way you are trying to compute that point. If we do it this way, in 2 dimensions:

``````float k = 20;
float angle = 109.5F;
PVector origin = new PVector(0,0,0);
PVector point = new PVector(0 - k * cos(radians(angle - 90)), 0 + k * sin(radians(angle - 90)),0);
println(origin.dist(point));
``````

We see that we get the even 20.0, (basically a unit circle times 20) but simply trying to add a third circle in the z direction will not work because to make a circle around the y axis we will also have to modify x. They are all interlocked and can not be computed one at a time. So changing just the z is a linear function that will only be 20.0 at exactly 0 and no where else. I forget the equations necessary to find a point at a distance around a unit circle in 3 dimensions though.

1 Like

I have to agree - your math is bad.

Try placing points with axis that have only the X offset or the Z offset, and maybe you’ll then see that you need to take one rotation into account when doing the other triangle… Or try varying the angle more to see what’s wrong.

Basically, you can’t just do the components. You have to put your two points in opposite ends of a box…

1 Like

I see that my approach is wrong. So how do I calculate the values correctly? What do you mean by putting two points in opposite ends of a box?

If the carbon atom is at the origin (0,0,0), knowing that I will rotate it along the Y and Z axis by 109.5 degrees, how do I calculate the coordinates of the hydrogen atom (x, y, z)?

Like so:

You worked out the blue like the orange was red.

Work out what the yellow line is first.

Or something. I’m not sure this is helpful.

EDIT: Basically, draw a better diagram of it in 3D, in a box. Do a better job than I did. Label the angles in the planes of the faces of the box.

1 Like