world_to_viewport works incorrectly? and a few other questions about the coordinate system #9210
-
QuestionsWhich coordinate systems does Bevy use? and what are their descriptionsAs far as I know, those are And are the local coordinates applicable only to the children of the camera? Or Not? how to get world | viewport coordinates from local coordinates?🔢Main Question(tested in 0.10, 0.11)For some reason, world_to_viewport, viewport_to_world_2d and others calculates coordinates relative to top-right corner of the screen world_to_viewport points to the upper right corner when converting [0.0, 0.0], but was supposed to show the upper left corner according to some info about the system:
Code which confirms my words use bevy::prelude::*;
fn main() {
App::new().add_plugins(DefaultPlugins).add_systems(Update,get_space_center).add_systems(Startup, create_camera).run();
}
fn create_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn get_space_center(mut commands:Commands,asset_server:Res<AssetServer>, query: Query<(&Camera,&GlobalTransform)>) {
let camera = query.get_single().unwrap();
let cords=camera.0.world_to_viewport(camera.1, Vec3::new(0.0,0.0,0.0)).unwrap();
commands.spawn(SpriteBundle {
texture: asset_server.load("planet00.png"),
transform:Transform::from_translation(Vec3::new(cords.x,cords.y,0.0)),
..Default::default()
});
dbg!(cords);
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Right-handed, x-right, y-up, z-back and cameras look along -z. |
Beta Was this translation helpful? Give feedback.
-
Methods like If you want to place an image relative to the window you probably want to take a look at the ui examples or |
Beta Was this translation helpful? Give feedback.
Methods like
world_to_viewport
convert between 'viewport space' and 'world space'; they don't convert a world space position to another world space position that is relative to the viewport.world_to_viewport
is useful when you want to position a UI element (which are positioned in 'viewport space') relative to something in world space (like a sprite).If you want to place an image relative to the window you probably want to take a look at the ui examples or
ImageBundle
specifically.