Moon
Moon

Reputation: 20012

C# XNA - Calculating next point in a vector direction

lets say i have two points A & B in my 3D Space

enter image description here

now i want to start calculate points from A to B in direction of B and i want to continue calculation farther from B on the same line.

How t do it ?

what i am actually working on is bullets from plane.

Upvotes: 0

Views: 5310

Answers (2)

Moon
Moon

Reputation: 20012

its a try to implement a 2D technique in 3D, i calculate the following once

position = start;
dx = destination.X - start.X;
dy = destination.Y - start.Y;
dz = destination.Z - start.Z;

distance = (float)Math.Sqrt( dx * dx + dy * dy + dz * dz );
scale = 2f / distance;

then i go on calculating

position.X += dx * scale;
position.Y += dy * scale;
position.Z += dz * scale;

but the result is still is not working of 3D Space, i am getting result for only 2 Dimension, the third axis is not being changed

Upvotes: 0

SirViver
SirViver

Reputation: 2441

If I understood your question correctly, you should first get the direction vector by calculating

dir = B - A

and then you can continue the travel by

C = B + dir

Otherwise, please clarify your question, like for example what you mean by "calculate points from A to B", because mathematically there is an infinite amount of points between A and B.


Edit: If you want to trace a bullet path you have two options:

1) You implement it as hitscan weapon like done in many FPS; any bullets fired will immediately hit where they were aimed. This can be best achieved by doing a raytrace via Ray.Intersects and is probably the simplest and least computationally intensive way to do it. Of course, it's also not terribly realistic.

2) You make the bullet a physical entity in your world and move it during your Update() call via a "normal" combination of current position vector and movement/velocity vector, doing a manual collision detection with any hittable surfaces and objects. The advantage of this is that you can implement proper physics like travel time, drop, friction, etc., however it is also orders of magnitude more difficult to implement in a robust way. If you want to go this way, I suggest using either a pre-made physics API (like Bullet, but I'm not sure if there's a version for XNA) or at least doing extensive Google research on the subject, so you can avoid the many pitfalls of collision detection with fast-moving objects.

Upvotes: 3

Related Questions