#help with query

1 messages · Page 1 of 1 (latest)

cloud rivet
#

I have a system to move a car but i also have one that make the camera follow the car.

fn car_move(mut query: Query<(&mut Car, &mut Transform)>,...)
{
  for (mut car, mut trans) in query.iter_mut() {...}
}```
the thing is the movement is laggy, i guess when i ask for the Transform it also takes the camera one ? So i've tried this one: 
```rust
fn car_move(mut query: Query<(&mut Car, (&mut Transform, (With<Car>, Without<Camera>)))>,...)
{
  for (mut car, mut trans) in query.iter_mut() {...}
}```
but it tells me there is a problem with the type of `trans` like the methods i use don't work
#
error[E0599]: no method named `rotate_z` found for tuple `(Mut<'_, bevy::prelude::Transform>, ((), ()))` in the current scope
  --> src/main.rs:64:15
   |
64 |         trans.rotate_z(rotation_factor * car.rotation_speed * time.delta_seconds());
   |               ^^^^^^^^ method not found in `(Mut<'_, Transform>, ((), ()))`

error[E0609]: no field `rotation` on type `(Mut<'_, bevy::prelude::Transform>, ((), ()))`
  --> src/main.rs:65:40
   |
65 |         let movement_direction = trans.rotation * Vec3::Y;
   |                                        ^^^^^^^^

error[E0609]: no field `translation` on type `(Mut<'_, bevy::prelude::Transform>, ((), ()))`
  --> src/main.rs:79:15
   |
79 |         trans.translation += translation_delta * movement_direction * 3. * time.delta_seconds();
   |               ^^^^^^^^^^^

Some errors have detailed explanations: E0599, E0609.
For more information about an error, try `rustc --explain E0599`.```
ornate arch
#

I think that error is caused by your malformed query. I would change the query to:
mut query: Query<&mut Transform, With<Car>>
This will only grab Transform of entities with Car Component.
And then for the loop you can just do:
for mut trans in query.iter_mut() { /*code to change said transform*/}

#

Or if you know you will have only 1 entity with Car then you can forgot loop and just do let trans = &mut query.get_single_mut()/let trans = &mut query.single_mut()

cloud rivet
#

but i need car attributes, so if i remove it from the query i'll not be able to use it

#

?

vast ore
#

let trans = &mut query.single_mut().unwrap() don't forget the unwrap
Query<(&mut Car, &mut Transform)> if you want to gt the Car component, and modify it
Query<&mut Transform, With<Car>> if Car is a markr component

#

Your problem may be that the system that updats the camera position does not come after the system that updates the car position?

ornate arch
#

you can do this:
mut query: Query<(&mut Transform, &Car>)>

vast ore
#

Query<(&mut Car, &mut Transform)> shouldn't return the transform of the camera except if the camera has the Car component

cloud rivet
vast ore
#

You can, or you can explicitely order them

#

When you add your systems, do something like

app.add_system(update_car_transform)
   .add_system(update_camera_transform.after(update_car_transform))
vast ore
#

or update both at once in a single system, with two different queries

#

Also are you updating the camera based on the Car's Transform or GlobalTransform?

#

Also, a simpler way to do this would probably be to spawn the camera as a child of the car, that way you just specify a transform of its position relative to the Car, and bevy's hierarchy system will update its GlobalTransform automatically

cloud rivet
#

i'm really new and i don't get the child principle

vast ore
#

Okay, so when you spawn an entity, you can spawn it by itself, or you can say "this new entity will be a child of this one", what it does basically is that the child's Transform does not represent its absolute position in space anymore, but instead represents its position relative to its parent's position

cloud rivet
vast ore
#

The absolute position is space is actually GlobalTransform and is calculated by Bevy automatically based on Transform, and if the entity is a child of another one or not

cloud rivet
#

okay and is it only about Transform?

vast ore
# cloud rivet okay and is it only about Transform?

Hierarchy (parent/child relationship) affects three things (to my knowledge):

  • Transform/GlobalTransform
  • Visibility/ComputedVisibility (same thing, ComputedVisibility is the real value indicating if an entity is visible or not, but is calculated by bevy, and a children can have its visibility inherited from its parent)
  • It's possible to despawn all the children of an entity
#

So if you want an entity to always have the same position relative to another entity, you can do it using a system like you did, but it's usually easier to just spawn it as a child of the entity it follows

cloud rivet
vast ore
#

Except if you want more complex logic yes

#

If you just want the camera to always be 10 meters behind the car at all times

vivid siren
#

BTW regarding the original question, since I don't see an exhaustive answer:

In your original query type Query<(&mut Car, (&mut Transform, (With<Car>, Without<Camera>)))> what was wrong was that you put the With and Without filters in the first generic parameter of Query.
Query has 2 parameters with the second being optional. The first one lists the component you want access to (like &mut Car and &mut Transform), the second lists the filters (like With<Car> and Without<Camera>). And if you want multiple components/filters you put them in a tuple, but still in the right position!

So, if you look at the original query, and in particular at the parenthesis, you'll see that everything is in the first parameter. If you move the filters to the second argument you would have Query<(&mut Car, &mut Transform), (With<Car>, Without<Camera>)>
Note that the With<Car> is redundant since getting &mut Car implies that the entity must have a Car component.

Hope this clears up how to express queries!

vast ore
#

(also btw you can change a child's transform, it will just move it relative to its parent)

cloud rivet
#

If you move the filters to the second argument you would have Query<(&mut Car, &mut Transform), (With<Car>, Without<Camera>)>
so if i just change my query to this one i'll have nothing else to change ?

vast ore
#

I think the problem you had was that you camera update system was running before your car update one, so it's unrelated, it's not that you had the the Transform of the Camera returned by the query.
Giuschi was commenting on proper Query syntax

#

Basically a query should look like Query<(Stuff, You, Need), (With<Stuff>, With<MoreStuff>, Without<Stuff>)> the parenthesis are important, you need to separate the "stuff I want to get" and the "with/withouts" (and if you have only one in those categories then don't put the parenthesis)

cloud rivet
#

okay i get it

#

thank you all