I need to scale a sprite's height exactly to a fixed value. I don't think the function scaleBy() or scaleTo() could help. Please help me to figure it out.
Each CCSprite object has properties for the scale, scaleX and scaleY. In your case, you should use the scaleY property and do some simple maths :
sprite.scaleY = DESIRED_HEIGHT/sprite.contentSize.height;
But you have to make sure that your desired height is in float, otherwise you might get some issues, like always having 0 or 1 as a result!
If you want to animate it, you can also use the CCScaleTo action like this :
float scaleY = DESIRED_HEIGHT/sprite.contentSize.height;
[sprite runAction:[CCScaleTo actionWithDuration:duration scaleX:sprite.scaleX scaleY:scaleY]];
Hope this helps!
Related
I want to apply a velocity vector to a dynamic body in the cursor direction:
void Game::mousePressEvent(QMouseEvent *e){
double angle = atan2(realBall->GetPosition().y - e->pos().y(), realBall->GetPosition().x - e->pos().x());
realBall->SetLinearVelocity(b2Vec2(-cos(angle) * 50, -sin(angle) * 50));
}
But the dynamic body has an incorrect direction, so i think that the cursor position it's wrong.
Thank you for the help!
First, you must know that in order for your code to work, the coordinates of your screen and the coordinates of box2d must match. Be aware that if you use screen coordinates in pixels, it means that the size of one pixel matches an 1 meter in box2d. But let’s assume that you have already taken all this into account. Then I would not advise you to use trigonometry for calculations. So you can easily make a mistake. In this case, simple vector operations will be enough for you: substraction, scaling and normalizing a vector. You can try this: velocity = (cursor_position - real_ball_position).normalize().scale(50f). In box2d there is a b2Vec class for vector operations. You can read about it in detail in the documentation.
I've been trying to understand the following code, but I don't seem to gain control over what the parameters are doing. I have to draw half an ellipse at a certain location. Could anyone explain to me what the parameters of the path mean in order to master this shape. Thanks.
var curve4 = paper.path("M150,150 A100,70 0 1,1 150,10")
.attr({"stroke-width": 2, stroke: "red"});
Ok I solved the syntax:
paper.path("M x_start,y_start A width,height rotation 1,direction x_end,y_end")
About that 1 between rotation and direction I'm not sure. It's probably also involved in the direction bit. I created a fiddle to play around with the ellipse parameters: https://jsfiddle.net/ansjovis/vgw3vdc8/4/
So I was programming a game in cocos2d-x and I need one of the sprites to get wider for a certain amount of time, so I tried with the method setScaleX(). The problem is that the content size of the sprite does not actually change, and since my collision system is based on the content size of the sprite, they do not properly work. Here is the code I use for scaling:
bar = Sprite::create( "Bar.png" );
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);
bar->setScaleX(1.5);
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);
The output is exactly the same on both cases. Is there any way of fixing this?
ContentSize represent the original texture size unless you set it using setContentSize method.
You can either multiply size with scale factor or use boundingBox().size to know about current size of scaled sprite(if not rotated or skewed).
cocos2d::Size size1 = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size1.width/768;
scaleY=size1.height/1024;
Sprite *sp1=Sprite::create("01.png");
sp1->setScaleX(scaleX);
sp1->setScaleY(scaleY);
this->addChild(sp1);
Scale for Landscape:
cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/1024;
scaleY=size.height/768;
Scale for Portrait:
cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/768;
scaleY=size.height/1024;
Sprite *sp=Sprite::create("01.png");
sp->setScaleX(scaleX);
sp->setScaleY(scaleY);
this->addChild(sp);
I have 2 points A and B. The distance is 100 and my sprite image is 50. My question is can I resize the sprite from the center of the image in order to keep the quality and if its possible how can I do that? I tried with this code, but it just scale the image width and look awful.
-(void)resizeSprite:(CCSprite*)sprite toWidth:(float)width toHeight:(float)height {
sprite.scaleX = width / sprite.contentSize.width;
sprite.scaleY = height / sprite.contentSize.height;
Not sure if this is what you're after - but if you just want to scale the sprite to twice the size without artefacts you can use linear filtering on the sprite texture
[sprite.texture setAliasTexParameters];
Then just scale it to whatever you like
[sprite setScale: 2.f];
I try to detect collision between two sprite.
if(CGRectIntersectsRect([SpriteA BoundingBox], [SpriteB boundingBox]))
But when i Rotate any sprite than collision detection is not perfect..
I know to use pixel perfect Collision but i have no idea about it.
Please anyone help me for how to detect collision, Give me any block of code if any.
You can use box2d to make it detect all collisions for you
In two ways you can do.
Use box2D body for your sprite. Example: CLICK HERE
Use CGMutablePathRef, and use CGPathContainsPoint() instead of CGRectIntersectsRect.
Example: CLICK HERE
You can also refere the Ray Wenderlich Tutorial for the detection of the Collision between any 2 Box2D bodies.
it's possible! try with CGPath.
I had the same problem. I've resolved with this tutorial: http://bobueland.com/cocos2d/2011/the-magic-of-cgpaths/
for rotate the path try this method, it rotated the path round the center of the boudingBox:
-(CGPathRef) rotateCGPath:(CGPathRef)path corner:(CGFloat)radians
{
CGRect bounds = CGPathGetBoundingBox(path);
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGAffineTransform transf = CGAffineTransformIdentity;
transf = CGAffineTransformTranslate(transf, center.x, center.y);
transf = CGAffineTransformRotate(transf, -radians);
transf = CGAffineTransformTranslate(transf, -center.x, -center.y);
return CGPathCreateCopyByTransformingPath(path, &transf);
}
after this you detect the collision simple with:
if (CGPathContainsPoint(Collisionpath, NULL, collisionPoint, NO))
{ //is inside the path }
good luck!