Cocos2dx-无法设置速度= 0.0 [英] Cocos2dx - Unable to set velocity = 0.0

查看:132
本文介绍了Cocos2dx-无法设置速度= 0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用cocos2dx做台球游戏. 首先,我使用此参数PhysicsMaterial(1.0f, 1.0f, 0.8f)设置edgeBox 然后这两个球PhysicsMaterial(1.0f, 1.0f, 0.5f)

I'm making an pool game with cocos2dx. First, i setup the edgeBox with this parameters PhysicsMaterial(1.0f, 1.0f, 0.8f) And then these 2 balls PhysicsMaterial(1.0f, 1.0f, 0.5f)

在更新功能上,我希望通过不增加重力来逐渐降低球的运动时间(例如使地面产生摩擦) physicsBody->setLinearDamping(0.3);

On the update function, i want slow down balls time by time without gravity (like making ground friction) by adding physicsBody->setLinearDamping(0.3);

在更新功能上,我设置了最小速度,如果每个球的速度低于15,则将速度重置为0,0

On the update function, i set the minimum velocity, if the velocity of each ball reaches lower than 15, reset velocity to 0,0

auto MV = 15;
auto v1 = player1->getPhysicsBody()->getVelocity();
auto v2 = player2->getPhysicsBody()->getVelocity();
if (v1.x > MV || v1.x < -MV ||
    v1.y > MV || v1.y < -MV) {
} else if(v1 != Vec2(0,0)) {
    player1->getPhysicsBody()->setVelocity(Vec2(0,0));
    CCLOG("sx 1 : %f %f",v1.x,v1.y);
}

if (v2.x > MV || v2.x < -MV ||
    v2.y > MV || v2.y < -MV) {
} else if(v2 != Vec2(0,0)) {
    player2->getPhysicsBody()->setVelocity(Vec2(0,0));
    CCLOG("sx 2 : %f %f",v2.x,v2.y);
}

一切正常,除非球靠近墙或彼此相邻.我看到了这些物体上的蓝色小胶,这是建立联系的时间.

Everything works fine except when the balls stand next to the wall or each other. I see the small blue glue to these objects, this is when the contact has been made.

在这种情况下,我无法将速度设置为0,0. 我认为某种力量会不断改变速度.您可以看到下面的图片,看到蓝色的胶水,并像永远一样将速度设置为0.0.

And in these situation, i can't set the velocity to 0,0. I think there is some kind of force constantly changing the velocity. You can see the image below to see the blue glue and keep setting velocity = 0.0 like forever.

推荐答案

在将速度设置为零之前首先重置力:player2->getPhysicsBody()->resetForces();

Firstly reset forces before setting velocity to zero: player2->getPhysicsBody()->resetForces();

重力也可能是物体继续运动的原因. 因此,您可以将整个物理学世界的引力设置为零.例如:

Also gravity can be a cause that bodies continue to move. So you can set gravity to zero for whole physics world. For example:

auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setGravity(Vec2(0, 0));

或仅针对特定身体:

player2->getPhysicsBody()->setGravityEnable(false);

或者您可以自定义速度函数:

or you can customize velocity function:

#include "chipmunk.h"

cocos2d::PhysicsBody * pBody = player2->getPhysicsBody();
pBody->getCPBody()->velocity_func = customVelFunc;

其中customVelFunc可以定义为:

void customVelFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
    cpBodyUpdateVelocity(body, cpvzero, damping, dt);
}

这篇关于Cocos2dx-无法设置速度= 0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