Chipmunk tutorial-5 tracking the motion of a sphere

Source: Internet
Author: User

Tracking the ball's movements

 

NoCodeConnect to the chipmunk simulator. Only after the corresponding method is extended can the position of the image be synchronously updated through chipmunk. This process can be implemented in multiple ways, such as storing a set of vertices to be checked. Fortunately, chipmunk has designed a very simple and abstract process for easy implementation. The following code is written once and rarely needs to be updated, unless one or more details are removed from the Code.

Therefore, the last step of creating a shape will be connected to a special body. Therefore, we also need to store a reference to a uiimageview in the same shape. This means that all the required data is in the middle. Remember, chipmunk always provides us with a convenient function:

View plaincopy to clipboardprint?
    1. cpspacehasheach (cpspacehash * hash, cpspacehashiterator func, void * Data)

This function takes a hash (an array), a function to be called for each element in the array and Optionally a custom data parameter.

This function requires a hash array as a parameter. The parameters in each array are

So, go to the tick method and add this code after the cpspacestep call already there:

View plaincopy to clipboardprint?
  1. // Call our function for each shape
  2. Cpspacehasheach (space-> activeshapes, & updateshape, nil );

 

As we mentioned earlier, the activeshape array needs to call the updateshape function, which will be implemented in the next step. When we implement the floor, we will discuss the active shape, so don't worry about the details.

At present, we need to implement the updateshape function, which is defined at the beginning, preferably after other function declarations:

View plaincopy to clipboardprint?
  1. VoidUpdateshape (Void* PTR,Void* Unused );// Updates a shape's visual representation (I. e. sprite)
  2. And then,ThisCode on the implementation file:
  3. // Updates a shape's visual representation (I. e. sprite)
  4. VoidUpdateshape (Void* PTR,Void* Unused ){
  5. // Get our shape
  6. Cpshape * shape = (cpshape *) PTR;
  7. // Make sure everything is as expected or tip & Exit
  8. If(Shape = nil | shape-> body = nil | shape-> DATA = nil ){
  9. Nslog (@"Unexpected shape please debug here ...");
  10. Return;
  11. }
  12. // Lastly checks if the object is an uiview of any kind
  13. // And update its position accordingly
  14. If([Shape-> data iskindofclass: [uiviewClass]) {
  15. [(Uiview *) Shape-> data setcenter: cgpointmake (shape-> body-> P. X, 480-shape-> body-> P. y)];
  16. }
  17. Else
  18. Nslog (@"The Shape Data wasn' t updateable using this code .");
  19. }

 

Although the last code segment is followed by 1 or 2 lines, many things are still implemented. I add a lot of tests and warnings to let you know what will happen, but it is important to update the position of the view. Note that we have not directly assigned the value of the Y axis, because the coordinate system of chipmunk is different from that of cocoa. The initial value of the cocoa system is 00 in the upper left corner and chipmunk in the lower left corner. Because different frameworks have different coordinate system systems, we need to pay more attention to these issues during implementation. Cocos2d and chipmunk have the same coordinate system.

Since we finally need this sphere for motion, it is time to compile and run it. If you follow this process, the result of the current operation should be that the sphere falls down from the top and then accelerates the movement. After not much time, the Sphere passes through the right side of the ground, this is what we need to do next.

 

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.