JSim 2026.06.01-p(1)
Loading...
Searching...
No Matches
gamepiece.hpp
Go to the documentation of this file.
1// Copyright (c) JSim contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the LGPLv3 license file in the root directory of this project.
4
5#pragma once
6
8#include <algorithm>
9#include <cctype>
10#include <string>
11
12namespace frcsim {
13
22 public:
25
27
28 enum class State { kAirborne, kGrounded, kHeld };
29
30 Gamepiece() = default;
34
35 void setGamepieceState(State s) { state_ = s; }
36 State getGamepieceState() const { return state_; }
37
43 bool pick(const PickupRequest& req) {
44 const bool ok = requestPickup(req);
45 if (ok) {
46 state_ = State::kHeld;
47 }
48 return ok;
49 }
50
54 void place(const Vector3& pos) {
56 s.position_m = pos;
59 s.held = false;
61 state_ = State::kGrounded;
62 }
63
67 void outtake(const Vector3& muzzle_position_m,
68 const Vector3& muzzle_velocity_mps,
69 const Vector3& muzzle_spin_radps = Vector3::zero()) {
70 BallPhysicsSim3D::shoot(muzzle_position_m, muzzle_velocity_mps,
71 muzzle_spin_radps);
72 state_ = State::kAirborne;
73 }
74
78 void step(double dt_s) {
79 if (state_ == State::kHeld) {
80 // Held pieces are updated by carrier pose; avoid integrating.
81 return;
82 }
83
84 if (state_ == State::kGrounded) {
85 // Lightweight ground update: snap to ground and decay planar speed.
87 const double floor_z = BallPhysicsSim3D::config().ground_height_m +
89 if (s.position_m.z < floor_z) {
90 s.position_m.z = floor_z;
91 }
92
93 const double planar_speed = s.velocity_mps.planarSpeed();
94 if (planar_speed > 1e-9) {
95 const double decay =
96 std::max(0.0, 1.0 - BallPhysicsSim3D::config().rolling_friction_per_s * dt_s);
97 s.velocity_mps.x *= decay;
98 s.velocity_mps.y *= decay;
99 if (s.velocity_mps.planarSpeed() <= 1e-3) {
100 s.velocity_mps.x = 0.0;
101 s.velocity_mps.y = 0.0;
102 }
103 } else {
104 s.velocity_mps.x = 0.0;
105 s.velocity_mps.y = 0.0;
106 }
107
108 s.velocity_mps.z = 0.0;
111 return;
112 }
113
114 // Default: airborne -> perform full ball physics step.
116 // If ball touches ground during step, transition to grounded state.
117 const double floor_z = BallPhysicsSim3D::config().ground_height_m +
119 if (BallPhysicsSim3D::state().position_m.z <= floor_z + 1e-6) {
120 state_ = State::kGrounded;
121 }
122 }
123
127 void setTypeName(const std::string& name) { type_name_ = name; }
128
132 const std::string& typeName() const { return type_name_; }
133
137 bool isBall() const {
138 if (type_name_.empty()) return true;
139 std::string lower = type_name_;
140 std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char c){ return std::tolower(c); });
141 if (lower.find("ball") != std::string::npos) return true;
142 if (lower.find("sphere") != std::string::npos) return true;
143 if (lower.rfind("generic_", 0) == 0) return true;
144 return false;
145 }
146
147 private:
148 State state_{State::kGrounded};
149 std::string type_name_{};
150};
151
152} // namespace frcsim
const BallState & state() const
Returns current ball state.
Definition ball_physics.hpp:114
void setState(const BallState &state)
Replaces state and sanitizes non-finite values.
Definition ball_physics.hpp:117
void shoot(const Vector3 &muzzle_position_m, const Vector3 &muzzle_velocity_mps, const Vector3 &muzzle_spin_radps=Vector3::zero())
Places the ball at a muzzle pose and sets launch velocity/spin.
Definition ball_physics.hpp:168
bool requestPickup(const PickupRequest &pickup_request)
Attempts to pick up and hold the ball.
Definition ball_physics.hpp:138
const BallProperties & ballProperties() const
Returns active, sanitized ball properties.
Definition ball_physics.hpp:105
void step(double dt_s)
Advances simulation by dt seconds.
Definition ball_physics.hpp:182
const Config & config() const
Returns active, sanitized configuration.
Definition ball_physics.hpp:98
BallPhysicsSim3D()=default
Gamepiece()=default
State getGamepieceState() const
Definition gamepiece.hpp:36
void outtake(const Vector3 &muzzle_position_m, const Vector3 &muzzle_velocity_mps, const Vector3 &muzzle_spin_radps=Vector3::zero())
Outtake (launch) the gamepiece into free flight and mark as airborne.
Definition gamepiece.hpp:67
Gamepiece(const BallPhysicsSim3D::Config &cfg, const BallPhysicsSim3D::BallProperties &props)
Definition gamepiece.hpp:31
BallPhysicsSim3D::Config Config
Definition gamepiece.hpp:23
BallPhysicsSim3D::BallProperties Properties
Definition gamepiece.hpp:24
bool isBall() const
Definition gamepiece.hpp:137
void setTypeName(const std::string &name)
Definition gamepiece.hpp:127
bool pick(const PickupRequest &req)
Attempt to pick up the gamepiece into a carrier. Delegates to BallPhysicsSim3D::requestPickup and fli...
Definition gamepiece.hpp:43
void step(double dt_s)
Step wrapper that applies low-cost behavior for grounded/held.
Definition gamepiece.hpp:78
State
Definition gamepiece.hpp:28
@ kAirborne
Definition gamepiece.hpp:28
@ kGrounded
Definition gamepiece.hpp:28
@ kHeld
Definition gamepiece.hpp:28
void place(const Vector3 &pos)
Place the gamepiece at world position and mark as grounded.
Definition gamepiece.hpp:54
const std::string & typeName() const
Definition gamepiece.hpp:132
void setGamepieceState(State s)
Definition gamepiece.hpp:35
Definition vector.hpp:13
Physical parameters for the ball body.
Definition ball_physics.hpp:49
double radius_m
Definition ball_physics.hpp:53
Dynamic state advanced by step().
Definition ball_physics.hpp:66
bool held
Definition ball_physics.hpp:74
Vector3 velocity_mps
Definition ball_physics.hpp:70
Vector3 position_m
Definition ball_physics.hpp:68
Vector3 spin_radps
Definition ball_physics.hpp:72
Runtime physics environment parameters.
Definition ball_physics.hpp:25
double ground_height_m
Definition ball_physics.hpp:39
Request payload used to capture a ball into a carrier.
Definition ball_physics.hpp:78
3D vector utility used throughout JSim physics.
Definition vector.hpp:22
double z
Z component.
Definition vector.hpp:28
double planarSpeed() const noexcept
Returns magnitude of the XY projection.
Definition vector.hpp:177
static constexpr Vector3 zero() noexcept
Returns the zero vector.
Definition vector.hpp:360
double x
X component.
Definition vector.hpp:24
double y
Y component.
Definition vector.hpp:26