Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Merge #1442
Browse files Browse the repository at this point in the history
1442: Add function to transform position r=jojolepro,Moxinilian,azriel91 a=vessd

## Description

Function to transform position from screen space into world space

## Additions

- `fn screen_to_world`


Co-authored-by: Sergey Veselkov <veselkovsd@yandex.ru>
  • Loading branch information
bors[bot] and vessd committed Mar 7, 2019
2 parents 4a86f02 + 7809bc7 commit c31ea06
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 25 additions & 1 deletion amethyst_renderer/src/cam.rs
Expand Up @@ -3,12 +3,15 @@
use amethyst_assets::PrefabData;
use amethyst_core::{
ecs::prelude::{Component, Entity, HashMapStorage, Write, WriteStorage},
math::{Matrix4, Orthographic3, Perspective3},
math::{Matrix4, Orthographic3, Perspective3, Point2, Point3},
GlobalTransform,
};
use amethyst_error::Error;

use serde::{Deserialize, Serialize};

use crate::ScreenDimensions;

/// The projection mode of a `Camera`.
///
/// TODO: Remove and integrate with `Camera`.
Expand Down Expand Up @@ -76,6 +79,27 @@ impl Camera {
std::f32::consts::FRAC_PI_3,
))
}

/// Transforms position from screen space to camera space
pub fn position_from_screen(
&self,
screen_position: Point2<f32>,
camera_transform: &GlobalTransform,
screen_dimensions: &ScreenDimensions,
) -> Point3<f32> {
let screen_x = 2.0 * screen_position.x / screen_dimensions.width() - 1.0;
let screen_y = 1.0 - 2.0 * screen_position.y / screen_dimensions.height();
let screen_point = Point3::new(screen_x, screen_y, 0.0).to_homogeneous();

let vector = camera_transform.0
* self
.proj
.try_inverse()
.expect("Camera projection matrix is not invertible")
* screen_point;

Point3::from_homogeneous(vector).expect("Vector is not homogeneous")
}
}

impl Component for Camera {
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@ it is attached to. ([#1282])
* Add `SpriteRenderPrefab`. ([#1435])
* Add `ScreenSpace` component. Draws entities using the screen coordinates. ([#1424])
* Add `add_removal_to_entity` function. ([#1445])
* Add `position_from_screen` to `Camera`. Transforms position from screen space to camera space. ([#1442])

### Changed

Expand Down Expand Up @@ -94,6 +95,7 @@ extra bounds from `AnimatablePrefab` and `AnimationSetPrefab` ([#1435])
[#1451]: https://github.com/amethyst/amethyst/pull/1451
[#1452]: https://github.com/amethyst/amethyst/pull/1452
[#1454]: https://github.com/amethyst/amethyst/pull/1454
[#1442]: https://github.com/amethyst/amethyst/pull/1442

## [0.10.0] - 2018-12

Expand Down

0 comments on commit c31ea06

Please sign in to comment.