Domanda

I would like to know how to move an object from pointA towards pointB by a small distance.
I know how it's done in 2d, but 3d is definitely a little different.
Here is some code of how i do it in 2D.

Vector pointA = ccp(1,1);
Vector pointB = ccp(10,10);

//find angle between the two points
float diffX = pointA.x - pointB.x;
float diffY = pointA.y - pointB.y;
float angle = atan2f(diffX, diffZ);
angle -= CC_DEGREES_TO_RADIANS(90);

float dis = 1.5;//how far away this should extend from the last particle
float x = -cosf(angle)*dis;
float y = sinf(angle)*dis;
object.position = ccp(object.position.x+x,object.position.y+y);

With that code i can successfully move an object by a distance of 1.5 towards pointB.

BUT i just can't find out how to do it with 3 dimensions.
So my question is how do i accomplish this in 3D?

È stato utile?

Soluzione

Uh, I think you don't need to calculate angle. Just normalize gap and move.

Vector pointA = Vector(1,1,1);
Vector pointB = Vector(10,10,10);
Vector gap = pointB - pointA;

// This code can occur division-by-zero error.
Vector dir = gap / sqrt(gap.x * gap.x + gap.y * gap.y + gap.z * gap.z); 

float dist = 1.5;
object.position += dir * dist;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top