Collision Detection in midp1.0

Source: Internet
Author: User
Tags gety
Document directory
  • Body


 

Body

In midp1.0, we cannot use collision functions as easily as the sprite class in midp2.0. We can only write code for implementation by ourselves. The common collision detection method is rectangle-based collision, because our images are rectangular. One way to detect rectangular collision is to check whether the four corners of a rectangle are inside the other. Assume that we have an actor class, which is a custom kind similar to the genie class. Then we can define a method like this:

  1.  
  2. /**
  3. * Checks whether a specific vertex enters an actor.
  4. * @ Param px x coordinate of a specific point.
  5. * @ Param py y coordinate of a specific point
  6. * @ Return if a specific vertex is in the actor range, true is returned; otherwise, false is returned;
  7. */
  8. Public BooleanIscolsponingwith (IntPx,IntPY)
  9. {
  10. If(PX> = getx () & PX <= (getx () + getactorwidth ())&&
  11. PY> = Gety () & py <= (Gety () + getactorheight ()))
  12. Return True;
  13. Return False;
  14. }
  15.  
  16. /**
  17. * Checks whether an actor object meets the current actor object.
  18. * @ Param another actor object
  19. * @ Return if another is in conflict with the current object, true is returned; otherwise, false is returned.
  20. */
  21. Public BooleanIscolpolicingwith (actor another)
  22. {
  23. // Check if any of our corners lie inside the other actor's
  24. // Bounding rectangle
  25. If(Iscollidingwith (another. getx (), another. Gety () |
  26. Iscolsponingwith (another. getx () + another. getactorwidth (), another. Gety () |
  27. Iscolsponingwith (another. getx (), another. Gety () + another. getactorheight () |
  28. Iscolsponingwith (another. getx () + another. getactorwidth (),
  29. Another. Gety () + another. getactorheight ()))
  30. Return True;
  31. Else
  32. Return False;
  33. }
  34.  

For rectangular collision detection, another easier way is to determine whether the four sides of a rectangle are out of the four sides of the other rectangle. Therefore, we can write a more general and fast simple collision method:

  1.  
  2. /**
  3. * More common rectangular Collision Detection Methods
  4. * @ Param ax a coordinate X in the upper left corner of the rectangle
  5. * @ Param ay y coordinate in the upper left corner of the rectangle
  6. * @ Param aw a rectangle width
  7. * @ Param ah a rectangular height
  8. * @ Param BX B x coordinate in the upper left corner of the rectangle
  9. * @ Param by B y coordinate in the upper left corner of the rectangle
  10. * @ Param bw B rectangle width
  11. * @ Param bh B rectangular height
  12. * @ Return
  13. */
  14. Public Static Final BooleanIsintersectingrect (IntAx,IntAy,IntAw,
  15. IntAh,IntBX,IntBy,IntBW,IntBH ){
  16. If(By + bH <ay |// Is the bottom of B abve the top of?
  17. By> Ay + Ah |// Is the top of B below bottom of?
  18. BX + BW <ax |// Is the right of B to the left of?
  19. BX> AX + aw)// Is the left of B to the right of?
  20. Return False;
  21.  
  22. Return True;
  23. }
  24.  
  25.  

This is much faster. For images with a transparent background, we can set up multiple Rectangular areas around the non-transparent part for collision detection.

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.