Sign in to follow this  

Adding trailer behind a rotateable missile

This topic is 1831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I have a missile that is rotated every frame so it is facing the target. I want to add a trailer behind this missile. I am having trouble getting the right coordinates.

First, it looks like this:
[attachment=13118:known.png]
All the coordinates of the rectangle is known at this point.
Then I rotate this missile with a known angle, and it may look like this:
[attachment=13119:unknown.png]
At this point, all the coordinates to the rectangle are lost.

How do I get them? Edited by P0jahn

Share this post


Link to post
Share on other sites

In my Smash PC game (detailed in my Old Blog, linked in my Signature), I have a smoke trail for my rocket, and it can shoot at any angle.  Although it doesn't follow, it should be the same deal.

 

Here's the code I use to lay down a smoke (which quickly fades out):

 

 
    // If we're drawing smoketrails, then lay one down, and set it to fade out
    if (mbSmokeTrail)
    {
        cpVect Loc = mpBody->p;
        GameLevel::tLevelItem LevelItem;
        LevelItem.ItemName = "Smoke";
 
        // Set location at back of bullet
        Loc.x -= cos(mpBody->a)*mpImage->GetWidth()/2;
        Loc.y -= sin(mpBody->a)*mpImage->GetHeight()/2;
        LevelItem.Location = Loc;
        LevelItem.ImageName = ""; // get it from xml
        SmashPcItem *pSmokeItem = new SmashPcItem(mGameData,
                mpApp, LevelItem, mpSpace);
 
        mGameData.AddActiveItem(pSmokeItem);
    }
 
Basically, I get the center location of the rocket (cpVect Loc = mpBody->p;) Then I take the sin and cos of the angle of the rocket and multiply it by 1/2 the length (which would put it at the end of the rocket), and subtract value from the rocket Location (where mpBody->a is the angle of the rocket in radians and mpImage is the image of the rocket):

Loc.x -= cos(mpBody->a)*mpImage->GetWidth()/2;
Loc.y -= sin(mpBody->a)*mpImage->GetHeight()/2;
 
Now Loc points to the back of the rocket, regardless the angle it is facing.
 
Here's an image of the rocket being fired with the smoke trail behind it:
SmokeTrails.png
 
Good Luck!
Edited by BeerNutts

Share this post


Link to post
Share on other sites

This is a problem best solved by a particle engine.

For each "smoke" piece you want it should be added to the scene as its own object and at the time it is placed given its own velocity/angle etc as well as a TTL (time to live) and fade out over that time period. As soon as it "dies" it should delete itself (or you can have Particle Factory above it do the deletion) and be removed from rendering lists.

I don't recommend trying to shortcut this, this can be amazingly useful in all things game related and is a great way to learn a lot more about how you should be treating objects in games.

Share this post


Link to post
Share on other sites
At this point, all the coordinates to the rectangle are lost.

How do I get them?

 

Transform the base point by whatever transform you are applying to the sprite. In D3D9X-speak:

 

D3DXMATRIX m;
D3DXMatrixRotationZ(angle, &m);

D3DXVECTOR3 coord(pos.x - halfWidth, pos.y - halfHeight, 0);
D3DXVECTOR3 newCoord;

D3DXVec3TransformCoord(&coord, &newCoord, &m);

 

 

May have the parameter orders wrong there.

Share this post


Link to post
Share on other sites
Sign in to follow this