Lame to note 36-cocos2d-x-3.2 box2d physical engine Ray-cast use

Source: Internet
Author: User

You are welcome to repost the original notes with lame legs. for reprinting, please indicate the source:

 

Previous review

This famous saying goes:Deep in the human soul, there are many sleeping forces; awakening these forces that people have never dreamed of, cleverly applied, will be able to completely change their lives.[Arison meilun]

In the previous articles, I learned how to portray the physical body we created in the box2d physical engine in a program. This allows us to see a very intuitive object for further learning and understanding.

 

Procedure

We still use the helloworld project.

1. First, the \ cocos2d-x-3.2 \ tests \ CPP-tests \ Classes \ box2dtestbed directory in 6 file GLES-Render.c, GLES-Render.h, raycast. h, test. CPP, test. h, testentries. copy CPP to the classes folder in our factory directory. Then add the six files to our own project.

2. Create a new class, box2d class. The layer class is used to host the physical world.

3. Add the following content to the header file helloworld. Scene. h:

# Include "GLES-Render.h"

# Include "test. H"

# Include "raycast. H"

# Include "box2d. H" contains the header file

 

3. Add the following code to the bool helloworld: Init () function.

Box2d * view = box2d: viewwithentryid ();

Addchild (view, 0, 2 );

View-> setscale (15 );

View-> setanchorpoint (vec2 (0, 0 ));

View-> setposition (vec2 (origin. x + visiblesize. width/2, origin. Y + visiblesize. Height/3 ));

4. Edit the header file test. h,

Find protected in the class definition: add

Friend class box2d;

 

5. Edit testentries. cpp.

Leave only the following information.

# Include <cstring>

Using namespace STD;

# Include "test. H"

# Include "raycast. H"

Testentryg_testentries [] =

{

{"Ray-cast", raycast: Create },

};

Int g_totalentries = sizeof (g_testentries)/sizeof (g_testentries [0]);

 

6. Edit the box2d. h file.

As follows:

# Pragma once

# Include "cocos2d. H"

# Include "GLES-Render.h"

# Include "test. H"

# Include "raycast. H"

Using_ns_cc;

Class test;

Class box2d:

Publiclayer

{

Public:

Staticbox2d * viewwithentryid ();

Bool box2d: initwithentryid ();

Box2d (void );

~ Box2d (void );

Raycast * raycast;

Test * m_test;

Settings settings;

Testentry * m_entry;

Glesdebugdraw * m_debugdraw; // create an example here

B2world * World;

Virtualvoid draw (Renderer * Renderer, const mat4 & transform, uint32_t flags) override;

Protected:

Customcommand _ customcmd;

Void box2d: ondraw (const mat4 & transform, uint32_tflags );

};

7. Edit the box2d. cpp File

As follows:

# Include "box2d. H"

 

Box2d: box2d (void)

{

}

Box2d ::~ Box2d (void)

{

}

Box2d * box2d: viewwithentryid ()

{

Box2d * pview = new box2d ();

Pview-> initwithentryid ();

Pview-> autorelease ();

 

Return pview;

}

Bool box2d: initwithentryid ()

{

M_entry = g_testentries;

M_test = m_entry-> createfcn ();

Returntrue;

}

 

Void box2d: Draw (Renderer * Renderer, const mat4 & transform, uint32_t flags)

{

Layer: Draw (Renderer, transform, flags );

_ Customcmd. INIT (_ globalzorder );

_ Customcmd. func = cc_callback_0 (box2d: ondraw, this, transform, flags );

Renderer-> addcommand (& _ customcmd );

 

}

Void box2d: ondraw (const mat4 & transform, uint32_t flags)

{

Director * Director = Director: getinstance ();

Ccassert (nullptr! = Director, "director is null when seting matrixstack ");

Director-> pushmatrix (matrix_stack_type: matrix_stack_modelview );

Director-> loadmatrix (matrix_stack_type: matrix_stack_modelview, transform );

GL: enablevertexattribs (cocos2d: Gl: vertex_attrib_flag_position );

M_test-> step (& settings );

M_test-> m_world-> drawdebugdata ();

Check_gl_error_debug ();

Director-> popmatrix (matrix_stack_type: matrix_stack_modelview );

}

Compile and run the program as follows:

We have successfully transplanted the raycast test case from the source code. Is it fun?

As for the principle knowledge, I will continue to learn it later.

 

Summary

Next, I will learn the architecture of box2dtextbed IN THE box2d physical engine, so that my friends can shuttle through the code.

Lame to note 36-cocos2d-x-3.2 box2d physical engine Ray-cast use

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.