Body Collision Contact Point Monitoring

Source: Internet
Author: User
Tags instance method stub

From: Himi "Android game programming from scratch" www.himigame.com

A collision listener Interface "Contactlistener" is provided in box2d, which uses an interface that requires rewriting its four abstract functions.

@Override public
	void Add (ContactPoint arg0) {
		//TODO auto-generated Method stub
		//Collision, response to this function
	when there is a new contact point }

	@Override public
	void persist (ContactPoint arg0) {
		//TODO auto-generated Method stub
		// Respond to this function when an existing contact point persists
	} @Override public
	void Remove (ContactPoint arg0) {
		//TODO auto-generated method Stub
		//Response to this function when the presence of a touch point is deleted
	}

	@Override public
	void result (Contactresult arg0) {
		//TODO auto-generated method Stub
		//per time step monitoring, if there is still a contact presence is responded to
	}

Through the instance of the parameter ContactPoint class, we can get the two body instances that collide with each other, get the body instance method as follows:

Body body1 = Arg0.shape1.getBody ();
Body body2 = Arg0.shape2.getBody ();

The application is as follows:

if (arg0.shape1.getBody () = = Bodyball && arg0.shape2.getBody () = = lostBody1) {
	gameislost = true;
} else if (arg0.shape1.getBody () = = Bodyball && arg0.shape2.getBody () = = LostBody2) {
	gameislost = true;
} else if (arg0.shape1.getBody () = = Bodyball && arg0.shape2.getBody () = = Winbody) {
	Gameiswin = true;
}

Finally, make sure to bind the listener to the physical world instance:

World.setcontactlistener (Contactlistener listener);



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.