Can any one please assist with code for two objects coming from different direction. Subsequently, collide after intersection. The objects should be moving at 80km/h. And collision should happen shortly 30 minutes after take off

Dani AI

Generated

A concise, practical way to complete this thread is to pick a clear model, convert to SI, and simulate 1‑D motion until overlap. As pointed out, decide how km/h maps to screen or world units; as noted, full momentum/energy calculations are only necessary if post‑impact motion or deformation is required. The minimal assumptions below produce a deterministic, easy‑to‑verify result.

Work in meters and seconds. Convert speeds (example: 80 km/h = 80 000 / 3600 = 22.2222 m/s) and time (30 minutes = 1800 s). For head‑on motion the closing speed is v1+v2, so initial separation required = (v1+v2) * t. If trains have length, collision occurs when their occupied segments overlap; subtract lengths from the gap when computing the meeting point.

A compact C++ simulation sketch (1D, constant velocities, configurable lengths, logs positions each second):

#include <iostream>
#include <cmath>

struct Train { double pos; double vel; double length; };

int main() {
    double dt = 1.0; // seconds
    double t = 0.0;
    // coordinate axis: positive = south. Two trains start symmetrically.
    Train passenger = { -40000.0, +22.222222, 200.0 }; // pos(m), vel(m/s), length(m)
    Train goods     = { +40000.0, -22.222222, 200.0 };
    while (t <= 3600) {
        if (std::fabs(passenger.pos - goods.pos) <= (passenger.length + goods.length)/2.0) {
            std::cout << "Collision at t=" << t << " s, pos=" << (passenger.pos+goods.pos)/2.0 << " m\n";
            break;
        }
        passenger.pos += passenger.vel * dt;
        goods.pos     += goods.vel * dt;
        t += dt;
    }
    return 0;
}

Practical tips: use double for precision, pick dt small enough to avoid skipping the overlap (1 s is usually fine), log every N seconds for readability, and document the pixel-to-meter scale if a GUI is added. For realistic crash results add train lengths, braking/acceleration profiles, and then consider momentum/energy as suggested.

Recommended Answers

All 6 Replies

Can any one please assist with code for two objects coming from different direction. Subsequently, collide after intersection. The objects should be moving at 80km/h. And collision should happen shortly 30 minutes after take off

That's a program with many parts. You need to provide much more information. Is this a console application or a GUI? What do the objects look like? What does 80 km/h mean in relation to the computer screen (i.e. what is the distance in kilometers of the computer screen width)? And many other details. You need to decide with much more specificity what you want, tell us what you know how to do already, and post at least an attempt at this.

Can any one please assist with code for two objects coming from different direction. Subsequently, collide after intersection. The objects should be moving at 80km/h. And collision should happen shortly 30 minutes after take off

This sounds like a physics problem with elements of-

Newton's Law
Conservation of Momentum
(Possible) Conservation of Energy

-You will most definitely need Conservation of Momentum since during the collision, there's no telling how much energy is lost from heat due to air resistance/crumpling. The first two laws should be a good start.

Also, what is the angle of misdirection? I have a program that can solve these kinds of problems but its written in Java.

This sounds like a physics problem with elements of-

Newton's Law
Conservation of Momentum
(Possible) Conservation of Energy

-You will most definitely need Conservation of Momentum since during the collision, there's no telling how much energy is lost from heat due to air resistance/crumpling. The first two laws should be a good start.

Also, what is the angle of misdirection? I have a program that can solve these kinds of problems but its written in Java.

It all depends on what the OP has in mind regarding the collision. He/she could be looking for a collision that follows all of the laws of physics to the letter or the collision could simply be to display "Kaboom!" in text when it happens. We have no way of knowing until the OP tells us.

A blue passenger train moves southwards at a speed of 80km/hr. Unfortunately as it enters a junction it collides with a brown goods train moving northwards. The collision happened 30 minutes after the goods train takes off. Use C++ to illustrate the above.

A blue passenger train moves southwards at a speed of 80km/hr. Unfortunately as it enters a junction it collides with a brown goods train moving northwards. The collision happened 30 minutes after the goods train takes off. Use C++ to illustrate the above.

You will most likely want to print the coordinates of both the train and the brown goods train every minute or second, and display their speed as well.

send me how to move aman

commented: Didn't you get it? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.