#change translation vector

12 messages · Page 1 of 1 (latest)

lavish sage
#

Hi, I have two spritesheets, and want to spawn them using a function.

I have a list with the images and use a for loop to spawn. And also have a tuple with two vector to use as a position.

I've tried to use a variable i to change the vector in translate:
translation: Vec3::new(pos.i.0, pos.i.1, pos.i.2),

and get this error.

no field i on type (({float}, {float}, {float}), ({float}, {float}, {float}))

fn spawn_bird(
    mut commands: Commands, 
    asset_server: Res<AssetServer>,
    mut texture_atlases: ResMut<Assets<TextureAtlas>>,){
    
    let atlases = [ "pajaro.png", "pajaro_loco.png"];
    let pos = ((0.,0.,0.) , (50.,0.,0.));
    
    for texture in atlases {

        let mut sprite = TextureAtlasSprite::new(0);
        sprite.custom_size = Some(Vec2::splat(1.0));

        let image = asset_server.load(texture);
        let atlas = TextureAtlas::from_grid(image, Vec2::splat(32.0), 3, 1);
        let atlas_handle = texture_atlases.add(atlas);
        
        commands.spawn_bundle(SpriteSheetBundle{
            texture_atlas:atlas_handle,
            sprite: sprite,
            transform: Transform {
                translation: Vec3::new(pos.i.0, pos.i.1, pos.i.2),
                scale: Vec3::new(SCALE,SCALE,SCALE),
                ..Default::default()
                },
            ..Default::default()
        
        })
        .insert(AnimationTimer(Timer::from_seconds(0.2, true)));                                         
        
    }
quartz widget
#

are you sure it's not 0? as in pos.0.0, pos.0.1

hexed lion
#

There is no i anywhere in your code there. You could do something like

for (i, texture) in atlases.iter().enumerate() {

To get an index while you're iterating, but it's not something that just comes automatically.

The next problem though is that you can't index into a tuple with a variable like that. Just not something you can do with tuples. But you could store the positions in an array instead just like you do with the texture names.

#

A couple other notes while I'm here:

#

You shouldn't mess with the z-scale in 2d. You're bound to end up with sprites that mysteriously disappear or don't layer properly.

Vec2::splat(SCALE).extend(0.)

That would scale your sprite in 2d and give it a z-value / layer of 0.

#

A cool thing you can do when building structs is instead of

            texture_atlas:atlas_handle,
            sprite: sprite,

Doing

            texture_atlas,
            sprite,

if the struct field name matches the variable, you don't have to repeat it.

#

Also, why store a tuple of positions if you're just going to make a Vec3 later? You could use a Vec3 to start with.

#

And a final thing, if you run cargo fmt to format your code before sharing, it'll help out those that are trying to help. Its use is extremely popular in the rust ecosystem.

And cargo clippy will have give you some suggestions to improve your code and possibly catch mistakes for you.

lavish sage
#

" There is no i anywhere in your code there. You could do something like

for (i, texture) in atlases.iter().enumerate() { "

Try this, didn't work don't remenber the error. I'll try again.

lavish sage
lavish sage
lavish sage