Hey again,
Making some good progress with the library but I've run into an issue (bug?) when modifying some variables of the Vehicle class at runtime.
We need a system that can generate a random vehicle (wheelbase/wheel size/suspension) when the player taps a button. I previously implemented a few simple functions that access the Vehicle's List of <Wheels> and then subsequent shape/suspension data to modify the values on the fly. This worked perfectly fine up until the vehicle is placed on a flat terrain collision mesh or a flat box collider..
- When the terrain collider is uneaven (even to just heights of 0.002f variance) the vehicle will regenerate and behave normally with the new settings.
- When the terrain collider is created with flat values, the suspension glitches out and just bounces continually.
I have just implemented a work around that destroys the vehicle and re-creates it, but it's messy and you get that awkward 1 frame gap of having a vehicle, no vehicle and then a new one - instead of neatly switching.
I thought it may be something to do with my implementation of the space or scale, but i quickly tried it in the Terrain.cs demo in the source downloads and it did a similar thing. (As an aside, we have a requirement in the project that the terrain is to be completely flat)
Any ideas on this?
Cheers
Regenerating Vehicle/Wheel data on the fly?
Re: Regenerating Vehicle/Wheel data on the fly?
This is caused by the WheelShape's detector not being updated to match the new state. Regenerating the vehicle recreates the detector, so it works correctly. To eliminate the bouncey effect, the wheel shape should be reinitialized after changing any property in the wheel which would affect the detector size.
The fix is implemented in the latest source version.
Side notes:
1) If the AABB of the detector overlaps an the AABB of an object, it will be tested. A large terrain object has an AABB which encompasses its whole shape. Even slight variation would tend to make the AABB large enough to intersect the detector's incorrectly sized AABB, hence the observations.
2) There should be no need for a frame gap when recreating a vehicle; it can be recreated instantly.
3) The Vehicle is a little bit ancient. It could use some significant improvements in behavior, flexibility, and performance. This bug was a byproduct of my hesitation to spend too much time on a system which was likely going to be rewritten from scratch. If you are working on a long term project (more than a few months), I would recommend not coupling your code too tightly with the details of the current Vehicle implementation.
The fix is implemented in the latest source version.
Side notes:
1) If the AABB of the detector overlaps an the AABB of an object, it will be tested. A large terrain object has an AABB which encompasses its whole shape. Even slight variation would tend to make the AABB large enough to intersect the detector's incorrectly sized AABB, hence the observations.
2) There should be no need for a frame gap when recreating a vehicle; it can be recreated instantly.
3) The Vehicle is a little bit ancient. It could use some significant improvements in behavior, flexibility, and performance. This bug was a byproduct of my hesitation to spend too much time on a system which was likely going to be rewritten from scratch. If you are working on a long term project (more than a few months), I would recommend not coupling your code too tightly with the details of the current Vehicle implementation.
Re: Regenerating Vehicle/Wheel data on the fly?
Ahh, this clears a few things up thanks
[EDIT] i must be tired.. realised that the fix you mentioned was in fact brand new! thank you very much.. i guess you can ignore the rest of this heh
You mentioned recreating the wheel shape would reinitialise the entire wheel (including the detector causing the problems when only updating the radius) - this reminds me, on my first attempt I implemented a function that re-created the WheelShape component of the Wheel, however this sprung a null reference exception for something like a connector or local attatchment point (I'll dig it out).
So this being said, should it be possible to say vehicle.Wheels[0].Shape = new CylinderCastWheelShape(new params) - because this currently throws the null reference exception (ill get that for you shortly).
As an aside - do you have recommendations as to recreating the vehicle? Or is there a neat way to say myVehicle = new PhysicsVehicle without having to first remove all it's bodies from the space, then recreate it? (Something in the demo code you can point me towards maybe?)
Thanks for your help!
[EDIT] i must be tired.. realised that the fix you mentioned was in fact brand new! thank you very much.. i guess you can ignore the rest of this heh
You mentioned recreating the wheel shape would reinitialise the entire wheel (including the detector causing the problems when only updating the radius) - this reminds me, on my first attempt I implemented a function that re-created the WheelShape component of the Wheel, however this sprung a null reference exception for something like a connector or local attatchment point (I'll dig it out).
So this being said, should it be possible to say vehicle.Wheels[0].Shape = new CylinderCastWheelShape(new params) - because this currently throws the null reference exception (ill get that for you shortly).
As an aside - do you have recommendations as to recreating the vehicle? Or is there a neat way to say myVehicle = new PhysicsVehicle without having to first remove all it's bodies from the space, then recreate it? (Something in the demo code you can point me towards maybe?)
Thanks for your help!
Re: Regenerating Vehicle/Wheel data on the fly?
Just calling Space.Add(Vehicle) or Space.Remove(Vehicle) will handle the Vehicle object, its body, and wheels. The subcomponents should not be individually managed.As an aside - do you have recommendations as to recreating the vehicle? Or is there a neat way to say myVehicle = new PhysicsVehicle without having to first remove all it's bodies from the space, then recreate it? (Something in the demo code you can point me towards maybe?)
So recreating a vehicle would just look something like this (assuming the vehicle was in the space to start with):
Code: Select all
Space.Remove(Vehicle);
Vehicle = SetUpVehicle();
Space.Add(Vehicle);
It should be possible to do that, and I found a few problems which stopped it from working. The new new version should fix them.So this being said, should it be possible to say vehicle.Wheels[0].Shape = new CylinderCastWheelShape(new params) - because this currently throws the null reference exception (ill get that for you shortly).