Skip to content

Commit

Permalink
backup: Ground collision experimentation #2
Browse files Browse the repository at this point in the history
  • Loading branch information
uggla committed May 8, 2024
1 parent 743a742 commit 62c2619
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 116 deletions.
15 changes: 14 additions & 1 deletion assets/level01.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="240" height="90" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="5">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="240" height="90" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="13">
<tileset firstgid="1" name="tileset-1" tilewidth="16" tileheight="16" tilecount="2030" columns="58">
<image source="tileset-1.png" width="928" height="560"/>
</tileset>
Expand Down Expand Up @@ -198,5 +198,18 @@
<object id="4" name="Ground" x="0" y="320">
<polyline points="0,0 0,272 1328,272 1552,160 1600,160 1712,272 3232,272 3232,336"/>
</object>
<object id="5" name="Rect" x="0" y="160" width="160" height="64"/>
<object id="6" name="Point" x="192" y="160">
<point/>
</object>
<object id="9" name="Elipse" x="256" y="192" width="128" height="128">
<ellipse/>
</object>
<object id="10" name="Polygon" x="480" y="208">
<polygon points="0,0 32,32 32,48 -32,48 -32,32"/>
</object>
<object id="12" name="Text" x="544" y="208" width="87.7188" height="23">
<text wrap="1">Hello World</text>
</object>
</objectgroup>
</map>
264 changes: 158 additions & 106 deletions src/ground_platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy_rapier2d::{
dynamics::{Ccd, ExternalImpulse, GravityScale, RigidBody},
geometry::{Collider, Restitution},
};
use tiled::ObjectShape;

