I am creating a game like bounce ball using cocos2d-x and box2d. I have different objects like rectangle, square etc.. I am able to detect collision, but i want to detect collision only on top of the objects. What exactly i want is, when the ball is on the top of the object, then only i want to jump the ball.
But, when the ball is collide on remaining side(bottom or left or right) i don't want to jump the ball.
In touchbegan, i am using the following code to the bounce ball. So every touch it is jumping when it collide with remaining side.
if(_ball->boundingBox().intersectsRect(rect->boundingBox()))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
Any advice?
Following steps can solve your problem-
1) CCRect projectileRect = CCRect(float x, float y, float width, float height);
if(_ball->boundingBox().intersectsRect(projectileRect))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
2) - Make body of an object and then check their collision.
I got the solution for my question from the below link.
http://www.raywenderlich.com/28606/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-2
Related
I'm making a Pong game in Actionscript 3 and I'm pretty much done it, however I need to create a feature where if the paddle passes the top of the screen it goes back to the bottom, and if it passes through the bottom it appears at the top. How would I create something like this?
Y axis is for up/down so use paddle.y to check the position...
Some logic like this :
if ( paddle.y <= (-paddle.height) )
{ paddle.y = ( stage.stageHeight - paddle.height ); }
Just an example you can test and refine. This assumes your paddle is on stage directly. If the paddle was instead added to some container (Sprite or MovieClip) then instead you use:
{ paddle.y = ( containerMC.height - paddle.height ); }
If you want an effect where, for example, the top-half of your paddle is above screen then there is a half-paddle at the bottom... for that you just use two instances (copies) of the paddle Sprite.
I am making a game in cocos2dx(c++).Here i have to make dynamic sprites that are falling top to down and on touch i have to kill them.I mean the sprites are of insects and they will be killed on touch
The problem is that I am unable to get the idea on how to implement them as on increasing the level the insects will be falling more and with more speed.
I have made for one insect.But don't know how to handle for multiple insects
Suppose, your 1st Sprite is coming from y height and it fell to -y/2 with a velocity of 10.
Then, you have to make several sprites with differnt velocities and different positions of x.
You can move a sprite in cocos2dx with the help CCMoveTo()
You can use this custome method to move sprite
void GameLayer::_mov(CCSprite *cp, float x, float y)
{
cp->setPositionX(cp->getPositionX()-x);
cp->setPositionY(cp->getPositionY()-y);
}
In this method we passing sprite which we have to move and x is how much change in sprite position and y is how much change in sprite position. In this I subtract x and y from the current position of sprite but you can add also according to your requirement.
You have to call this method in your update method.
Here's what i want to do:
I want to load a model (most likely .3ds) into my .swf and be able to rotate it with the mouse.
This works fine at first glance, but there's problem, the rotations 'switch' over. Or to say it differently:
Imagine we have a model in the shape of a pipe.
If we drag the mouse to the right, the model rotates along its X-Axis to the left, mouse to the left, X-Axis rotation to the right, moving the mouse up, Y-Axis rotation downward, mouse down, Y-Axis rotation upward.
Now, lets say we turn the pipe to the right or left, until we face the (former) 'backside' of the pipe, and then we move the mouse down. The model will rotate downward instead of upward.
I hope you understand what i mean with this. I've been looking around for a good while now and never found a satisfying solution. There was talk about quaternions, but i can't grasp them.
Another suggestion i read somewhere is the following:
create a Matrix3D object, apply rotation on it, then multiply it with the desired Matrix3D of my 3d-Model.
I tried to do it, but the result stays the same, the directions of rotation switches depending on what side i'm facing.
private function update(e:Event):void
{
xCalc = (0.3*(stage.mouseX - lastMouseX));
yCalc = (0.3*(stage.mouseY - lastMouseY));
if(move)
{
var objTransform:Matrix3D = new Matrix3D();
objTransform.prependRotation(xCalc, Vector3D.Y_Axis, objC.pivotPoint);
objTransform.prependRotation(yCalc, Vector3D.X_Axis, objC.pivotPoint);
mesh.transform = multiply3DMatrices(mesh.transform, objTransform);
}
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
view.render();
}
multiply3DMatrices simply multiplies two 4x4 Matrices together.
objC is the ObjectContainer3D that holds my model. For some reason i cannot rotate it properly, unless i manipulate the rotationX/Y/Z properties.
mesh is the mesh inside of the Model (which is very simple, a single mesh).
Also, i'm aware that i could try another Framework for this (like papervision) but this project requires me to use Away3D.
Solved it by myself, the problem was that i created a new Matrix3D Object every time. The fixed code looks like this:
private function update(e:Event):void
{
...
if(move)
{
var objTransform:Matrix3D = mesh.transform;
objTransform.appendRotation(xCalc, Vector3D.Y_Axis, objC.pivotPoint);
objTransform.appendRotation(yCalc, Vector3D.X_Axis, objC.pivotPoint);
mesh.transform = objTransform;
}
...
}
And yes, the user bwroga was actually right, i should've used appendRotation instead of prependRotation, as well.
i've been making a twist on the labyrinth game and i've got my ball to move with physics but im struggling with getting it to hit the walls around it. its currently a movie clip with black walls, and ive used this code to try and stop it:
if (character.hitTestObject(walls)){
character.x = //something
character.y = //something
}
all this does is when it hits any part of the movie clip, (even the blank spaces) it moves my character,
is there any sort of code i can use to maybe detect hitting a certain colour?
One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.
hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your character to see if it's touching the wall :
// I am assuming that x,y is the top left corner of your character
if (wall.hitPointTest(character.x, character.y, true))
{
// top left collided with wall
{
So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the character.
Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/
I'm writing a paint program that uses shape brushes to draw by using the matrix function.
Everything works well aside from the fact that it's not smooth at all. There will be gaps in the painting if the mouse is moved at a high speed.
I've looked everywhere but haven't been able to find any solution.
The code basically looks like this:
//Press mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseDown_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove_drawContainer);
_layout.drawContainer.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp_drawContainer)
}
//Move mouse within container. Uses Matrix to draw instances of the brush.
private function handleMouseMove_drawContainer(e:MouseEvent):void
{
_matrix.identity();
_matrix.translate(mouseX - 10, mouseY - 30);
_layout.bitmapData.draw(_layout.brush, _matrix);
}
If anyone could help me figure out how to smooth out the drawing, I'd be forever grateful! =p
Thanks in advance.
You probably need some kind of interpolation between the mouse positions... there are of course many ways, I'll describe one very easy to implement but a bit hard to fine tune. Basically instead of drawing in each mouse position, you use an easing equation that follows the mouse with some delay... this way the described line will be a bit smoother, and will draw a few times between each mouse position.
So instead of doing (pseudocode):
onMouseMove {
draw(mouseX, mouseY);
}
You do something like:
x = 0;
y = 0;
onEnterFrame {
x += (mouseX - x) * 0.2;
y += (mouseY - y) * 0.2;
draw(x, y);
}
Although maybe what you really need is a way to limit the maximum distance between points, so if the mouse moves more in one frame, you interpolate points between the two positions and draw as many times as it's needed.
Or if you're looking for smoother lines (avoid sharp corners) maybe you also need to use beziers to control the resulting line.
Anyway, it all depends on the kind of drawing you're looking for.