This example is running in WebGL2 and should work in most browsers. You can check the WebGPU examples here.
//! Demonstrates rotating entities in 2D using quaternions. use ; const TIME_STEP: f32 = 1.0 / 60.0; const BOUNDS: Vec2 = new; /// player component /// snap to player ship behavior ; /// rotate to face player ship behavior /// Add the game's entities to our world and creates an orthographic camera for 2D rendering. /// /// The Bevy coordinate system is the same for 2D and 3D, in terms of 2D this means that: /// /// * `X` axis goes from left to right (`+X` points right) /// * `Y` axis goes from bottom to top (`+Y` point up) /// * `Z` axis goes from far to near (`+Z` points towards you, out of the screen) /// /// The origin is at the center of the screen. /// Demonstrates applying rotation and movement based on keyboard input. /// Demonstrates snapping the enemy ship to face the player ship immediately. /// Demonstrates rotating an enemy ship to face the player ship at a given rotation speed. /// /// This method uses the vector dot product to determine if the enemy is facing the player and /// if not, which way to rotate to face the player. The dot product on two unit length vectors /// will return a value between -1.0 and +1.0 which tells us the following about the two vectors: /// /// * If the result is 1.0 the vectors are pointing in the same direction, the angle between them /// is 0 degrees. /// * If the result is 0.0 the vectors are perpendicular, the angle between them is 90 degrees. /// * If the result is -1.0 the vectors are parallel but pointing in opposite directions, the angle /// between them is 180 degrees. /// * If the result is positive the vectors are pointing in roughly the same direction, the angle /// between them is greater than 0 and less than 90 degrees. /// * If the result is negative the vectors are pointing in roughly opposite directions, the angle /// between them is greater than 90 and less than 180 degrees. /// /// It is possible to get the angle by taking the arc cosine (`acos`) of the dot product. It is /// often unnecessary to do this though. Beware than `acos` will return `NaN` if the input is less /// than -1.0 or greater than 1.0. This can happen even when working with unit vectors due to /// floating point precision loss, so it pays to clamp your dot product value before calling /// `acos`.