رفتن به محتوا

Creating an AI tramway that follows tracks using lists

این محتوا هنوز به زبان شما در دسترس نیست.

💡 Building complex AI movement systems? Need help with pathfinding and track systems? 🚀 Get Help Now

TB

TrackBuilder_Sam

Posted on August 5, 2024 • Intermediate

🚊 Need help with AI tramway system

Hi everyone! I’m working on a city simulation project where I want to create an AI tramway that automatically follows predefined tracks throughout the map. I’m trying to figure out how to make the tram follow tracks using lists, but I can’t seem to get it working properly.

The tramway should be able to navigate through different stations and follow a specific route. Any help with implementing this would be greatly appreciated!

TS

TransportSystem_Pro

Replied 2 hours later • ⭐ Best Answer

Great question @TrackBuilder_Sam! Creating an AI tramway system is a fantastic project. Here’s a comprehensive guide to build a smart track-following system:

🚊 Tramway AI System Overview

Here’s how the intelligent tramway system works:

flowchart TD A[🚩 Game Start] --> B[📋 Initialize Track Lists] B --> C[📍 Set Starting Position] C --> D[🎯 Set Current Waypoint = 1] D --> E{🔄 Main Loop} E --> F[📊 Get Target Coordinates] F --> G[🧭 Calculate Direction] G --> H[🚊 Move Towards Target] H --> I{📏 Distance Check} I -->|Distance < 10| J[✅ Waypoint Reached] I -->|Distance ≥ 10| E J --> K[⏱️ Station Stop?] K -->|Yes| L[🛑 Stop at Station] K -->|No| M[➡️ Next Waypoint] L --> N[👥 Passenger Actions] N --> O[⏰ Wait Timer] O --> M M --> P{🔄 End of Route?} P -->|Yes| Q[🔄 Reset to Waypoint 1] P -->|No| R[📈 Increment Waypoint] Q --> E R --> E style A fill:#e1f5fe style B fill:#f3e5f5 style C fill:#e8f5e8 style D fill:#fff3e0 style E fill:#ffebee style J fill:#e8f5e8 style L fill:#fff8e1 style N fill:#f1f8e9

🛤️ Step 1: Set Up Track Data Lists

First, create lists to store your track waypoints:

    when flag clicked
// Create track coordinate lists
delete all of [track_x v]
delete all of [track_y v]

// Define your track route (example: circular route)
add [-200] to [track_x v]
add [0] to [track_y v]
add [-100] to [track_x v]
add [100] to [track_y v]
add [0] to [track_x v]
add [150] to [track_y v]
add [100] to [track_x v]
add [100] to [track_y v]
add [200] to [track_x v]
add [0] to [track_y v]
add [100] to [track_x v]
add [-100] to [track_y v]
add [0] to [track_x v]
add [-150] to [track_y v]
add [-100] to [track_x v]
add [-100] to [track_y v]
  

🚊 Step 2: Basic Tramway Movement

Create the core movement system for your tramway:

    when flag clicked
set [current_waypoint v] to [1]
set [tram_speed v] to [3]
go to x: (item (current_waypoint) of [track_x v]) y: (item (current_waypoint) of [track_y v])

forever
// Get target position
set [target_x v] to (item (current_waypoint) of [track_x v])
set [target_y v] to (item (current_waypoint) of [track_y v])

// Move towards target
point towards x: (target_x) y: (target_y)
move (tram_speed) steps

// Check if reached waypoint
if <(distance to x: (target_x) y: (target_y)) < [10]> then
change [current_waypoint v] by [1]
if <(current_waypoint) > (length of [track_x v])> then
set [current_waypoint v] to [1]
end
end
end
  

🎯 Step 3: Smooth Movement System

For smoother, more realistic movement:

    define move smoothly to x: (target_x) y: (target_y)
set [distance_x v] to ((target_x) - (x position))
set [distance_y v] to ((target_y) - (y position))
set [total_distance v] to ([sqrt v] of (((distance_x) * (distance_x)) + ((distance_y) * (distance_y))))

