Feel Your Vibe - Game Documentation
I. Overview
"Feel Your Vibe" is an interactive music game that allows users to dynamically control tremolo effects using intuitive swipe gestures. Vertical swipes adjust the tremolo frequency (speed), while horizontal movements control the tremolo depth (amplitude). The game features a custom Low-Frequency Oscillator (LFO) and frequency-adaptive haptic feedback for a rich sensory experience.
II. Architecture and Components
The game is built using a modular design, leveraging shared components and game-specific logic:
- Audio System:
AudioManager: Manages audio session configuration, playback, and volume control.LFO: A custom class responsible for generating the tremolo effect.
- User Interaction:
DragGesture: Detects and processes swipe gestures.- Screen Center-crossing detection: Ensures accurate timing for frequency changes based on vertical swipes.
- Haptic Feedback:
HapticManagerprovides tactile feedback synchronized with the tremolo effect. - State Management: Manages playback state, LFO parameters (frequency and amplitude), and UI visibility.
III. Core Functionality
A. LFO (Low-Frequency Oscillator)
The LFO generates a sine wave to modulate the audio volume, creating the tremolo effect.
- Class Structure:
class LFO {
var frequency: Double = 1.0 // Oscillation speed (Hz)
var amplitude: Double = 0.1 // Oscillation depth (0-1)
private var phase: Double = 0.0
func update(deltaTime: TimeInterval) -> Double {
phase += 2 * .pi * frequency * deltaTime
return amplitude * sin(phase)
}
}
- Key Functionality:
- Wave Generation: Uses a sine wave function for smooth oscillation.
- Parameter Control:
- Frequency: Ranges from 0.1 Hz to 10.0 Hz.
- Amplitude: Ranges from 0.0 (no effect) to 1.0 (maximum modulation).
- Time Management: Uses
deltaTimefor precise phase calculation, ensuring consistent tremolo regardless of frame rate.
B. Gesture Handling and Parameter Mapping
Swipe gestures are processed to control the LFO parameters:
-
Vertical Swipes (Frequency Control):
- Screen Center Crossing Detection: Tracks when the swipe crosses the vertical midpoint of the screen.
- Frequency Calculation: Calculates the time between center crossings to determine the swipe speed and map it to the LFO frequency. A moving average is used for smoothing.
- Frequency Range: Constrained between 0.1 Hz and 10 Hz.
-
Horizontal Movements (Amplitude Control):
- Position Mapping: Maps the horizontal touch position to the LFO amplitude. Left side corresponds to minimum amplitude, right side to maximum.
- Amplitude Scaling:
amplitude = (position.x / screenWidth) * (maxAmp - minAmp) + minAmp
-
LFO Update: The calculated frequency and amplitude values are applied to the LFO instance in each update cycle.
C. Haptic Feedback
The haptic feedback is frequency-adaptive:
- Low Frequency (≤ 2 Hz): Continuous haptic patterns with longer duration and higher intensity.
- High Frequency (> 2 Hz): Short, sharp pulses with lower intensity.
- Gesture Response: Haptic feedback is triggered on screen center crossing, with intensity varying based on swipe speed.
IV. Game Flow and Lifecycle
A. Game Flow
The main game loop is as follows:
- Initialization: Audio, LFO, and haptic systems are initialized.
- User Input (Swipe): Gestures are detected and processed.
- Parameter Update: LFO frequency and amplitude are updated based on swipe data.
- Audio Modulation: The LFO output modulates the audio volume.
- Haptic Feedback: Haptic patterns are triggered based on the LFO frequency.
- Repeat Steps 2-5.
B. Lifecycle Methods
onAppear:- Configures the audio session.
- Prepares the haptic engine.
- Initializes the background music player and LFO.
- Starts inactivity monitoring.
- Gesture Handling: Continuously monitors
DragGesturechanges, updates LFO parameters, and resets the inactivity timer. onDisappear:- Fades out the audio.
- Stops and cleans up the LFO.
- Invalidates timers.
- Releases haptic resources.
- Cleans up the audio session.
V. Key Features
- Dynamic Tremolo Control: Intuitive gesture-based control of tremolo frequency and depth.
- Custom LFO: Efficient sine wave generation for smooth tremolo effects.
- Frequency-Adaptive Haptics: Enhanced tactile feedback synchronized with audio changes.
- Minimalist UI: Focuses on the core interactive experience.
VI. State Variables (Examples)
lfo: The LFO instance.currentFrequency: The current LFO frequency.currentAmplitude: The current LFO amplitude.lastTouchLocation: The last recorded touch position.lastTouchTime: The timestamp of the last touch event.
