1

My problem is probably quite simple for you guys, I'm just new with programming so yeah.

I want to have a circle on the center of the screen, then I want that when a player gets close to it, he'll be pushed away in relation to the direction he came from, Here's a little drawing I made to be abit more clearimage

The red circles are the player coming from different directions, the green circle is the obstacle. The arrows show what direction the player should be pushed to the player moves only in the x,y axis

Thank you very much for your time

Radicate
  • 2,934
  • 6
  • 26
  • 35
  • Does the player get pushed away when he collides with it or does the force get stronger as the player gets closer? – Joseph Mansfield Mar 04 '13 at 17:20
  • @sftrabbit I'd rather have it push away when he collides only, both ways are fine though – Radicate Mar 04 '13 at 17:20
  • By push away, do you mean it applies some velocity or acceleration to the player or just moves them back a certain distance? – Joseph Mansfield Mar 04 '13 at 17:21
  • @sftrabbit Just move them back a certain distance in pixels – Radicate Mar 04 '13 at 17:22
  • Find the vector from the circle to the player. ( It should be pointed away from the circle at the player ) Then apply a force, stop the player at a distance, whatever you want to do. For circle to circle collision checking here is a [link](http://stackoverflow.com/questions/1736734/circle-circle-collision) – Connor Hollis Mar 04 '13 at 17:22
  • I'm sorry, but having c++ AND actionscript-3 tags just seems wrong – StoryTeller - Unslander Monica Mar 04 '13 at 17:23
  • @ConnorHollis I currently check for collision with rectangles, I still want to push the object away according to the angle it is from the other, can the link you sent still answer that? – Radicate Mar 04 '13 at 17:26
  • That answer just explains how to check for colliding circles not really applying some force to repel the objects. Unless you mean pushing away as "Not going through." Infact has a good answer for force based stuff below. – Connor Hollis Mar 04 '13 at 17:57

1 Answers1

0

Transform player coordinates (red circle centres) from Descartes-like screen system to polar with origo of centre obstacle (green circle).

Choosing complex numbers for that purpose could be a solution: complex(x,y) == polar(r,th). Radii of objects are given and a tolerancy radius ('close') also. Iterate through player coordinates and compare their magnitude (distance of red circle centres from origo). When distance is larger than sum of their radius + tolerancy then they are not considered close. Otherwise magnitudes are adjusted. Back transforming output coordinates into screen gives the result.

void PushAway( std::vector< std::complex< double > > & player_coords )
{
    const double r_o = 3.0; // obstacle radius
    const double r_p = 1.0; // player radius
    const double r_t = 0.1; // 'close'
    const double push = 10.0; // pushing amount
    for( uint idx = 0; idx < player_coords.size(); ++idx )
    {
        std::complex< double > & pc = player_coords[idx];
        double magn = std::abs( pc );
        // too close ?
        if( magn < r_o + r_t + r_p )
        {
            // push away in appropriate direction
            pc = std::polar( push + magn, std::arg( pc ) );
        }
    }
}
renonsz
  • 571
  • 1
  • 4
  • 17