Week2 Examples: Vectors, Normal/Tangent directions, Turning, Home-In,
Following a Function, and Finite State Machine: Here is the
source code.
- Vector Components:
- Right-ThumbStick: Move the block
- Left-ThumbStick: Move the soccer
- Right Trigger: rotate the block
- A/B-Button: Show vector on either the block on the ball
- Block: Note the Rotate parameter of Primitive
- Note: Tangent + Normal: are updated after rotation!
- VectorComponent: The two Update() methods: with and without Normal vector input
- Reflect A Vector:
- A-Button: to shoot the ball
- X-Button: to pause everything
- Left ThumbStick-Y: Speed of ball
- Right ThumbStick: direction for the ball
- Right Trigger: rotate the blocker
- Shoots the ball towards the blocker and shows velocity decomposition at
collision
-
Notice: the 4-5 lines of interesting code in VectorComponent::Update() function.
-
This solution works for circle/rectangle intersection.
-
Difference between FrontDirection and VelocityDirection:
- FrontDirection: the front of the primitive.
- VelocityDireciton: the direction the primitive velocity.
- Elasticity and Friction:
- Almost exactly the same as above, only difference ...
- Right ThumbStick: direction and size of Ball
- Left ThumbStick.X: elasticity of Block (what does this mean?)
- Left ThumbStick.Y: friction of Block (what does this mean?)
- Do we see now why we want to have the different components? Cool?
- Home in to target:
- Right ThumbStick: move the target dot
- Left ThumbStick.X: turn rate
- watch that soccer home into the target!!
- 3-5 lines of code in MySoccer::Update()!! Cool?
- Gradual Turning:
Almost identical to Example-4 from week1, except, at the last stage of the
road, if user change the road-end-point, the car moves/rotates gradually
towards the endpoint rather than rotate to face the end point
immediately.
- Left Thumb-Stick-Y: Change current road segment length
- Left Thumb-Stick-X: Change the turn approaching rate!?
- Right Thumb-Stick: Change current road segment direction
- A-Button: Add in new road segment
- Car rotates and follows current segment of road it is on
- MyCar class: UpdateCarDirAndSpeed()!!
- Plotting and Following a Sine Function:
- Left-Thumb-Stick Up/Down: to change number per periods that
will be fitted in the entire WorldDimension.X,
- Left-Thumb-Stick-Left/Right to change the Amplitude
- Only point interesting is to recognized we need to fit (2*PI*mPeriod)
into WorldDimension.X:
- Use mFrequeicyScale to help in converting x coordinate position into
angular value.
- Note: x-displacement is constant, speed is NOT constant.
- Now to make speed a constant? (Type the A-Button)
- Notice: use of PrimitiveSet for showing/hiding entire set (plotted out
sine curves)
- Notice: moving an object by ourselves (instead of setting
ShouldTravel to true)
-
Finite State Example: The
Paddle:
- A-Button: to create a new random shooting ball
- B-Button: to destroy current moving ball
- Left-Trigger: trigger the left-side paddles
- Right-Trigger: trigger the right-side paddles
- The Paddle class:
- Two states: Rest and PaddleReturning
- Update() method test and call each explicitly
- Rest: if trigger angle, then rotate the paddle and goto
PaddleReturning
- PaddleReturning: gradually return, if user has new input,
maintain the paddle rotation if necessary
- Record hit/miss for fun. Really? Not fun at all!
-
Abstract Paddle -- Almost free Left/Right paddles:
- The Paddle class:
now abstract
- Left and Right subclasses, only differences is in resting/rotation
values
-
The
PaddleSet:
- The PaddleSet class: some simple parameterization
- Suddenly, we can create any number of paddles in a set!!
- Have I convinced you that abstraction is important?
-
Simple Patrol:
- A-Button: to add a new patrol (try adding 100 patrols!)
- B-Button: to remove an existing patrol
- Notice behavior of patrol:
- appears random
- moves in counter-clockwise order
- speed is sort of random
- Class to check out: PatrolObject
- Update function: define a state service function for each state!!
- RandomXXRegionPosition() functions: generate a random position in
the XX region, where XX can be top/bottom-left/right.
- ComputePositionAndVelocity(): Compute a new velocity (with 20%
randomness) and use time (number of ticks) as condition for transition
to the next state.
-
Random Patrol:
- Identical to above, except,
- Randomize next states, look it is relatively straightforward to
determine what each state should transition to (localized in each individual
state's service routine!).
- Some randomness is kind of cool?
-
Smooth Transition Patrol:
- Exactly the same as the above, except
- We do not compute new Velocity direction in
ComputePositionAndVelocity() anymore!!
- Instead: during each update, we call ComputeNewDirection() to rotate
gradually from FrontDirection towards the final target position.
- Oh, the patrols are smaller and we assign a random color to each patrol
-
Patrol That Chases:
- Notice: straightforward to add in a new state: just add in new
service routine into Update()
- Notice: "partial" keyword to split complex class into multiple files
- All Hero related behaviord are in PatrolObject_HeroSupport.cs
- All other service routines now check for Hero being near by (DetectHero()):
- In DetectHero():
- Compute distance to Hero
- If sufficiently close: transit to ChaseHero state!
- Hero state service:
- Always try to follow the hero (by setting mTargetPosition to
hero.Center)
- If cannot catch hero in specific time (mStateTimer),
transition back into regular state by determining which quadrant
the patrol is currently in.
- The app removes patrol if there is a collision with hero.