简体   繁体   中英

C# & XNA - 2D Engine - Group Sprites From Car

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 the Draw method ( MSDN ). In this answer where I say "sprite" I mean a single call to Draw - 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 :)

When you move your car, only update the position of the car. The offsets for the parts should remain the same. When you draw the sprites, you would combine the position of the car with the relative position of the part. For example:

var partPosition = List[0].RelativePosition;
var position = Position + partPosition;

You could then use a rectangle to draw it in place

var rectangle = new Rectangle(position.X, position.Y, partWidth, partHeight);

To apply any rotation, you would need a rotation matrix to apply to the position/rectangle. XNA has Helper methods on the Matrix class to help you with this (see http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.matrix_members.aspx ).

If you are unfamiliar with matrix and vector mathematics, I suggestion brushing up on that first though.

You could use a Matrix which describes Position and Orientation of your car. You can apply that to a spritebatch and just use the local coordinates (RelativePosition) to draw.

Else, you describe the transformation for each part in a matrix and multiplay it with the matrix of the car. You can then multiply direction and position of each part with the transformation matrix to translate it to the screen position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM