I’m getting started to XNA and I want to build a basic 2D tile-based engine.
So far i’ve allready made some car steering maths and handling but NO COLLISION yet.
Now I’ve decidet to make the car destructible, so when i hit a wall, the area of the car will be bumped.

The center part of the car remains the same, but when an other car hits the players car from the left side, I want to replace the “normal” image of the left side with the “bumped” image from the left side.
My problem is, that i dont even now how to GROUP all the parts of the car ( there will also be Front and back lights added ) so when I drive all the parts are following.
So far i have a simple Car and CarParts class like this:
public Car() {
Vector2D Position;
float Rotation;
Vector2D Direction;
Texture2D BaseTexture;
List<CarParts> Parts;
}
public CarParts() {
Vector2D RelativePosition;
Texture2D Testure;
}
where:
List[0] = FrontPart;
List[1] = FrontLeftPart;
List[2] = FrontRightPart;
List[3] = LeftPart;
List[4] = RightPart;
List[5] = RearLeftPart;
List[6] = RearRightPart;
List[7] = ReartPart;
How can i make those CHILDS move with the Main Car ?
° EDIT: or is there another way to make that happen ?
Thanks for all your help !
If you are using
SpriteBatch, then the way to handle this is with careful setting of the origin parameter to theDrawmethod (MSDN). In this answer where I say “sprite” I mean a single call toDraw– so your car will be made up of multiple sprites.The origin parameter sets the origin from where position, scaling and rotation of your sprite takes place. You can set the origin to a position outside of the boundary of a sprite.
The origin position is interpreted as relative to the top-left corner of the source rectangle of your sprite. (If you do not specify a source rectangle, a source rectangle that covers the entire texture is assumed). The origin is specified in units that match pixels in the source texture.
So – you should maintain a list of the 9 textures (or 9 source rectangles, if you are using sprite sheets) that make up the different parts of your car. And, also, for each part, store an origin position.
The origin positon that you set for each sprite will be
(centre of car) - (top left of sprite).So for the centre sprite in the car, it will be approximately in the centre of the rectangle. For the top left corner of the car, it will be positive on both axes but beyond the boundaries of the sprite. For the bottom right corner, the origin will be negative on both axes therefore outside the sprite boundary. And so on for the other parts.
Then, simply draw all 9 sprites that make up your car, each one having a different source-rectangle/texture and origin. But each one having the same position, scale, and rotation.
No need to mess around with matrices or anything 🙂