My question is same as in the title. I’m making a board game with dice. I want to drag and throw (fling) the dice in the direction user swipes and speed must be according to the user’s swipe speed. I’m using cocos2d-x 3.8 c++, default physics; chipmunk. Any help from you guys will be much appreciated.
Don’t have much idea about Chipmunk, but the below fragment should get you started. Sorry for the clumsy code though (am at office
):
// HelloWorld.h
float swipeTime; // Delta Time
float swipeDistance;
// HelloWorld.cpp
HelloWorld::init(){
swipeTime = -1;
swipeDistance = 0;
this->scheduleUpdate();
}
HelloWorld::update(float dt){
if(swipeTime > -1){ // Starts counting frames since touch
swipeTime++; // Increments by 1 for every frame (which is 0.0167 seconds @ 60 fps)
}
}
HelloWorld::onTouchesStart(touches, event){
swipeTime = 0;
}
HelloWorld::onTouchesEnded(touches, event){
swipeDistance = touch[0]->getLocationInView()->distance(touch[0]->getStartLocationInView());
float swipeAngle = touch[0]->getLocationInView()->getAngle(touch[0]->getStartLocationInView());
float swipeSpeed = swipeDistance/(swipeTime * 0.0167f);
swipeTime = -1;
}