Four dimensional cone beam computed tomography (4DCBCT) is an emerging clinical image guidance strategy for tumour sites affected by respiratory motion. In current generation 4DCBCT techniques, both the gantry rotation speed and imaging frequency are constant and independent of the patient's breathing which can lead to projection clustering. We present a mixed integer quadratic programming (MIQP) model for respiratory motion guided-4DCBCT (RMG-4DCBCT) which regulates the gantry velocity and projection time interval, in response to the patient's respiratory signal, so that a full set of evenly spaced projections can be taken in a number of phase, or displacement, bins during the respiratory cycle. In each respiratory bin, an image can be reconstructed from the projections to give a 4D view of the patient's anatomy so that the motion of the lungs, and tumour, can be observed during the breathing cycle. A solution to the full MIQP model in a practical amount of time, 10 s, is not possible with the leading commercial MIQP solvers, so a heuristic method is presented. Using parameter settings typically used on current generation 4DCBCT systems (4 min image acquisition, 1200 projections, 10 respiratory bins) and a sinusoidal breathing trace with a 4 s period, we show that the root mean square (RMS) of the angular separation between projections with displacement binning is 2.7 degrees using existing constant gantry speed systems and 0.6 degrees using RMG-4DCBCT. For phase based binning the RMS is 2.7 degrees using constant gantry speed systems and 2.5 degrees using RMG-4DCBCT. The optimization algorithm presented is a critical step on the path to developing a system for RMG-4DCBCT.