use crate::{
helpers::tiled::TiledMap,
Expand Down Expand Up @@ -32,18 +33,62 @@ pub struct Ground;
#[derive(Component)]
pub struct Platform;

fn get_ground_layer(tiled_map: &TiledMap) {
for layer in tiled_map.map.layers() {
if layer.name == "Ground" {
info!("Found Ground layer");
if let tiled::LayerType::Objects(object_data) = layer.layer_type() {
for object in object_data.objects() {
info!("Found object {:?}", object.name);
info!("Found object {:?}", object.shape);
}
}
fn get_ground_layer(commands: &mut Commands, tiled_map: &TiledMap, level: &Level) {
tiled_map.map.layers().for_each(|layer| {
if layer.name != "Ground" {
return;
}
}

info!("Found Ground layer");

let object_data = match layer.layer_type() {
tiled::LayerType::Objects(object_data) => object_data,
_ => return,
};

object_data.objects().for_each(|object| {
info!("Found object {:?}", object.name);
debug!("Found object {:?}", object.shape);

// match &object.shape {
// ObjectShape::Rect { width, height } => {
// todo!();
// }
// ObjectShape::Polygon { points } => {
// todo!();
// }
// ObjectShape::Polyline { points } => {
// let points: Vec<Vec2> = points
// .iter()
// .map(|(x, y)| {
// level.map.tiled_to_bevy_coord(
// Vec2::new(*x, *y) + Vec2::new(object.x, object.y),
// )
// })
// .collect();
//
// debug!("points: {:?}", points);
//
// commands
// .spawn((
// SpatialBundle::default(),
// Collider::polyline(points, None),
// Ground,
// ))
// .insert(Ccd::enabled());
// }
// ObjectShape::Text { .. } => {
// todo!();
// }
// ObjectShape::Ellipse { .. } => {
// todo!();
// }
// ObjectShape::Point { .. } => {
// todo!();
// }
// }
})
})
}

fn setup_ground_platforms(
Expand All @@ -52,102 +97,109 @@ fn setup_ground_platforms(
current_level: Res<CurrentLevel>,
levels: Query<&Level, With<Level>>,
) {
for level in levels.iter() {
if level.id == current_level.id {
if let Some(tiled_map) = assets.get(&level.handle) {
// dbg!(&tiled_map.map);
get_ground_layer(tiled_map);
}
info!("setup_ground_platforms");

levels.iter().for_each(|level| {
if level.id != current_level.id {
return;
}
}
/* Create the ground. */

info!("setup_physics");
let points = vec![
Vec2::new(-1280.0 * 2.0 * 3.0, 128.0),
// Vec2::new(48.0, -8.0 - 224.0),
// Vec2::new(48.0 + 14.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)),
// Vec2::new(272.0 + 3.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)),
// Vec2::new(320.0 + 7.0 * 16.0, -8.0 - (224.0)),
Vec2::new(1280.0 / 2.0 * 2.0 + 30.0, 128.0),
];

commands
.spawn((
SpatialBundle::default(),
Collider::polyline(points, None),
Ground,
))
.insert(Ccd::enabled())
// .insert(TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)))
.with_children(|parent| {
// Create 2 x test platforms
parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
1280.0 / 2.0 * 2.0,
-324.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
1280.0 / 2.0 * 2.0,
-724.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
1280.0 / 2.0 * 2.3,
-224.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
1280.0 / 2.0 * 2.5,
-124.0 + 3.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
1280.0 / 2.0 * 2.4,
-024.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

parent
.spawn((Collider::cuboid(60.0, 5.0), Platform))
.insert(TransformBundle::from(Transform::from_xyz(
-1280.0 / 2.0 + 380.0, // 270 Gap is reachable, 290 seems not
-224.0 + 10.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
0.0,
)));

/* Create the bouncing ball. */
parent
.spawn(RigidBody::Dynamic)
.insert(GravityScale(20.0))
.insert(Collider::ball(20.0))
.insert(Restitution::coefficient(0.0))
// .insert(ColliderMassProperties::Density(20.0))
.insert(ExternalImpulse {
// impulse: Vec2::new(100.0, 200.0),
// torque_impulse: 14.0,
..default()
})
// .insert(Damping {
// linear_damping: 100.0,
// angular_damping: 0.0,
// })
.insert(TransformBundle::from(Transform::from_xyz(0.0, 400.0, 0.0)));
});

let tiled_map = match assets.get(&level.handle) {
Some(tiled_map) => tiled_map,
None => return,
};

get_ground_layer(&mut commands, tiled_map, level);
});

// /* Create the ground. */
//
// info!("setup_physics");
// let points = vec![
// Vec2::new(-1280.0 * 2.0 * 3.0, 128.0),
// // Vec2::new(48.0, -8.0 - 224.0),
// // Vec2::new(48.0 + 14.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)),
// // Vec2::new(272.0 + 3.0 * 16.0, -8.0 - (224.0 - 7.0 * 16.0)),
// // Vec2::new(320.0 + 7.0 * 16.0, -8.0 - (224.0)),
// Vec2::new(1280.0 / 2.0 * 2.0 + 30.0, 128.0),
// ];
//
// commands
// .spawn((
// SpatialBundle::default(),
// Collider::polyline(points, None),
// Ground,
// ))
// .insert(Ccd::enabled())
// // .insert(TransformBundle::from(Transform::from_xyz(0.0, 0.0, 0.0)))
// .with_children(|parent| {
// // Create 2 x test platforms
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// 1280.0 / 2.0 * 2.0,
// -324.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// 1280.0 / 2.0 * 2.0,
// -724.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// 1280.0 / 2.0 * 2.3,
// -224.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// 1280.0 / 2.0 * 2.5,
// -124.0 + 3.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// 1280.0 / 2.0 * 2.4,
// -024.0 + 5.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// parent
// .spawn((Collider::cuboid(60.0, 5.0), Platform))
// .insert(TransformBundle::from(Transform::from_xyz(
// -1280.0 / 2.0 + 380.0, // 270 Gap is reachable, 290 seems not
// -224.0 + 10.0 * 16.0, // 8.0 is hard to climb, 9.0 can not be climbed
// 0.0,
// )));
//
// /* Create the bouncing ball. */
// parent
// .spawn(RigidBody::Dynamic)
// .insert(GravityScale(20.0))
// .insert(Collider::ball(20.0))
// .insert(Restitution::coefficient(0.0))
// // .insert(ColliderMassProperties::Density(20.0))
// .insert(ExternalImpulse {
// // impulse: Vec2::new(100.0, 200.0),
// // torque_impulse: 14.0,
// ..default()
// })
// // .insert(Damping {
// // linear_damping: 100.0,
// // angular_damping: 0.0,
// // })
// .insert(TransformBundle::from(Transform::from_xyz(0.0, 400.0, 0.0)));
// });
}

fn print_ball_altitude(positions: Query<&Transform, With<RigidBody>>) {
Expand Down
18 changes: 9 additions & 9 deletions src/screen_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Map {
pub fn tiled_to_bevy_coord(&self, tiled_coord: Vec2) -> Vec2 {
Vec2::new(
tiled_coord.x - (self.width / 2) as f32,
(tiled_coord.y - (self.height / 2) as f32) + 1f32,
-(tiled_coord.y - (self.height / 2) as f32) - 1f32,
)
}

Expand Down Expand Up @@ -330,19 +330,19 @@ mod tests {

assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(0.0, 0.0)),
Vec2::new(-1920.0, -1079.0)
Vec2::new(-1920.0, 1079.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(1919.0, 0.0)),
Vec2::new(-1.0, -1079.0)
Vec2::new(-1.0, 1079.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(0.0, 719.0)),
Vec2::new(-1920.0, -360.0)
Vec2::new(-1920.0, 360.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(3839.0, 2159.0)),
Vec2::new(1919.0, 1080.0)
Vec2::new(1919.0, -1080.0)
);
}

Expand All @@ -356,19 +356,19 @@ mod tests {

assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(0.0, 0.0)),
Vec2::new(-1280.0, -359.0)
Vec2::new(-1280.0, 359.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(1280.0, 360.0)),
Vec2::new(0.0, 1.0)
Vec2::new(0.0, -1.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(0.0, 719.0)),
Vec2::new(-1280.0, 360.0)
Vec2::new(-1280.0, -360.0)
);
assert_eq!(
map.tiled_to_bevy_coord(Vec2::new(2559.0, 719.0)),
Vec2::new(1279.0, 360.0)
Vec2::new(1279.0, -360.0)
);
}

Expand Down

0 comments on commit 62c2619

Please sign in to comment.