Shooting projectiles with angle Problem

I want my projectile to start at the red dot everytime I shoot

barrel is a child of my gun’s body

problem:

my formula:

radius = SCALEX(barrelInView->getBoundingBox().size.width) + basicProjectile->getBoundingBox().size.width/2;
Vec2 center = body->getPosition();
float angle = body->getRotation();
        
finalX = center.x + radius * cos(angle);
finalY = center.y + radius * sin(angle);

result of the formula from log :

angle -15.912188
body center x:362.000000 y:519.799988
radius 126.000000
finalX 238.618454 finalY 545.353760

what’s wrong with my formula?

I do something similar and solved the issue by putting a dummy node where I want my projectiles to fire from (exhaust particles in my case). I didn’t need to calculate anything at all…

It seems ‘radius * cos(angle)’ is negative! use:

finalX = center.x + radius * (-cos(angle));

Hi @trojanfoe I thought of doing that too, thanks.

hello @mralizadeh I will try your computation, thanks :smile:

Still won’t work. I guess i’ll have to do @trojanfoe method for now

look at Vec2::rotate

1 Like

Thanks! got it already.