Flash Basic Theory Course tenth chapter coordinate rotation and angle bounce Ⅰ

Source: Internet
Author: User
Tags cos sin

Back to "flash Basic Theory Class-Catalog"

This chapter introduces a special technique, known as the coordinate rotation. As its name implies, it is the object that rotates its coordinates around a point, and the rotation of the coordinates is essential in making some very interesting effects. Among them is the question that has been discussed in the Flash world for many years: "How do you bounce on an incline?" "And I'll give you one answer."

Another program with coordinate rotation is the interactive bounce effect between the two objects. We'll talk about momentum conservation in the next chapter. and the coordinates of this chapter rotate, we have already contacted before. If you want to skip this chapter, I would advise you to sit down and go through it.

Simple rotation of coordinates

Although we introduced the method of calculating coordinate rotation in the third chapter of trigonometry, we should do some review first. Suppose you know a central point, an object, a radius and an angle. By constantly increasing or decreasing the angle, and using the basic trigonometry knowledge to rotate the object around the center point. We can set the variable to be VR (rotational speed) to control the increase or decrease of the angle. Also, do not forget to use the angle of curvature system to express. The structure of the code looks like this:

vr = 0.1;
angle = 0;
radius = 100;
centerX = 250;
centerY = 200;
// 在enterFrame 处理函数中:
sprite.x = centerX + cos(angle) * radius;
sprite.y = centerY + sin(angle) * radius;
angle += vr;

Use a simple trigonometric function to set the X,y property of an object according to the angle and radius, and change the angle in each frame. We use Flash animation to demonstrate. Here is the first example, document class Rotate1.as:

package {
 import flash.display.Sprite;
 import flash.events.Event;
 public class Rotate1 extends Sprite {
  private var ball:Ball;
  private var angle:Number = 0;
  private var radius:Number = 150;
  private var vr:Number = .05;
  public function Rotate1() {
   init();
  }
  private function init():void {
   ball = new Ball();
   addChild(ball);
   addEventListener(Event.ENTER_FRAME, onEnterFrame);
  }
  private function onEnterFrame(event:Event):void {
   ball.x = stage.stageWidth / 2 + Math.cos(angle) * radius;
   ball.y = stage.stageHeight / 2 + Math.sin(angle) * radius;
   angle += vr;
  }
 }
}

There is no new knowledge in this code. You can change the angle and radius of the test run results. But what if we only know the location of the object and the center point? It is not difficult to calculate the current angle (angle) and radius (radius) with x,y coordinates. The code is as follows:

var dx:Number = ball.x - centerX;
var dy:Number = ball.y - centerY;
var angle:Number = Math.atan2(dy, dx);
var radius:Number = Math.sqrt(dx * dx + dy * dy);

This kind of rotation based on coordinates is only good for the rotation of a single object, especially if the angle and radius can be determined once in a while. However, in a dynamic program, it is sometimes necessary to rotate multiple objects, and their relative position to the center point may change. Therefore, for each object, it is necessary to calculate the distance, angle and radius, but also to use VR to increase the angle, and finally to calculate the new x,y coordinates, each frame. This is too much trouble, and not very efficient. It's okay, we have a better way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.