Repository: godotengine/godot
File: scene/3d/spring_arm_3d.cpp (C++)

Code before:
real_t SpringArm3D::get_hit_length() {
	return current_spring_length;
}

void SpringArm3D::process_spring() {
	// From
	real_t motion_delta(1);
	real_t motion_delta_unsafe(1);

	Vector3 motion;
	const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));

	if (shape.is_null()) {
		motion = Vector3(cast_direction * (spring_length));
		PhysicsDirectSpaceState3D::RayResult r;
		bool intersected = get_world_3d()->get_direct_space_state()->intersect_ray(get_global_transform().origin, get_global_transform().origin + motion, r, excluded_objects, mask);
		if (intersected) {
			real_t dist = get_global_transform().origin.distance_to(r.position);
			dist -= margin;
			motion_delta = dist / (spring_length);
		}
	} else {
		motion = Vector3(cast_direction * spring_length);
		get_world_3d()->get_direct_space_state()->cast_motion(shape->get_rid(), get_global_transform(), motion, 0, motion_delta, motion_delta_unsafe, excluded_objects, mask);
	}

	current_spring_length = spring_length * motion_delta;
	Transform3D childs_transform;
	childs_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);

	for (int i = get_child_count() - 1; 0 <= i; --i) {
		Node3D *child = Object::cast_to<Node3D>(get_child(i));
		if (child) {
			childs_transform.basis = child->get_global_transform().basis;
			child->set_global_transform(childs_transform);
		}
	}
}


Review comment (suggestion):
```suggestion
			// Use camera rotation, but spring arm position.
```

Code after:

real_t SpringArm3D::get_hit_length() {
	return current_spring_length;
}

void SpringArm3D::process_spring() {
	// From
	real_t motion_delta(1);
	real_t motion_delta_unsafe(1);

	Vector3 motion;
	const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));

	motion = Vector3(cast_direction * (spring_length));

	if (shape.is_null()) {
		Camera3D *camera = nullptr;
		for (int i = get_child_count() - 1; 0 <= i; --i) {
			camera = Object::cast_to<Camera3D>(get_child(i));
			if (camera) {
				break;
			}
		}

		if (camera != nullptr) {
			//use camera rotation, but spring arm position
			Transform3D base_transform = camera->get_global_transform();
			base_transform.origin = get_global_transform().origin;
			get_world_3d()->get_direct_space_state()->cast_motion(camera->get_pyramid_shape_rid(), base_transform, motion, 0, motion_delta, motion_delta_unsafe, excluded_objects, mask);
		} else {
			PhysicsDirectSpaceState3D::RayResult r;
			bool intersected = get_world_3d()->get_direct_space_state()->intersect_ray(get_global_transform().origin, get_global_transform().origin + motion, r, excluded_objects, mask);
			if (intersected) {
				real_t dist = get_global_transform().origin.distance_to(r.position);
				dist -= margin;
				motion_delta = dist / (spring_length);
			}
		}
	} else {
		get_world_3d()->get_direct_space_state()->cast_motion(shape->get_rid(), get_global_transform(), motion, 0, motion_delta, motion_delta_unsafe, excluded_objects, mask);
	}

	current_spring_length = spring_length * motion_delta;
	Transform3D childs_transform;
	childs_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);

	for (int i = get_child_count() - 1; 0 <= i; --i) {
		Node3D *child = Object::cast_to<Node3D>(get_child(i));
		if (child) {
			childs_transform.basis = child->get_global_transform().basis;
			child->set_global_transform(childs_transform);