How to apply linear Velocity?

How to apply linear Velocity?
0

#1
onLoad(){
 window.rb = this.node.getComponent(cc.RigidBody);
window.rb.linearVelocity = 100;
}

linearVelocity applied to rigidBody at start and it works but

onUpdate(dt){
window.rb.linearVelocity = 100;
}

in above function linearVelocity does not work at all, as I want to apply continuous linear velocity on RigidBody also other properties like window.rb.sensor = true not even work in above function

I’m new to Cocos2D-x can someone solve this simple issue?

Thanks


#2

@ousaf You can use the following code:

this.rigidBody.linearVelocity = new cc.Vec2(this.direction * this.speed * deltaTime, this.rigidBody.linearVelocity.y);

this will create a linear velocity in x axis. set direction to 1 or -1 accordingly.


#3

@DanielMatute has a good solution. Thanks Daniel!!


#4

Thanks @DanielMatute your solution works…just curious to know that why

this.rigidBody.linearVelocity.x = this.direction * this.speed * deltaTime;
not works in update(dt){} function but Works in onLoad(){} function

and why
this.physicsCircleCollider.sensor = true;
not works in update(dt){} function as it works in onload(){} function


#5

@ousaf did you call scheduleUpdate()?


#6

@DanielMatute IDK about that, is it like setInterval() function?
I have done the following way but it not still works…

onLoad () {
this.schedule(function() {
this.rigidBody.linearVelocity.x = 1000;
}, 16/1000);
}

#7

@ousaf just call it this.scheduleUpdate(); when the game is initialized;

then work on your update function:

update(dt)
{
//Apply your velocity
}