Is it possible to adjust the sound volume cocos2d? - cocos2d-iphone

I am using the following method, and express a sound effect in cocos2d.
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"BackGround.m4a"];
But sound volume of BackgroundMusic is too small.
so, is there a way that adjust a sound volume?

[[SimpleAudioEngine sharedEngine]setBackgroundMusicVolume:1.0f];
From 0.0f to 1.0f.

The backgroundMusicVolume property of the SimpleAudioEngine class can be used for this.
- (float) backgroundMusicVolume [read, write, assign]
//Background music volume. Range is 0.0f to 1.0f. This will only have an effect if willPlayBackgroundMusic returns YES

Related

OpenAL 2d panning C++

I'm trying to figure out how to get openAL to pan in 2D (by manipulating the 3D positioning). Ideally I want to achieve panning such that the Left or Right channel can be fully engaged with the other channel completely silent. It seems that Open AL handles 3d distances and falloffs nicely, but I'm struggling to emulate this kind of 2D panning.
I'm using
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED)
float sourcePosition[3] = {0.99f,0.f,0.f};
alSourcefv(sourceID, AL_POSITION, sourcePosition);
alSourcei(sourceID, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcef(sourceID, AL_MAX_DISTANCE, 1.f);
alSourcef(sourceID, AL_REFERENCE_DISTANCE, 0.5f);
However there is a substantial amount of audio in the right channel. I don't really want gain to drop off based on distance, just proportion the channels.
Is it possible to emulate 2d panning with open AL?
You'll want to set AL_SOURCE_RELATIVE to AL_TRUE, rather than false.
AL_SOURCE_RELATIVE set to AL_TRUE indicates that the position,
velocity, cone, and direction properties of a source are to be
interpreted relative to the listener position.
So says the OpenAL 1.1 Specification (page 34)!
So, changing
alSourcei(sourceID, AL_SOURCE_RELATIVE, AL_FALSE);
to
alSourcei(sourceID, AL_SOURCE_RELATIVE, AL_TRUE);
should achieve the desired result.

Cocos2d - Collision Detection of Rotated Sprite

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!

Several newbie questions for Physx 3

Today, I tried to play around Physx and Physx visual debugger and as always, newbies have problems and questions. I'll try to describe my problems as best as I can with my poor english skills.
1) I managed to create a physx scene. added a dynamic actor and manipulated it. I see in Visual Debugger it's motion. It's a standard PxSphereGeometry ball. However, when I add a second ball in the scene, the second one is not visible, but I can see that collision happens. Here's the code and if anyone can point me what's wrong with it I'd be very grateful:
PxMaterial* mMaterial;
mMaterial = mPhysics->createMaterial(0.5f, 0.5f, 0.5f); //static friction, dynamic friction, restitution
if(!mMaterial)
error("createMaterial failed!");
PxVec3 position(0, 50, 0);
PxRigidDynamic* aSphereActor = PxCreateDynamic(*mPhysics, PxTransform(position), PxSphereGeometry(3), *mMaterial, 1.f);
PxRigidDynamic* aTrActor = PxCreateDynamic(*mPhysics, PxTransform(PxVec3(3, 1, 1)), PxSphereGeometry(3), *mMaterial, 1.1f);
if(!aSphereActor)
error("Unable to create sphere actor");
aSphereActor->setMass(1);
aTrActor->setMass(10);
PxRigidStatic* plane = PxCreatePlane(*mPhysics, PxPlane(PxVec3(0,1,0), 0), *mMaterial);
if (!plane)
error("create shape failed!");
mScene->addActor(*plane);
mScene->addActor(*aSphereActor);
mScene->addActor(*aTrActor);
while(true)
{
mScene->simulate(1.0f / 30.0f);
if(!mScene->fetchResults(true))
error("cant fetch result");
Sleep(10);
}
In this scene, aSphereActor collides with aTrActor, but I can't see aTrActor in Visual Debugger, however collision is perfectly visible.
2) Nvidia's documentation is very very poor. It's a torture for newbies like me to find it's way through it. So I wanted to know how can I import a 3d model and add it in the scene. I know there is a Physx plugins for 3ds max, maya etc. Say I have a model exported with this plugin, how can I import it in my app and add it to the scene?
3) During the creation of the scene
sceneDesc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
, what value should I provide to get a real gravity, the one we have on earth
4) I can assign a mass to an actor, however I don't know in which measurement unit the mass is. For example, if I set aSphereActor->setMass(1); will aSphereActor will be 1kg, gram or what?
Thank you very much everyone. I appreciate your help.
First, I am doing my first Physx project this quarter. (read as, I might be making this up)
1)
You don't check aTrActor's creation, but I don't think that's your problem.
Check if aTrActor in your draw/update callback.
2)
Dunno
3)
-9.81 m/s^2 is the acceleration for Earth's gravity.
I'm guessing that the PxVec3 is the gravity with respect to each axis.
So, PxVec3(0.0, -9.81, 0.0) is no x or z acceleration and -9.81 m/s^2 y acceleration.
4)
The answer to #3 would suggest the units are metric.
You can probably throw it all together in standard, but metric > standard imo.
Just looked at the date, this may not help Davita, but hopefully it'll be of some use to someone.

