i am using box2d for bouncing the ball the problem arises that ball only bounce in vertical direction when user press left and right i change the position off the ball it working fine. Problem arises whenever it hit the block i set the velocity like (0,5) so if ball hit from top of block it bounce back and its fine but also when moving the ball horizontally ball hit the left or right side and at that point i dont need to set the velocity so how i achieve this in a nutshell i want to detect which side of the block ball hit in contact listner
Related
I'm following the tutorial here to mimic Flappy Bird. At the part to scroll the game scene:
- (void)update:(CCTime)delta {
_hero.position = ccp(_hero.position.x + delta * scrollSpeed, _hero.position.y);
_physicsNode.position = ccp(_physicsNode.position.x - (scrollSpeed *delta), _physicsNode.position.y);
...
}
Ideally the whole world will scroll left, and _hero (which is a child node of _physicsNode) move right with the same speed it would stay still on the screen. But when I run the code on simulator the _hero sprite just blast off to the right at the speed of light (about 10~20 times faster than the scrolling speed of _physicsNode). The _physicsNode and every other things inside it is scrolling left at normal speed as intended.
If I don't give _hero any movement it would scroll along with _physicsNode normally.
I have tried other method like using CCAction at game start:
CCAction *moveAction = [CCActionMoveBy actionWithDuration:1 position:ccp(-scrollSpeed,0)];
CCActionRepeatForever *repeatAction = [CCActionRepeatForever actionWithAction:(CCActionInterval *)moveAction];
[_physicsNode runAction:repeatAction];
And still get the same result. The speed value that _hero receive is always different from the speed that _physicsNode receive.
Could anyone explain to me why this is happening and how to fix it?
I'm using cocos2d 3.3 if it helps.
I finally figured it out.
The _hero node had its positionType set to CCPositionTypeNormalized (because I was trying to center him on the screen) while the _physicNode has its positionType as CCPositionTypePoints. Any change to the node's position (including movement actions) are based on the node's positionType. The result is that when I move the _hero node by 2, it doesn't move 2 points but 2% of the parent node's width instead.
I fixed it by aligning the _hero on SpriteKit using CCPositionTypeNormalized, then switching it to CCPositionTypePoints at game start.
I am working on a platformer game using cocos2d with box2d in which i have ground body that must continuously move to left and right, but doesn't fall when player jumps onto it. I am not able to do so, my body falls when player jump onto it. Provide me some link or code to do so.
I think the best solution is to move the sprite for the platform, not the platform itsef.
Instead of creating a small platform for the jumping, create a rectangle that extends across the entire pit.
Only move the sprite over the region that you want the player to jump on to (i.e. move it left and right so the player knows the "safe" area to jump).
Use the b2ContactListener to detect the collision between the rectangle and the player. If they are in contact and the player is over the moving sprite, then don't do anything different.
If they are in contact and the player is NOT over the moving sprite, disable the collisions response in the PreSolve event of the b2Contact listener and for the entire time the player is in contact with it. The player should fall through to the pit below.
You could also use a sensor body for the moving platform to give you a better "contact mechanism" than just the sprite. So if the player is in contact with the "platform sensor" and the "rectangle across the pit", don't do anything in the PreSolve event. Otherwise, if they are only in contact with the "rectangle across the pit", let them drop through it.
Does this work?
Cocos2d offers two ways to let the player jump up, but by using jumpto() and jumpby(), people could not change the position of the sprite any more when it is jumping up.
How to write a method so that the sprite can jump like the "Super Mario"?
Some time ago I contributed some cocos2d code which made CCMove and CCJump actions stackable. See here. Starting from cocos2d 2.1, you can apply a CCJump actions concurrently with other movements.
However, if you want to deeply fine tune how the controls of your game feel, I'd avoid using CCActions altogether and I'd manage the sprite.position directly by processing player input.
you have to check for two condition for making your player to jump, same as in Super Mario.
Condition 1)
Only jump button is pressed at that time in CCJumpTo action you have to give the next position as the current position , height and no. jumps as one.
id jump = [CCJumpTo actionWithDuration:1 position:ccp(player.position.x, player.position.y)) height:20 jumps:1];
Condition 2)
When jump and forward button is pressed at that time in CCJumpTo action you have to give the next position as the current position.x + the distance you want to cover by jump,this will be static all the time , height and no. jumps as one.
id jump = [CCJumpTo actionWithDuration:1 position:ccp(player.position.x+20, player.position.y)) height:20 jumps:1];
And at the end you have to run jump action on player, hope you found this as solution for your question...!!
I have a problem when I try to update my camera.
I want to change the pitch and yaw of the the camera (where its looking) via the mouse
But I want the mouse to stay positioned to the center of the window.
//where MouseP.x .y is the mouse position
//(which is centered to the current window)
//get old position of the mouse
OldP.x = MouseP.x;
OldP.y = MouseP.y;
//work out the distance traveled
Delta.x = MouseP.x - OldP.x;
Delta.y = MouseP.y - OldP.y;
//update the camera(using distance traveled)
Rot.Yaw -=Delta.x/5;
Rot.Pitch -= Delta.y/5;
//move mouse to the center of the screen
SetCursorPos(CENTER_SCREEN_X,CENTER_SCREEN_Y);
the problem is the camera snaps back to a certain point as the mouse is set to return to the origin.
I want to update the camera by the distance traveled from the origin but not the distance back to the origin.
If I take it out, it works wonderfully but the then mouse can go out of the window.
One has to be careful with cursor and 3D mouse movement. People tend to think it is related, but in fact it is not.
read the article from the msdn:
http://msdn.microsoft.com/en-us/library/windows/desktop/ee418864%28v=vs.85%29.aspx
"Taking Advantage of High-Definition Mouse Movement"
this is how one must get mouse input in a 3D application.
the cursor should be hidden.
If you try to recenter it, it will create a horrible jaggy feeling where the cursor is trying to escape the center with the user moving the mouse but is kept in place by an invisible spring. Which doesn't look very profesional. You can't fight that because your application is not scheduled before mouse cursor display.
I believe the issue here is that your block of code probably is inside the catch of a WM_MOUSEMOVE event?
When you call SetCursorPos, it itself generates another WM_MOUSEMOVE event, so you process this block of code once when you move the mouse, and once again when you call SetCursorPos and it does the opposite of this.
You probably don't want to put SetCursorPos inside of a WM_MOUSEMOVE event catch, or else you'll generate an endless loop of messages (every SetCursorPos generates another one).
Perhaps you can move this code outside of the message pump, and just run it once per frame in your update loop: query the current mouse position, make your camera transform, then set the cursor back to the origin.
if(g_States::Instance().MouseLook())
{
//Test the mouse input
POINT mousePos;
GetCursorPos(&mousePos);
mouseX = mousePos.x; //g_InputEngine::Instance().GetX();
mouseY = mousePos.y; //g_InputEngine::Instance().GetY();
mouseX = mouseX - m_HalfWidth;
mouseY = mouseY - m_HalfHeight;
mouseFloat = mouseX * C_MOUSESMOOTHINGFACTOR;
g_Scene::Instance().GetCamera()->RotateYaw(-mouseFloat);
mouseFloat = mouseY * C_MOUSESMOOTHINGFACTOR;
g_Scene::Instance().GetCamera()->RotatePitch(mouseFloat);
//Reset mouse to center on the screen
SetCursorPos(m_HalfWidth,m_HalfHeight);
}
So this is the Mouselook function of a spacegame prototype I was developing for fun a while back what I did was change it to use GetCursorPos(&mousePos); instead. This will get the current position of the cursor no matter when your input code updates the mouse cursor location. The rest of the math in the function is just for the sensitivity and actually rotating the camera. Hopefully this code helps a little getting yours working.
Let me know if you need more explanation.
Edit: I just remembered the reason i did this. It was because the screen was flickering it would move but then the input engine would get updated by the SetCursorPos() call because i was using WM_MOUSEMOVE to update the input engine. I'm not sure how your getting your input but this should still help you.
I have designed a small tutorial named "Stacker", As the name suggests, The game involves stacking blocks on each other. I have a large number of blocks to be stacked and hence all cant be accomodated in the screen itself... I m new to cocos2d and box2d but have managed to create a body with its adjoining sprite wen a user clicks on the screen. I have used MouseJoint to give movement to the body till the user performs the drag action that is till the user takes his finger off the screen.
The problem is that i need to follow the sprite (actually need the camera to follow the sprite) when the user drags it above the screen space, i referred the following links with no success... i guess wat i need is to move the body to a virtual coordinates which m not getting coz even if the screen does shift using the camera methods, but the sprite doesnt move with respect to the screen...
cocos2d forum link
flash concept but box2d
Can some1 guide me in case i need to have some pre-requisites before following camera in the manner i specified.. Thanx!
Ok Guys!
Got it guys! Had to take a global variable which records the increments per frame, The increments were equal to the layer movement which i did by setting the position of the layer to a unit less in every frame! Then set the Mouse join to the target which is (ScreenCoordinates + increment) dis too has to be done in every frame!
Done!
Cool method but needed a bit of brainstorming!!