[Cocos2d-x] Day 3 - MouseDrag Box Create & RayCast Collision

|


 <DAY> 2016.05.20

 < WORK LIST >
 <Class> Object_Hero.cpp & Box_Manager.cpp

 - MouseDrag Box Create

 - RayCast Collision

 - Gravity Jump Revise



   1

  MouseDrag Box Create


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Scene_Tower.cpp
 
bool SceneTower::init()
{
    this->setIsTouchEnabled(true);
}
 
CGPoint SceneTower::getMousePos( NSSet *_pTouches, UIEvent *_pEvent )
{
    CCTouch * pTouch = (CCTouch*)_pTouches->anyObject();
    CGPoint   pos    = pTouch->locationInView(pTouch->view());
    CGPoint   tpos   = CCDirector::sharedDirector()->convertToGL(pos);
 
    return tpos;
}
 
void SceneTower::ccTouchesBegan( NSSet *pTouches, UIEvent *pEvent )
{
    CGPoint pos = getMousePos( pTouches, pEvent );
    startPoint  = pos;
}
 
void SceneTower::ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent)
{
    CGPoint pos = getMousePos( pTouches, pEvent );
}
 
void SceneTower::ccTouchesEnded( NSSet *pTouches, UIEvent *pEvent )
{
    CGPoint pos = getMousePos( pTouches, pEvent );
 
    CGRect rc;
    rc.origin = startPoint;
    rc.size   = CGSizeMake( pos.x - startPoint.x , pos.y - startPoint.y );
 
    this->addChild( ST::call()->BM.AddBox( rc ) );
}
cs






   2

 RayCast Collision



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Box_Manager.cpp
 
bool Box_Manager::Raycast( CGPoint _start, CGPoint _vec, RaycastHit *_hit, float _Distance )
{
    for( _hit->fDistance = 0; _hit->fDistance < _Distance; _hit->fDistance += _Distance * 0.0005f )
    {
        _start.x += _vec.x;
        _start.y += _vec.y;
        
        Object_Box * p = CollisionCheck( _start );
        if( p != NULL )
        {
            _hit->ObTarget = p;
            return true;
        }
    }
    return false;
}
 
cs





   3

 Gravity Jump Revise


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Object_Hero.cpp
 
case DROP:
{
    m_fAccelerator += GRAVITY * dt;
    ST::call()->BM.Raycast( pos, CGPoint(0-3.f), &Hit, m_fAccelerator );
    pos.y          -= Hit.fDistance;
 
    if( Hit.fDistance < 0.1f )
    {
        m_fAccelerator = 0;
        m_iState       = IDLE;
    }
    break;
}
 
cs



평 가

 .난이도 : 보통

 별   점 :  

 한 마디: 충돌과 관련된 RayCast를 만

듬으로써 충돌의 선택지가 넓어지게 

되었다. 




<Progress List> 

 - Key Control

 - Gravity Jump -> <Complete List>

 - Box Create -> MouseDrag Box Create -> <Complete List>

 - Box CollisionCheck


<Complete List> 

  - Gravity Jump

  - MouseDrag Box Create

  - No Data

  - No Data






And