Movement problems

Hi everyone.

I’ve got this little issue with my game I’m developing and I’m feeling kind of stupid to not have solved this already. I’m not sure if it’s physics related but because most of the objects in-game uses physics related movement, I’ve decided to add this thread into the physics category.

I’ve got a scene model (a maze) that has a Mesh collision shape. I also have a cube (the enemy) that moves either horizontally or vertically, depending on what I’ve specified. It has a dynamic ‘rigidbodycontrol’ attached to it. Using the prePhysicsTick, I move my object using setLinearVelocity (which I specify using a getter and setter method to retrieve a vector) and the angular factor set to 0.

In my Main class, I created two new Nodes called ‘Enemy’ and ‘Maze’. I then use a ‘collision listener’ to see whenever Enemy collides with Maze. The following code gives a better example.

[java]
@Override
public void collision(PhysicsCollisionEvent pce) {
if(“Enemy”.equals(pce.getNodeA().getName()) || “Enemy”.equals(pce.getNodeB().getName())) {
if(“Maze”.equals(pce.getNodeA().getName()) || “Maze”.equals(pce.getNodeB().getName())) {
if(enemyDir.x != -10) {
enemyDir.x = -10;
} else {
enemyDir.x = 10;
}
enemyHandle.setMovement(enemyDir);
}
}
}
[/java]

The enemyDir is a vector I use as an argument for enemyHandle.setMovement(), which of course, is the getter and setter method to add the vector as an argument for setLinearVelocity in the EnemyHandle class. At default, the enemyDir is set to 10 on the x-axis.

The problem is, whenever it collides, the cube shakes furiously, as if it’s rapidly changing its direction. If I remove the ‘else’ statement, it will perfectly move right and then left again after it collided with the maze wall without any movement problems. But, I want it to move it right and left again every time it collides with the maze wall.

Can anyone help me with this issue? I will appreciate any help.

Thanks guys.

You get collision callbacks as long as you collide, so you probably bounce into the wall, reverse direction and in the next frame you still bounce into the wall and change the direction again…

1 Like

Thanks Normen for the help! Truly appreciate.

Based on what you said, I fixed the issue using a timer and a boolean to tell the object whenever to move again after it collided with the wall.