bevyengine / bevyengine/bevy

Spherical Vec3 Representation in bevy::math

Open
#11,004 6 comments 0 reactions 0 assignees View on GitHub
A-Math A-Transform C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

It can be easier to reason about transforms in terms of spherical coordinates.

For example, say I want to position a camera facing the +x, -z quadrant (where the camera is in the -x, +z quadrant). The camera should be rotated 225 degrees on the xz-plane from the +x-axis, and 30 degrees above the xz plane relative to the origin. The camera should be 5 units away from the origin.

Doing something like this with the current api isn't very trivial.

```rust
let mut transform = Transform::from_xyz(5., 0., 0.);
transform.rotate_y(5 * PI / 4);
// find the axis perpendicular to the current transform on the xz plane
// rotate around that axis 30 degrees
```

Doing this via spherical coordinates is a one liner

```rust
Transform::from_spherical(5.0, PI / 3, 5 * PI / 4);
```

This makes writing a camera for a 3d platformer easy.

## What solution would you like?

At the very least a ```from_spherical_coord(rho: f32, phi: f32, theta: f32)``` function in Transform.

Alternatively an explicit SphericalVec3 either in bevy::math or glam itself might be helpful.

```rust
struct SphericalVec3 {
/// Distance from origin
rho: f32,
/// Angle from the +y-axis
phi: f32,
/// Angle from the +x-axis
theta: f32,
}
```

## What alternative(s) have you considered?

People can write this themselves or hack around it using the current bevy_math primitives. There's also https://crates.io/crates/lina which seems to have support for spherical coords.

## Prior Art

Unreal 4 seems to support something related as part of their core math library: https://docs.unrealengine.com/4.27/en-US/API/Runtime/Core/Math/FVector/UnitCartesianToSpherical/

Contributor guide

Open the contributing guide

Research direction

Start with the bevy::math Transform API and its existing math primitives, then compare the issue’s rho, phi, and theta definitions with the camera example. Done means an agreed spherical-coordinate API or representation whose behavior matches those angle conventions and supports the stated camera use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.