Box2d tutorial 2-mouse Interaction

Source: Internet
Author: User

Box2d tutorial 1-create a collision world

Box2d tutorial 2-mouse Interaction

Box2d tutorial 3-Rigid Body binding

Box2d tutorial 4-complex appearance of complex rigid bodies

Box2d tutorial 5-Collision Detection

The mouse interaction of box2d is not to allow the mouse to interact with the displayed object, but to create a mouse joint. the two ends of the joint are the rigid body and the ground respectively. When you click the mouse, create a very small area, traverse and obtain the rigid body under the area. After obtaining the rigid body, create the mouse joint. As the mouse moves, the joint rigid body moves along. When the mouse is released, the mouse joints are destroyed.

ImportantCodeAs follows:

 1   Private   Function Handleenterframe (EVT: Event): void
2 {
3 // Update mouse position
4 Updatemouseworld ();
5 // Constantly call the mouse drag Method
6 Mousedrag ();
7
8 VaR Timestep: Number = 1/30;
9 VaR Velocityinterations: Int = 10;
10 VaR Positioniterations: Int = 10;
11
12 World. Step (timestep, velocityinterations, positioniterations );
13 // In version 2.1, the force is removed to improve efficiency.
14 World. clearforces ();
15 // Draw
16 World. drawdebugdata ();
17 }
18 Private Function Updatemouseworld (): void
19 {
20 // Mouse position in the physical world
21 _ Mousexworldphys = This. mousex/pixel_to_meter;
22 _ Mouseyworldphys = This. Mousey/pixel_to_meter;
23 // Mouse position on the screen
24 _ Mousexworld = This. mousex;
25 _ Mouseyworld = This. Mousey;
26 }
27 Private Function Getbodyatmouse (includestatic: Boolean = False ): B2body
28 {
29 // Create a small boundary area
30 _ Mousepvec. Set (_ mousexworldphys, _ mouseyworldphys );
31 VaR AABB: b2aabb = new b2aabb ();
32 AABB. lowerbound. Set (_ mousexworldphys-0.001, _ mouseyworldphys-0.001 );
33 AABB. upperbound. Set (_ mousexworldphys + 0.001, _ mouseyworldphys + 0.001 );
34 VaR Body: b2body = Null ;
35 VaR Fixture: b2fixture;
36
37 // Get the rigid body under the mouse in the form of a callback function
38 Function Getbodycallback (fixture: b2fixture ): Boolean
39 {
40 VaR Shape: b2shape = fixture. getshape ();
41 If (Fixture. getbody (). GetType ()! = B2body. b2_staticbody | required destatic)
42 {
43 VaR Inside:Boolean = Shape. testpoint (fixture. getbody (). gettransform (), _ mousepvec );
44 If (Inside)
45 {
46 Body = fixture. getbody ();
47 Return False ;
48 }
49 }
50 Return True ;
51 }
52 World. queryaabb (getbodycallback, AABB );
53 Return Body;
54 }
55
56 Private Function Mousedrag (): void
57 {
58 // If you press the mouse, but there is no mouse Joint
59 // Create mouse Joints
60 If (Mousedown &&! _ Mousejoint)
61 {
62 VaR Body: b2body = getbodyatmouse ();
63 If (Body)
64 {
65 VaR MD: b2mousejointdef = new b2mousejointdef ();
66 Md. bodya = World. getgroundbody ();
67 Md. bodyb = body;
68
69 Md.tar get. Set (_ mousexworldphys, _ mouseyworldphys );
70 Md. collideconnected = True ;
71 Md. maxforce = 300.0 * body. getmass ();
72 _ Mousejoint = World. createjoint (MD) as b2mousejoint;
73 Body. setawake ( True );
74 }
75 }
76 // If the mouse is released, the mouse joints are destroyed.
77 If (! Mousedown)
78 {
79 If (_ Mousejoint)
80 {
81 World. destroyjoint (_ mousejoint );
82 _ Mousejoint = Null ;
83 }
84 }
85 // If you already have a mouse joint, the joint target is always set to the mouse position.
86 If (_ Mousejoint)
87 {
88 VaR P2: b2vec2 = new b2vec2 (_ mousexworldphys, _ mouseyworldphys );
89 _ Mousejoint. settarget (P2 );
90 }
91 }
92
93 Public Function Handlemousedown (E: mouseevent): void
94 {
95 Mousedown = True ;
96 }
97
98 Public Function Handlemouseup (E: mouseevent): void
99 {
100 Mousedown = False ;
101 }

Download source code

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.