Skip to content

Collision detection: CARLA collision sensor + Newtonian 2D collision physics#463

Open
servinar wants to merge 3 commits intoBerkeleyLearnVerify:mainfrom
servinar:collision-detection
Open

Collision detection: CARLA collision sensor + Newtonian 2D collision physics#463
servinar wants to merge 3 commits intoBerkeleyLearnVerify:mainfrom
servinar:collision-detection

Conversation

@servinar
Copy link
Copy Markdown

@servinar servinar commented May 5, 2026

Description

Adds collision detection in two places — a CARLA collision sensor wrapped as a Scenic-usable CollisionSensor, and pairwise 2D collision physics in the Newtonian simulator.

CARLA:

  • New CarlaCollisionSensor wrapping sensor.other.collision, exported from the world model as CollisionSensor. Tracks collision_history, has_collision, and the most recent event's intensity / impulse, with reset() for between-simulation cleanup.
  • CarlaSimulation.step() learns about event-based sensors: rather than blocking on sensor.frame != current_frame (which never matches for sensors that only fire on events), bump the frame number via updateFrame() and skip the wait. Sensors with a reset() method are reset on simulation teardown.
  • Auto-attach a collision sensor to the ego in createObjectInSimulator so users can write terminate when ego.sensors['collision'].has_collision without boilerplate.
  • Add a colored "Collision:" status line to the CARLA HUD via a new (label, value, color) tuple format; green for none, red for hit + intensity.

Newtonian:

  • 2D pairwise collision detection via shapely polygon intersection on _corners2D, resolved with an SAT-derived collision normal. Apply impulse along the normal scaled by mass and restitution, and split penetration depth between the two objects.
  • Optional enable_crash_physics (default on) marks colliding cars and pedestrians as crashed and applies post-impact dynamics: heavy friction, momentum transfer for car-pedestrian hits, randomized spin for car-car crashes. Per-object mass defaults to 1500 kg for cars and 70 kg for pedestrians.

Issue Link

N/A

Checklist

  • I have tested the changes locally via pytest and/or other means
  • I have added or updated relevant documentation
  • I have autoformatted the code with black and isort
  • I have added test cases (if applicable)

Additional Notes

  • Verified the CARLA path end-to-end against a running CARLA server (Town01): ran a scenario without with sensors {...}, drove the ego forward via SetThrottleAction(1.0), and confirmed terminate when ego.sensors['collision'].has_collision fires when the ego rear-ends a stopped vehicle. Collision history populates with frame, other actor type, and impulse intensity.
  • The HUD "Collision:" line is composed by this PR but is only blitted once step() actually calls hud.tick() and hud.render() — that wiring lives in companion PR CARLA usability fixes (HUD, cleanup, substepping) + Ubuntu/2D defaults #462. Until both land, the line is built but not rendered.
  • These are two simulator changes in one PR for thematic reasons; happy to split if preferred.

servinar added 3 commits May 5, 2026 20:52
Add a CarlaCollisionSensor wrapping CARLA's sensor.other.collision
blueprint, exported from the world model as CollisionSensor. It exposes
collision_history, has_collision, and the most recent event's intensity
and impulse, with reset() to clear state between simulations.

Make CarlaSimulation.step() aware of event-based sensors: rather than
blocking on sensor.frame != current_frame (which never matches for
sensors that only fire on events), bump their frame number via
updateFrame() and skip the wait. Sensors with a reset() method are
reset on simulation teardown.
Attach a CarlaCollisionSensor to the ego in createObjectInSimulator if the user hasn't declared one explicitly, so scenarios can use terminate when ego.sensors['collision'].has_collision without boilerplate.

Add a colored "Collision:" status line to the CARLA HUD via a new (label, value, color) tuple format. Renders green when no collision, red when one is in progress, with the impacted actor and impulse intensity.
Detect pairwise object collisions via shapely polygon intersection on _corners2D, then resolve them with an SAT-derived collision normal: apply an impulse along the normal scaled by mass and restitution, and split the penetration depth between the two objects.

Optional crash physics (toggled by enable_crash_physics, default on) marks colliding cars and pedestrians as crashed, applies post-impact dynamics — heavy friction, momentum transfer for car-pedestrian hits, randomized spin for car-car crashes — and freezes their controls in subsequent ticks. Per-object mass defaults to 1500 kg for cars and 70 kg for pedestrians.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant