A general framework for modeling and dynamic simulation of multibody systems using factor graphs