Check if all bodies are stopped - cocos2d-x-3.0

I am new to physics engines. I am using Cocos2dx v3.4 with physics engine Chipmunk.
How can I check whether all bodies are stopped or not?

Try method body.isSleeping() during iterating over all bodies.
Link: https://chipmunk-physics.net/forum/viewtopic.php?f=4&t=1103

This is the way I check if one sprite is stopped:
#define STOP_VELOCITY 1.
if (sprite->getPhysicsBody()->getVelocity().x > STOP_VELOCITY || sprite->getPhysicsBody()->getVelocity().x < -STOP_VELOCITY ||
sprite->getPhysicsBody()->getVelocity().y > STOP_VELOCITY || sprite->getPhysicsBody()->getVelocity().y < -STOP_VELOCITY) {
return false;
} else {
sprite->getPhysicsBody()->setVelocity(Vec2(0,0));
return true;
}
}

Related

Unity3d - Collision detection and position inaccuracy

I'm a beginner on Unity3D and I decided to create a 3D Tetris to start learning. Everything was fine so far, but I have a problem with collision detection.
https://gyazo.com/49fbf5798dc67a546c5e187fde8f6096
As you can see on this screen, the blue tetromino is not at the good place. This is due to the fact that the orange tetromino position in X is not precise (-2.9999).
Each block that makes up a tetromino has the tag "Block". The ground has the tag "Ground".
This is how I detect collisions in my OnCollisionEnter method.
if (collision.collider.tag == "Block" && !detectedBefore)
{
for (int k = 0; k < collision.contacts.Length; k++)
{
if (Vector3.Angle(collision.contacts[k].normal, validDirection) <= contactThreshold)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
break;
}
}
}
if (collision.collider.tag == "Ground" && !detectedBefore)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
}
Do you know how to get around this problem? Thank you in advance.

How to get my overlapsphere to work?

This is my error code :
Assets/Scripts/PlayerDamage.cs(166,35): warning CS0219: The variable `hitCol' is assigned but its value is never used
I'm just not sure why it's not working properly. I thought I was using it in the input function under attackVKC. I should mention this is for a multiplayer game in unity.
Basically damage is getting done to the player casting the attack and not to the other players. I'm trying to get an aoe (area of effect) attack.
private Collider[] hitColliders;
else if (Input.GetButton ("Special") && (Input.GetButtonDown ("Fire2") && offcd == true && offccd == true)) {
attackVKC();
StartCoroutine("GlobalCooldown");
StartCoroutine("GlobalCCooldown");
}
[Client]
void attackVKC(){
hitColliders = Physics.OverlapSphere (transform.position, special.rangeVKC, mask);
{
if (GetComponent<Collider>().tag == PLAYER_TAG)
{
CmdPlayerHit(GetComponent<Collider>().name, special.damageVKC);
}
}
Debug.LogError (GetComponent<Collider>().name);
}
I actually sloved this. I wasn't looping through the array and I didn't realise getcomponent was for the gameobject attached. So that's why it was hitting the player using the attack and not other players.

Unable to destroy gameObject?

I am trying to destroy a gameObject once it has collided with two other objects with the following code but it does not work.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
{
Destroy (GameObject.FindWithTag("Pentagon"));
}
}
Can someone offer a correction of my code because I can't seem to figure out how to destroy my "Pentagon"?
The problem is in your first line:
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
You cant have 2 tags on a gameObject. I assume what you want to say is || instead of &&.
What are you trying to achieve in that second block of code? Instead of deleting the rigidbody, maybe just set RigidBody.Enabled = false.

OnCollisionEnter is not working:

Please Guys help, I am new to Unity and programming:
I have a two Bouncing Ball, tag as BouncingBall1 and BouncingBall2, I want when a bullet hit both to destroy and if the time have not exceeded displaySecond and you have destroy the balls you win, but my problem is the OnCollisionEnter is not working. The part of wining is not working the rest are, my code fragment is below.
function OnCollisionEnter(col.collision) {
if ((displaySecond < 30) && (
col.gameObject.tag ==
BouncingBall1 == null &&
col.gameObject.tag ==
BouncingBall1 == null)) {
print("You have won");
}
}
col.gameObject.tag == BouncingBall1 == null
I think the problem is with the way you check if an object is destroyed. This isn't the way to do it. Also, you're checking if two identical lines of code are identical. So this will always return true if the display second is lower than 30.
Try changing it to
if ( (displaySecond < 30) &&
(col.gameObject.tag == "BouncingBall1") ) {
Debug.Log("You have won!")
}
It sounds like you want keep track of whether each ball has been destroyed and display "You have won" when both have been destroyed. This code is a little crude but should achieve this:
var ball1Destroyed = false;
var ball2Destroyed = false;
...
function OnCollisionEnter(col : Collision) {
if (col.gameObject.tag == "BouncingBall1") {
ball1Destroyed = true;
}
if (col.gameObject.tag == "BouncingBall2") {
ball2Destroyed = true;
}
if (displaySecond < 30 && ball1Destroyed && ball2Destroyed) {
Debug.Log("You have won");
}
}
This code assumes that the only collisions will be between bullets and balls (not between one ball and another), this can be done with collision layers.

b2contactlistener issues

I am making an iphone app with cocos2d and box2d and I am having trouble, I followed this tutorial on how to use the b2ContactListener because I need it very badly for my app... http://www.raywenderlich.com/505/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-22 I am trying to detect when two objects collide
It has been one of the hardest things for me to figure out... not nessecarally what he is doing in the game but how to use the b2contactlistener... However, I want to use what he did in that tutorial within my code
this is how I updated my tick method so it would check for collisions every time
-(void) update: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||
(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture)) {
NSLog(#"Ball hit bottom!");
}
}
}
However, I don't know how to change it to work with my two b2Bodies, eggBody and locations.platform...
I believe it is these few lines I must change...
if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||
(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture)) {
NSLog(#"Ball hit bottom!");
}
where it says _bottomFixture and _ballFixture I thought I would plug in my b2Bodies... but now I feel like _bottomFixture isn't even a b2body
:) any help would be great sorry if I confused anybody just post any questions and I will clarify thankyou
_bottomFixture is a b2Fixture object.
It is created when you create the fixturedef of the body by
bottomBody->CreateFixture(&_fixturedef);
the above line returns a b2Fixture.
so you can write
b2Fixture *_bottomFixture = bottomBody->CreateFixture(&_fixturedef);
normally we don't track this b2Fixture object. But in collision detection, we need to use it.