Confusing box2d forces - cocos2d-iphone

Hello Friends.
This is my demo game screen-shoot. Here i am using three buttons. Right-bottom button is used for jump and left-bottom buttons used for move left and right.
I have some questions
1) should i use linearImpuls for jump body??
2) For move right and left which types of force i applied???
PLease tell me i am confusing to use linearImpuls, applyforce and linearVelocity.
Thanks in advance

for jump you use LinerImpulse
b2Vec2 locationWorld;
locationWorld = b2Vec2(0.0f,8.0f);
double Force= _body->GetMass();
_body->ApplyLinearImpulse(Force*locationWorld, _body->GetWorldCenter());
for move left also use it but b2Vec2(5.0f,0.0f)

For move left/right you can use velocity. When you need to stop your character, just set velocity to (0.f, 0.f). For jumps use impulse. If your b2World has gravity, it will calculate jump trajectory by itself.

Related

Cocos2d V3 - Getting current sprite position?

Probably quite a simple question! I'm making a side scrolling shooter game using Xcode5, with Cocos2d Version 3.
When the main character (on the left of the screen) flies up and down, I want the enemy (on the right of the screen) to follow him along the Y axis.
I've set up an action so the enemy enters, then moves to the main character's Y position. But the enemy moves to the main characters starting Y position, not it's current Y position.
Is there code to get the current position of a sprite? Or is there perhaps a better way to achieve this? Basically we want the enemy sprite to know the main characters Y position and move to it continuously.
Thanks
Every CCNode has a position property.
#property(nonatomic,readwrite,assign) CGPoint position;
So use this:
CGPoint position = yourSprite.position;

SDL drawing a sprite once a button is pressed

So, i'm a complete stranger to SDL and i found this nice code online:
http://gamedevgeek.com/tutorials/animating-sprites-with-sdl/
I was just wondering how to make it so that when i press space a shape gets placed infront of me? For instance, im just walking around and when i press space a rectangle or another bmp is places in front of me.
Sorry for not being explicit in what i want, i just dont know how to explain it.
You need:
Another Surface to draw (blit), made from the rectangle.bmp. This would use the same method as is used for the grass (or the player if you wanted animation).
Knowledge of where "in front" is: up, down, left or right. Look at the code and see what variables change when you press one of the arrow keys. (Hint: don't use rcSprite.) In a larger game, you would want to define a new variable for the direction that the player is facing, and then use that for both the sprite animation and for placing the rectangle, as it would make the code easier to understand.
Some new code in HandleEvent, which does something if the key is SDLK_SPACE.
Then calculate a position to place the rectangle (say, the player's position with 50 added to "x" if they were facing right), and draw the rectangle in the same way as the grass.
In general, look at what code has already been written, and Google for stuff you don't know (e.g. the name of SDLK_SPACE).
Good luck

how to change the position of player using left and right button when the player is jumping?

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...!!

Sprite does not jump when moving

I'm making a platform game, and everything was great. My sprite moved when i touched the left side of the screen, and jumped when i touched the right side. But then i decided to make it move by itself. so i added the ccmoveto function, but now it does not jump! I'm new to cocos2d but everything is working ok, except this, already searched but couldn't find the answer can someone please help me?
I tried everything, but it only jumps if i delete the ccmoveto action.
I'm using cocos2d 2.0
Thank you!!
CcMoveTo will override any manual position changes, inluding changes from other actions like CCJump. Your character is set to move to destination in a straight line, no matter what.
It's issues like these why I always recommend not to use actions for gameplay logic. Especially position, you need to retain full control over it. Use a direction vector and integrate position every update: and you're free to do everything you need.
my advice is to use one of the physics engines provided with cocos2d: Box2D and Chipmunk physics. Thanks to this engines you can define the characteristics of the world (i.e. gravity vector) a shape and a mass for your sprite (i.e. a rectangle with a weight). Then when you need it to jump you will just create a force vector with the characteristics you need (i.e. angle, etc.) and keep updated your sprite with its physical body. This will make your sprite jump and land quite realistically.

How can I change the position of the mouse cursor in OpenGL/Glut?

I'm writing a simple game and I'm going to have the mouse control the camera (using GlutPassiveMotionFunc).
I'm going to pitch and yaw based off the mouse difference between callbacks, however I think it would be a good idea to "force" the mouse back to the center of the screen every time they tried to move it. This way their cursor won't be at the edge of the screen and they can't move any further in that direction.
What Glut / OpenGL command can I use to force the position of the mouse to change?
Use glutWarpPointer(x, y), where x and y (both ints) are in pixels (relative to the window's origin). For example:
glutWarpPointer(windowWidth / 2, windowHeight / 2);
Sorry for the late answer. What i meant was that after i use glutWrapPointer function, if i print out the x and y values, i could not catch the change on the cursor and it always prints out the values inside the glutWrapPointer function that i provided. What i did to fix it was to move this function inside the Animate function of openGl and it worked fine.