The brain sitting inside its bony cavity sends and receives myriads of sensory inputs and outputs. A problem that must be solved either in ontogeny or phylogeny is how to extract the particular characteristics within this “blooming buzzing confusion” that signal the existence and nature of physical space, with structured objects immersed in it, among them the agent’s body. The idea that spatial knowledge must be extracted from the sensorimotor flow in order to underlie perception has been considered by a number of thinkers, including Helmholtz, Poincare, Nicod, Gibson, etc. However, little work has considered how this could actually be done by organisms without a priori knowledge of the nature of their sensors and effectors. Here we show how an agent with arbitrary sensors will naturally discover spatial knowledge from the undifferentiated sensorimotor flow. The method first involves tabulating sensorimotor contingencies, that is, the laws linking sensory and motor variables. Second, further laws are created linking these sensorimotor contingencies together. The method works without any prior knowledge about the structure of the agent’s sensors, body, or of the world. We show that the extracted laws endow the agent with basic spatial knowledge, manifesting itself through perceptual shape constancy and the ability to do path integration. We further show that the ability of the agent to learn all spatial dimensions depends on the ability to move in all these dimensions, rather than on possessing a sensor that has that dimensionality. This latter result suggests, for example, that three dimensional space can be learned in spite of the fact that the retinas are two-dimensional. We conclude by showing how the acquired spatial knowledge paves the way to building the notion of object.