Speed.
You need to use a very short shutter speed to freeze motion. In order to use a short shutter speed, you need enough light, a lens with a wide enough aperture, and a recording medium, such as fast film or a sensor with high enough sensitivity to properly expose your subject when using a short enough shutter speed.
In terms of still photography relative motion is measured by the angle of the subject's motion relative to the camera's field of view for a given length of exposure. In your train scenario you have described the 'perfect storm' of relative motion: The camera is moving one direction along an axis perpendicular to the optical axis and the subject is moving the same speed in the opposite direction. In order to get a picture of that train or an object thrown from it you would need an exposure time short enough that no perceptible details of the subject's image projected on the sensor move more than the width of a single pixel in the camera's sensor. Obviously, the faster each train is moving the shorter the shutter speed (Tv for time value) would need to be.
Most advanced digital cameras are capable of a shutter speed of 1/8000 sec. The problem with most of these is that although each portion of the sensor is only exposed to light for 1/8000 sec, the exposure is done as a narrow slit between shutter curtains travels in front of the sensor. Even at 1/8000 sec in conventional cameras it takes approximately 1/250 sec to 1/500 sec for this slit to travel across the entire sensor. So what is visible at the bottom of a frame exposed for 1/8000 sec happened 1/250 sec before what is visible in the top of the frame. If your subject moved within the frame during this time the shape of your subject will be distorted. Even cameras without a mechanical shutter usually scan the sensor electronically one row at a time. For your scenario it would be easiest to capture the required subject using a type of sensor that scans the entire sensor at the same time. Many machine vision cameras use such a sensor.
You then must consider how the subject will be properly lit to provide enough light for such a short exposure. Direct sunlight is probably enough if you also have a lens with a fairly wide aperture. The issue with using a wide aperture is that the depth of field (DoF) will be fairly narrow so that accurate focus becomes critical. You would probably need a way to measure or figure the distance to your subject ahead of time and pre-focus the lens to that distance. In the case of two trains passing one another on a straight stretch of tracks the rails are likely at a constant distance apart and the distance could be measured taking into account the position of the camera on one train and the position of the subject on the other relative to the rails.