Character animation is important in many applications, such as video games, movie production, and somatosensory interaction. However, it is difficult to control the locomotion of a virtual character in a dynamic environment, because bipeds are under-actuated and unstable. In this paper, we present a novel optimization strategy to improve the realism and stability of virtual character animation. Our trajectory optimization method produces lifelike motions by using the motion capture data. The method describes systematic computation of controllers that can optimize locomotion. We use center of mass (COM) control and foot control to enhance the sense of reality in physical movement, and use tracking control to maintain similarity with the reference motion. Experiment results demonstrate our approach is applicable by simulating walking motion and the optimized motion is both natural and recognizable.