Collision avoidance is one of the most challenging tasks people need to consider for developing the self-driving technology. In this paper we propose a new spatiotemporal motion planning algorithm that efficiently solves a constrained nonlinear optimal control problem using the iterative linear quadratic regulator (iLQR), which takes into account the uncertain driving behaviors of the traffic vehicles and minimizes the collision risks between the self-driving vehicle (referred to as the "ego" vehicle) and the traffic vehicles such that the ego vehicle is able to maintain sufficiently large distances to all the surrounding vehicles for achieving the desired collision avoidance maneuver in traffic. To this end, we introduce the concept of the "collision polygon" for computing the minimum distances between the ego vehicle and the traffic vehicles, and provide two different solutions for designing the constraints of the motion planning problem by properly modeling the behaviors of the traffic vehicles in order to evaluate the collision risk. Finally, the iLQR motion planning algorithm is validated in multiple real-time tasks for collision avoidance using both a simulator and a level-3 autonomous driving test platform.