# Decomposition of speed vector

hi all,

i have some trouble with decomposition of vectors for steering behaviour seek which im trying to implement.

i have space ship which have position and linear velocity, heading in somewhere
i also have random destination point anywhere, not necessarily in the direction of ship

how can i decomposite the linear velocity, to get vector that represents the length of the velocity facing in the target direction?

pseudo code:
[java]
// Get the direction to target
Vector3f approachingDirection = targetPosition.subtract(myPosition).normalize();
// Get the approaching speed length
float approachingSpeed = myLinearVelocity.extractApproachingSpeed(approachingDirection).length();
[/java]

im thinking about experimenting with dot product, but i dont know

thanks ascaria

What you ask for would be simply vector.length()?
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

@normen no, no, sorry for confusion

i want to know approaching speed to the target if linear velocity does not aiming directly to the target

i know myPosition “A”, targetPosition “B”, myLinearVelocity “V”, and i want to solve for “X”, how big speed component is in that direction

I think the vector dot product is what you are looking for.

yea im thinking about that. it seems to be linear if we going from angle 0° to 90°
at angle 0° i want full speed and angle 90° have zero speed…
angle 180° i need zero speed or full backward speed i dont know, negative length should be okay
but i dont know if i want half of the speed if we are going at 45° angle, but for simplicity it could be enough precision… at the end, it is only a central linear force applied to rigidbody

edit: erm it might be harder than it seems, i have now:
[java]
/**
* Do state things.
*/
public void update(float tpf) {
// Set linear velocity and physics location of the entity
rigidBody.getLinearVelocity(linearVelocity);
rigidBody.getPhysicsLocation(physicsLocation);
mass = rigidBody.getMass();

    // Determine distance from target
actualDistance = physicsLocation.distance(loc);

// Look at target only if we are far away
if(actualDistance &gt;= criticalApproachingDistance) {
Quaternion targetRotation = new Quaternion();
Vector3f dir = loc.subtract(physicsLocation).normalizeLocal();
Vector3f up = rigidBody.getPhysicsRotation().getRotationColumn(1);
targetRotation.lookAt(dir, up);
rigidBody.setPhysicsRotation(targetRotation);
}

// Calculate steering force
desiredVelocity.set(loc).subtractLocal(physicsLocation).normalizeLocal()
.multLocal(/*TODO: implement engine movementPower*/ movementPower * mass * tpf);
steering.set(desiredVelocity).subtractLocal(linearVelocity);

// Calculate braking distance
Vector3f speedSquared = Vector3fHelper.square(linearVelocity);
Vector3f acceleration = steering.divide(mass);
float brakingDistance = 0.5f * speedSquared.divide(acceleration).length();

// Determine if we are approaching to (dot &gt; 0), or distancing from (dot &lt; 0) the target
Vector3f targetDir = loc.subtract(physicsLocation).normalize();
float dot = linearVelocity.normalize().dot(targetDir); // 1 moving towards target, -1 moving away from target

// If we are too near and too slow, stop and pop state
Vector3f hybnost = linearVelocity.mult(mass).mult(1 / tpf);
if(hybnost.length() &lt; (mass / tpf) &amp;&amp; actualDistance &lt; criticalApproachingDistance) {
rigidBody.applyCentralForce(hybnost.negate());
ai.server.broadcast(new ChatMessage(ai.name + ": destination reached: " + Vector3fHelper.round(physicsLocation, 2)));
ai.popState();
} else {
// Determine if we are in slowing area
if(dot &lt; 0 || actualDistance &gt;= brakingDistance) {
// We can accelerate
rigidBody.applyCentralForce(steering);
} else {
// We are in braking distance
rigidBody.applyCentralForce(steering.negateLocal());
}
}
}


[/java]

and i should add some dot product to this mess