Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignored entity pivot #294

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d9bc287
Use un-pivoted conversion for entity translation
Koopa1018 Jan 16, 2024
985e9b7
Make to_translation_pivoted more readable
Koopa1018 Jan 16, 2024
a7a3de0
Update tests to match new translation behavior
Koopa1018 Jan 16, 2024
dfc0089
Use if-let in sprite_sheet_bundle_from_entity_info
Koopa1018 Jan 20, 2024
f5114bf
Set sprite anchor from entity pivot
Koopa1018 Jan 20, 2024
8c87d95
Fix traitless example
Koopa1018 Jan 20, 2024
071b2e8
Dedicated fn for finding entity center
Koopa1018 Jan 20, 2024
16fbf92
First draft of pivot behavior migration guide
Koopa1018 Jan 20, 2024
7a52bbb
rustfmt my changes
Koopa1018 Jan 24, 2024
7ca46e9
Actually use pivot.y for the Y axis
Koopa1018 Jan 30, 2024
cec7cd8
Use clearer wording in pivot migration guide
Koopa1018 Jan 30, 2024
6f8ae88
Clarify migration guide further
Koopa1018 Jan 30, 2024
653f115
Make pivot->anchor conversion a fn
Koopa1018 Jan 30, 2024
15113f8
Mention pivot->anchor fn in migration guide
Koopa1018 Jan 30, 2024
b0e36c5
Rename entity_center for style consistency
Koopa1018 Jan 30, 2024
1e71e0b
Document center-finding function
Koopa1018 Jan 30, 2024
72fc906
Merge branch 'fix/entity-pivot' of https://github.com/Koopa1018/bevy_…
Koopa1018 Jan 30, 2024
1ba503c
Add diagram of pivot behavior between versions
Koopa1018 Jan 30, 2024
ac66fe3
Apply pivot to non-sheet sprite bundle too
Koopa1018 Jan 30, 2024
2274b42
Rustfmt today's changes
Koopa1018 Jan 30, 2024
5a237d1
Simple test suite for ldtk_pivot_to_anchor
Koopa1018 Jan 31, 2024
633e4ad
Rustfmt new test suite
Koopa1018 Jan 31, 2024
479e3a2
Move pivot diagram next to migration guide
Koopa1018 Jan 31, 2024
8d1ae7a
Clarify pivot diagram
Koopa1018 Feb 1, 2024
284f5c1
Move pivot migration guide to new document
Koopa1018 Mar 6, 2024
bbf25bc
Update pivot diagram for version differences
Koopa1018 Mar 6, 2024
7d227c8
Merge remote-tracking branch 'upstream/main' into fix/entity-pivot
Koopa1018 Jul 21, 2024
b3d56de
Merge branch 'main' into fix/entity-pivot
Koopa1018 Jul 21, 2024
0a9c304
Reorganize platformer example
Koopa1018 Jul 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ pub fn calculate_transform_from_entity_instance(

let size = IVec2::new(entity_instance.width, entity_instance.height);

let translation = ldtk_pixel_coords_to_translation_pivoted(
let translation = ldtk_pixel_coords_to_translation(
Koopa1018 marked this conversation as resolved.
Show resolved Hide resolved
entity_instance.px,
level_height,
size,
entity_instance.pivot,
);
let scale = size.as_vec2() / def_size.as_vec2();

Expand Down Expand Up @@ -227,13 +225,16 @@ pub fn ldtk_pixel_coords_to_translation_pivoted(
entity_size: IVec2,
pivot: Vec2,
) -> Vec2 {
let pivot_point = ldtk_coord_conversion(ldtk_coords, ldtk_pixel_height).as_vec2();
// Find entity's LDtk-defined position.
let translation = ldtk_coord_conversion(ldtk_coords, ldtk_pixel_height).as_vec2();

// Adjust entity pivot so that 0.5 is the center.
let adjusted_pivot = Vec2::new(0.5 - pivot.x, pivot.y - 0.5);

// Find the spot on the entity which the pivot corresponds to.
let offset = entity_size.as_vec2() * adjusted_pivot;

pivot_point + offset
translation + offset
}

/// Similar to [LayerBuilder::new_batch], except it doesn't consume the [LayerBuilder]
Expand Down Expand Up @@ -462,22 +463,22 @@ mod tests {
};
let result =
calculate_transform_from_entity_instance(&entity_instance, &entity_definition_map, 320);
assert_eq!(result, Transform::from_xyz(272., 48., 0.));
assert_eq!(result, Transform::from_xyz(256., 64., 0.));

// difficult case
let entity_instance = EntityInstance {
px: IVec2::new(40, 50),
def_uid: 2,
width: 30,
height: 50,
def_uid: 2, // default size: (10, 25)
width: 30, // spawned with triple size on X
height: 50, // double size on Y
pivot: Vec2::new(1., 1.),
..Default::default()
};
let result =
calculate_transform_from_entity_instance(&entity_instance, &entity_definition_map, 100);
assert_eq!(
result,
Transform::from_xyz(25., 75., 0.).with_scale(Vec3::new(3., 2., 1.))
Transform::from_xyz(40., 50., 0.).with_scale(Vec3::new(3., 2., 1.))
);
}

Expand Down Expand Up @@ -517,7 +518,7 @@ mod tests {
calculate_transform_from_entity_instance(&entity_instance, &entity_definition_map, 100);
assert_eq!(
result,
Transform::from_xyz(32., 68., 0.).with_scale(Vec3::new(4., 2., 1.))
Transform::from_xyz(64., 36., 0.).with_scale(Vec3::new(4., 2., 1.))
);
}

Expand Down
Loading