Animate CCSprite on Each Update

I have a CCSprite object of which I need to update the on screen (x,y) position as quickly as possible. It is an augmented reality app so the on screen position needs to appear fixed to a real world location.
Currently, during each update I check the heading and attitude of the device then move the sprite accordingly by determining the new x and y positions
[spriteObject setPosition:ccp(newX, newY)];
Each degree change in heading corresponds to 10 pixels in on screen position, so by setting the position this way the sprite jumps around in intervals of 10 pixels which looks stupid. I'd like to animate it smoothly, maybe by using
[spriteObject runAction:[CCMoveTo actionWithDuration:0.2f position:ccp(newX, newY)]];
but the problem here is that a new position update comes in while the sprite is animating and it sort of screws the whole thing up. Anyone know of a nice solution to this problem? Any help is much appreciated as I've tried numerous failed solutions to this point.
You can try to just animate your sprite movement to the point. I mean, you can several times during one second run animated position correction with duration of 1/numberOfUpdates in one second. Something like
- (void) onEnter
{
[super onEnter];
[self schedule:#selector(updatePositionAnimated) interval:0.2f];
}
- (void) updatePositionAnimated
{
[spriteObject runAction:[CCMoveTo actionWithDuration:0.2f position:ccp(newX, newY)]];
}
I suppose, you will have smooth enough animation in this case

cocos2d - Showing only part of a sprite -- irregular shape

I'm really going crazy trying to figure this out, so any help would be really appreciated. I'm trying to hide most of a sprite and show it gradually. This works fine if I only work with rectangles. For example, I found someone's implementation of the "ClippingNode" class and it worked well, namely, this part of the code:
-(void) visit
{
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(clippingRegion.origin.x + positionInPixels_.x, clippingRegion.origin.y + positionInPixels_.y, clippingRegion.size.width, clippingRegion.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
The problem is I need an irregular shape, not just a rectangle. I was hoping I could stack calls to glScissor and create a shape with many smaller rectangles, but unfortunately glScissor only works once (the last time it was called).
It seems that cocos2d doesn't support OpenGLs stencil buffer (does it?) and even if it did, I find OpenGL so hard to understand, I'd still need someone to explain it to me. If I could set a bezier path on the sprite as a mask (which I think you can do in Quartz), that would be great, but it doesn't seem like that's supported.
Please, if anyone has any bit of wisdom here, that'd be great!
Figured it out. You can call glScissor multiple times, you just also need to draw that scissored shape each time:
-(void) visit
{
NSEnumerator *enumerator;
NSValue *val;
CGRect aRegion;
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
enumerator = [regions objectEnumerator];
while ((val = (NSValue *)[enumerator nextObject])) {
aRegion = [val CGRectValue];
glScissor(aRegion.origin.x, aRegion.origin.y,
aRegion.size.width, aRegion.size.height);
[super visit];
}
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
It isn't possible with glScissor, but you could easily achieve this effect using the stencil buffer. Here is the documentation:
http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node117.html
There is also a NeHe tutorial on the stencil buffer, but it is in C++, not Objective C (though it should be easy to translate into whatever application you need):
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=26
EDIT: This is based on the assumption that you want to clip it to some arbitrary shape, for example a star, smiley whatever, instead of just a rectangle.