Some details of the collision detection of IOS game templates

Source: Internet
Author: User

We put physical bodies that need to detect collisions (we call this physical body xx)

Xx.physicsbody=skphysicsbody (Rectangleofsize:cgsize (x:100,y:100))

Then set an identifier for the physical body (Bitmasktype is a new class of its own)

Xx.physicsbody? categorybitmask=bitmasktype.xx

Finally set the physics that will collide (such as the response to the event with YY)

Xx.physicsbody? Contacttestbitmask=bitmasktype.yy

Finally add code

Func didbegincontact (contact:skphysicscontact) {

println ("Collision")

}

We can see that there has been a collision (the above part is roughly described, if you have any questions please own Baidu)

Sometimes we find that two physics problems have not yet collided, but the system has responded to the collision event, at which point we might as well set the size of the physical body relatively small.

Sometimes two physical bodies have already had a collision, but the system does not respond to collision events, this is why?

Notice our first code, which is the sentence that sets the size of the physical body, in which we set up a 100*100 physical body, but we do not specify the center point, and the default center is Cgpoint (0,0).

The following figure shows:

A red square indicates that you want to create a physical body, whereas the physical body created by the system is actually a yellow square. This physical body does not appear on the screen, but the physical body is actually colliding. Why is this happening? The reason is Xcode the default is to create a 100*100 square centered on the lower left corner of the coordinate. To solve this problem, we modify the code that creates the physical body.

Xx.physicsbody=skphysicsbody (Rectangleofsize:cgsize (x:100,y:100), Center:cgpoint (x:0.5, y:0.5))

Okay, in the appellate code, we created a 100*100 square with the coordinates (50,50) for ZTE, the Red Square we imagined.

Related Article

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.