Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Sun Sep 22, 2019 4:37 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Mar 02, 2014 7:51 am 
Offline
User avatar

Joined: Thu Apr 04, 2013 3:20 pm
Posts: 28
Location: South Shields
HI,

I've just read Aim or Track a Target - viewtopic.php?f=9&t=178
However this doesn't really clear up tracking for me, In the next game that I've got in mind I'm thinking of some sort of topdown shooter.
I don't have any code to show for this as I'm not entirely sure as to how to tackle this.

Image

Take this image as the best to try explain what I'm asking for help with.
The player being blue will walk about being controlled by the keyboard and being rotated 360 degress (by the mouse), they will be attacked by a horde of characters (the people with two hands) which will walk onto the screen and basically walk straight towards the players position and follow them/attack them.

Depending on where the enemy spawns, they are going to have to have their sprite rotated so that it looks like they are actually face the player whilst walking towards them, this is where I'm very stuck - I don't know how exactly to do this apart from the two functions to return the centre of the player sprite (being the centre of the blue circle) - Could you please help/advise.


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 4:50 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 553
I've modified the Spacewar game to make ship2 aim at ship1.
Code:
//=============================================================================
// Artificial Intelligence
//=============================================================================
void Spacewar::ai()
{
    float distX = ship1.getCenterX() - ship2.getCenterX();   // ship1X - ship2X
    float distY = ship1.getCenterY() - ship2.getCenterY();   // ship1Y - ship2Y
    float angle = atan2(distY, distX);  // angle between ships
    float angleDifference = angle - ship2.getRadians();   // angle difference

    // normalize angleDifference to [-PI,PI] range so we turn shortest direction toward target
    if(angleDifference >= PI)
        angleDifference -= 2*PI;
    else if(angleDifference < -PI)
        angleDifference += 2*PI;

    // Fire if aiming within +/- PI/16 radians of target.
    if(angleDifference < PI/16 && angleDifference > -PI/16)
        torpedo2.fire(&ship2);

    if(angleDifference > 0)         // if aimed right of target
        rotate = spacewarNS::RIGHT; // prepare to turn left
    else if(angleDifference < 0)    // if aimed left of target
        rotate = spacewarNS::LEFT;  // prepare to turn right
    else                            // aimed at target
        rotate = spacewarNS::NONE;              // do not turn
}

The turning happens in the update function:
Code:
            if(rotate == spacewarNS::RIGHT)     // if turn right
                ship2.rotate(shipNS::RIGHT);    // rotate
            if(rotate == spacewarNS::LEFT)      // if turn left
                ship2.rotate(shipNS::LEFT);     // rotate

I also modified the update function in ship2 to remove the angular momentum so the computer has better control.
Code:
    switch (direction)                          // rotate ship
    {
    case shipNS::LEFT:
        rotation = -shipNS::ROTATION_RATE;      // rotate left
        break;
    case shipNS::RIGHT:
        rotation = shipNS::ROTATION_RATE;       // rotate right
        break;
    }
    spriteData.angle += frameTime * rotation;   // apply rotation

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 02, 2014 5:42 pm 
Offline
User avatar

Joined: Thu Apr 04, 2013 3:20 pm
Posts: 28
Location: South Shields
This is brilliant!
Thank you!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group