Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17091

Physics Body Doesn't Rotate With Sprite Problem

$
0
0

@legendaryscrotom wrote:

My issue is that i cant get a sprite and its physics body to rotate to the same angle. They both rotate but the physics body rotates a bit quicker and eventually ends up rotating completely off of the sprite, which is bad because i'm using it to detect the sprite's collision. I have no idea what it could be so any information that might work will help. Also i'd love tips because this is my first program and i'm sure it's written horribly.

Code:

touchListener->onTouchBegan = [=](Touch* touch, Event* event)->bool
{
touchLocation = touch->getLocation();

Vec2 touchLocation = touch->getLocation();
Vec2 offset = touchLocation - projectile->getPosition();

float diff_X = touchLocation.x - projectile->getPosition().x;
float diff_Y = touchLocation.y - projectile->getPosition().y;
Point diffPoint = Point(diff_X, diff_Y);

float angleRadians = atan2f(diffPoint.y,diffPoint.x);

angleRadians = CC_RADIANS_TO_DEGREES(angleRadians);
angleRadians = GameScene::changingAngleAccordingToCoordinateSystem(projectile->getPosition(), touchLocation, angleRadians);

projectile->setRotation(-angleRadians);

MyBodyParser::getInstance()->parseJsonFile( "bullet.json" );
auto physicsBody = MyBodyParser::getInstance()->bodyFormJson(projectile, "bullet", PhysicsMaterial(0, 0, 0) );

physicsBody->setDynamic(true);
physicsBody->setCategoryBitmask((int)PhysicsCategory::Projectile);
physicsBody->setCollisionBitmask((int)PhysicsCategory::None);
physicsBody->setContactTestBitmask((int)PhysicsCategory::Monster);
projectile->setPhysicsBody(physicsBody);

}


float GameScene::changingAngleAccordingToCoordinateSystem(Point imageCenter, Point touchLocation, float      calculatedAngle)
{
//Consideration :- all Angles is in Degree
if((calculatedAngle >= 0 && calculatedAngle <= 90) || (calculatedAngle >= 90 && calculatedAngle <= 180)){
    //Ist quardant
    calculatedAngle = calculatedAngle;
}
else if(calculatedAngle < 0 && calculatedAngle >= -90){
    //IInd quardant
    calculatedAngle = 270 + (90 + calculatedAngle);
}
else if(calculatedAngle < -90 && calculatedAngle >= -180){
    calculatedAngle = 180 + (180 + calculatedAngle);
}

 return calculatedAngle;
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17091


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>