This paper deals with distributed and cooperative control algorithm to make each agents to form a formation for a second-order system. However, when forming a formation with a multi-agent system, there is a possibility that the agents collide with each other or collide with obstacles other than the agent. Also, if the agents avoid other agents and obstacles, the distance between agents may be temporarily too far apart. In order to solve these problems, this paper proposes a control algorithm which consists of Leader-Follower structure, consensus algorithm and artificial force based on artificial potential field. For the control algorithm to make agents achieve formation in a decentralized cooperative way, we derive the condition concerning the control gain to ensure stability. In addition, by comparing the amount of energy, we design conditions that ensure collision avoidance between agents, collision avoidance with obstacles, and distance between agents, and also design potential functions that satisfy the conditions. The validity of the control algorithm proposed in this paper is verified by numerical simulations.