if <(total_distance) > [5]> then
// Normalize movement vector
set [move_x v] to (((distance_x) / (total_distance)) * (tram_speed))
set [move_y v] to (((distance_y) / (total_distance)) * (tram_speed))

change x by (move_x)
change y by (move_y)
else
// Snap to target when close enough
go to x: (target_x) y: (target_y)
end
  

🔄 Step 4: Advanced Tramway with Stations

Add station stops and passenger management:

    when flag clicked
// Station data
delete all of [station_waypoints v]
add [2] to [station_waypoints v]  // Station at waypoint 2
add [5] to [station_waypoints v]  // Station at waypoint 5
add [8] to [station_waypoints v]  // Station at waypoint 8

set [station_stop_time v] to [3]  // seconds
set [is_at_station v] to [false]

forever
move smoothly to x: (target_x) y: (target_y)

// Check if reached waypoint
if <(distance to x: (target_x) y: (target_y)) < [5]> then
// Check if this waypoint is a station
if <[station_waypoints v] contains (current_waypoint)?> then
set [is_at_station v] to [true]
broadcast [tram arrived at station v]
wait (station_stop_time) seconds
broadcast [tram departing station v]
set [is_at_station v] to [false]
end

// Move to next waypoint
change [current_waypoint v] by [1]
if <(current_waypoint) > (length of [track_x v])> then
set [current_waypoint v] to [1]
end
end
end
  

🎨 Step 5: Visual Enhancements

Add realistic tramway behavior:

    // Tramway rotation and animation
when flag clicked
forever
if <not <(is_at_station) = [true]>> then
// Point in direction of movement
set [direction_x v] to ((target_x) - (x position))
set [direction_y v] to ((target_y) - (y position))
point in direction ([atan2 v] of (direction_y) (direction_x))

// Animate wheels or add movement effects
next costume
wait [0.1] seconds
end
end
  

🚦 Step 6: Traffic and Signal System

Add traffic lights and collision detection:

    // Traffic signal system
when I receive [check traffic signals v]
if <touching [traffic light v]?> then
if <([costume name v] of [traffic light v]) = [red]> then
set [tram_speed v] to [0]  // Stop for red light
else
set [tram_speed v] to [3]  // Normal speed
end
else
set [tram_speed v] to [3]
end
  

This system creates a fully functional AI tramway that follows predefined tracks with realistic behavior! 🎉

TB

TrackBuilder_Sam

Replied 3 hours later

@TransportSystem_Pro This is incredible! Thank you so much! 🚊✨

The smooth movement system works perfectly, and the station stops add such a realistic touch. One question - how can I make the tramway handle curved tracks more smoothly?

CM

CurveMaster_Dev

Replied 2 hours later

@TrackBuilder_Sam Great question! For smooth curved tracks, you can use Bézier curves or spline interpolation:

    define smooth curve between (x1) (y1) and (x2) (y2)
// Add intermediate control points for curves
set [curve_steps v] to [10]
set [step v] to [0]
repeat (curve_steps)
set [t v] to ((step) / (curve_steps))
// Quadratic interpolation for smooth curves
set [curve_x v] to (((1 - (t)) * (x1)) + ((t) * (x2)))
set [curve_y v] to (((1 - (t)) * (y1)) + ((t) * (y2)))

add (curve_x) to [track_x v]
add (curve_y) to [track_y v]
change [step v] by [1]
end
  

This creates much smoother curves between waypoints! 🌟

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Build Advanced Transportation Systems!

Amazing discussion on AI tramway systems! For those looking to create even more sophisticated transportation networks, our community can help you implement:

  • 🚇 Multi-line subway systems
  • 🚌 Dynamic bus routing
  • 🚦 Smart traffic management
  • 👥 Passenger AI and scheduling

📚 Related Topics

Ready to build the next generation of transportation simulations? Get expert guidance from our AI specialists in the Vibelf app!