Cocos2d Joystick - cocos2d-iphone

I have been looking all over for help on how to make a CCSprite move and rotate with the movement of a virtual joystick. I have tried examples and sample code, but, none have worked for me. If someone could tell me how to implement a joystick into the HelloWorld layer, that would be wonderful. P.S. The joystick does not need to move the sprite with variable velocity, it just needs to move it in a direction at a constant speed.
Thanks in advance.

First of all, this is a poorly worded question. Normally I would vote you down for this but for some reason I am not going to today.
You say there is no need for variable velocity, but it needs to move in a direction at a constant speed. I assume then that you need a full 360 degree range. What you do is have a joypad sprite on your layer. When you touch the sprite you calculate the angle of the touch (ignoring distance from center since you don't care about variable velocity). Convert the angle to a CGPoint ( ccpForAngle(CGFloat) is helpful for that ) and then use ccpMult(CGPoint, CGFloat) to decide how fast to move your sprite. Then you can use CCMoveBy to move your sprite.
That's one way of doing it at least.

Related

How to apply Orbital Gravity in cocos2d chipmunk

I am building an IOS game in Cocos2d - Chipmunk - Spritebuilder. I need to make a characters orbit around around a point, and I was having quite some difficulty in implementing orbits with real physics.
So far, I have tried two methods, one is creating a distance joint from the player to the planet, and then applying a 90 degree force on the player where an angle is created between the end of the distance joint and an imaginary line drawn at a 90 angle to it. He moves around crazily and this method is not working for me.
I then tried calculating 180 points on the circumference at a radius from the planet (which is the bounds of detecting and implementing its effects on the player) - and then in a scheduled update method [character.physicsbody applyForce:nextCircumferencePoint]; This does not work, as he does not follow the path exactly and is quite far from it. I am thinking that I need to also apply a gravitational force towards the planet which would cause him to circle it. Though I don't know how to calculate that force, apply it, or if it would even help.
A third method which would never work, but was used for testing was to set his position to the next circumference point. He does orbit, but any collision won't work (such as if a piece of space junk goes in his way.) He will simply be positioned right over any other object. This world great if you don't need collisions, are writing your own physics engine. This is not a polished way of doing things, so will avoid it.
Please correct anything I have already done and tell me how it would work, or shed light on other options and how to implement them.
Check out my answer here. It's for box2d, but you can do it with Chipmunk. If bodyA position will change, orbit will be same. You just need to change speed of body movement for your needs, just increase smoothness and slow down your update method.

Rotation and Movement with rigid body in Bullet Physics

I have made a rigid body for the player and have been trying to get the rigid body moving along with the player's controls.
What I mean is that whenever I press forward I want the rigid body to move forward in the direction the player is facing, same with back, left, right. So far I'm able to use apply force to move the rigid body in static directions.
My straight question is how do I move the player's rigid body in the direction the player is facing.
Other Details:
I don't really want to use kinematic bodies if not necessary mostly because their very fiddly at the moment
I'm using glfw3 for input
This is quite amazing that you would not see how to do that after you actually managed to apply forces in static directions to something you configured over bullet.
Come on, you HAVE the skill to figure it out.
Here, just a push in the direction (hehe), hem. Just take the vector of the facing direction (which could be determined by camera, 1st or 3rd view, or even something else...).
Congrats, this vector is your force by a k factor.
You should also modulate this force according to speed, you don't need to accelerate to infinite speed, just accelerate lots at first and then regulate force to tend to desired walk speed.
Then, the side directions are obtained by rotating the facing vector by 90 degrees around the standing axis (most surely the vertical). You can obtain that by simply swapping components and multiplying by -1 one of them. x,y,z becomes y,-x,z
To go backward, its just -x, -y, -z on the facing vector.
So your up key is not bound to 0,1,0 but to facing_dir actually. This facing dir can change with mouse or some other view controls, like numeric keys 2,6,8,4 for example. Or you could drop up,left,right,down for movement and use w,a,s,d like everybody else, and use direction keys to rotate facing direction. (+mouse)
It is much more difficult to obtain the facing vector from mouse movement or direction keys than finding out how to apply the force, so if you already have the facing vector I'm puzzled that you even have a problem.

Keeping Velocity Constant and Player in Position - Sidescrolling

I'm working on a Little Mobile Game with Cocos2D-X and Box2D.
The Point where I got stuck is the movement of a box2d-body (the main actor) and the according Sprite. Now I want to :
move this Body with a constant velocity along the x-axis, no matter if it's rolling (it's a circleshape) upwards or downwards
keep the body nearly sticking to the ground on which it's rolling
keep the Body and the according Sprite in the Center of the Screen.
What I tried :
in the update()- method I used body->SetLinearVelocity(b2Vec2(x,y)) to higher/lower values, if the Body was passing a constant value for his velocity
I used to set very high y-Values in body->SetLinearVelocity(b2Vec2(x,y))
First tried to use CCFollow with my playerSprite, which was also Scrolling along the y-axis, as i only need to scroll along the x-axis, so I decided to move the whole layer which is containing the ambience (platforms etc.) to the left of my Screen and my Player Body & Player sprite to the right of the Screen, adjusting the speed values to Keep the Player in the Center of the Screen.
Well...
...didn't work as i wanted it to, because each time i set the velocity manually (I also tried to use body->applyLinearImpulse(...) when the Body is moving upwards just as playing around with the value of velocityIterations in world->Step(...)) there's a small delay, which pushes the player Body more or less further of the Center of the Screen.
... didn't also work as I expected it to, because I needed to adjust the x-Values, when the Body was moving upwards to Keep it not getting slowed down, this made my Body even less sticky to the ground....
... CCFollow did a good Job, except that I didn't want to scroll along the y-axis also and it Forces the overgiven sprite to start in the Center of the Screen. Moving the whole Layer even brought no good results, I have tried a Long time to adjust values of the movement Speed of the layer and the Body to Keep it negating each other, that the player stays nearly in the Center of the Screen....
So my question is :
Does anyone of you have any Kind of new Approach for me to solve this cohesive bunch of Problems ?
Cheers,
Seb
To make it easy to control the body, the main figure to which the force is applied should be round. This should be done because of the processing mechanism of collisions. More details in this article: Why does the character get stuck?.
For processing collisions with the present contour of the body you can use the additional fixtures and sensors with an id or using category and mask bits. For of constant velocity is often better to use SetLinearVelocity, because even when using impulse velocity gets lost at sharp uphill or when jumping. If you want to use the implulse to change the position of the body, then you need to use the code for the type of this:
b2Vec2 vel = m_pB2Body->GetLinearVelocity();
float desiredVel = mMoveSpeed.x; //set there your speed x value
float velChange = desiredVel - vel.x;
float impulse = m_pB2Body->GetMass() * velChange;
m_pB2Body->ApplyLinearImpulse( b2Vec2(impulse, mMoveSpeed.y), m_pB2Body->GetWorldCenter());
This will allow maintain a constant speed most of the time. Do not forget that these functions must be called every time in your game loop. You can combine these forces, depending on the situation. For example, if the at the beginning you need to make a small acceleration, it is possible to use ApplyForce to the body, and when a desired speed is to use ApplyLinearImpulse or SetLinearVelocity. How correctly to use it is described here: Moving at constant speed
If you use world with the normal gravity(b2Vec2(0, -9.81)), then it should not be a problem.
I answer for this question here: Cocos2D-x - Issues when with using CCFollow. I use this code, it may be useful to you:
CCPoint position = ccpClamp(playerPosition, mLeftBounds, mRightBounds);
CCPoint diff = ccpSub(mWorldScrollBound, mGameNode->convertToWorldSpace(position));
CCPoint newGameNodePosition = ccpAdd(mGameNode->getPosition(), mGameNode->getParent()->convertToNodeSpace(diff));
mGameNode->setPosition(newGameNodePosition);
P.S. If you are new to box2d, it is advisable to read all the articles iforce2d(tuts), they are among the best in the network, as well as his Box2D Editor - RUBE. At one time they really helped me.
I do not know if this is possible but I have an idea:
Keep the circle at a fixed position and move the background relatively. For example, during the course of the game, if the circle has a velocity of 5 towards left then keep circle fixed and move screen with velocity 5 towards right. If circle has 5 velocity towards left and screen has 3 velocity towards right, then keep circle fixed and move screen with 8 velocity towards left and so on. This should allow you to fix the circle at the center of the screen.
Another method would be to translate the entire screen along with the ball. Make everything on the screen an object that can have a velocity. And the x-component of the velocity of the ball (circle) should be the velocity of all other objects. This way, whenever the circle moves, all the other objects will try and keep up with it.

How can I move a ball in Cocos2D,Box2D like flicking a real ball, with touches?

I am trying to learn box2d in cocos2d. I have searched for online tutorials and ray Wenderlich's seem to be the best ones!I am learning in a slow but steady pace! I am currently developing an app in which I will include a ball in the screen, and every time I touch/tap this ball, it is going to move like someone is flicking a soccer ball. I have coded the part for the upward movement(y axis) of the ball, when touching/tapping it. The problem that I encounter is that I can not find a way to give the ball the appropriate angle in order to rotate and move on the x axis. Let me give you an example. Imagine you are flicking a soccer ball, you kick the ball upwards but also there is a spinning force on the ball. I am trying to develop the spinning and moving across the x axis part. I would like to touch the ball, in its lower right "corner" for example and spin and move in a realistic way. Touching it in the lower right "corner" while the ball is in the air, means that the ball will move and spin to the left side of the screen. I hope I clarified the situation! I would be grateful if someone could help me to understand that kind of movement! If you still haven't understand the concept of my game, search for "parksoccer" and you will understand what I am trying to develop.
I did this yesterday only. In this case what you can do is when you touch or tap a screen , you take that point in ccTouchesBegan and then find the position of the ball by GetPostion() method on body. Than you have to calculate the angle between these two points and find in which direction ball will go from these two points. now you have angle and direction in which ball will go so apply linearForce on that by ApplyLinearForce(b2vec2 forceangle, body->GetPosition()). Thats it.
Enjoy.

Bullets trajectory

I need some some help. I'm working on game, and have problem with bullets trajectory. I have enemies and have player. Enemies and player can be positioned in random positions on screen. So when enemies are shooting in player, i need to have some formula to know the bullet trajectory. Bullet must move from enemy to player and move to out of screen bounds, in the same trajectory, like in others shooters. Can some one help me with this? Thanks.
Whilst I can't give you the code in the language you're looking for (don't have it to hand atm), I can explain the process. This requires a bit of trigonometry. Essentially, the most basic thing to do here is calculate the correct X and Y speed of the bullet to simulate "locking on". That can be achieved by working out the x and y distance between the player and enemy, the calculating (most likely using trig) the angle at which to fire, and then basing the xSpeed and ySpeed of the bullet on that angle.
When I first learned how to do this, I overlayed a triangle on top of the enemy which would indicate whether my trigonometry was correct by pointing directly to the player.
Hopefully understanding this process will give you a means to approach it!
There are also a lot of actionscript 3 tutorials on this which you could translate the logic from.
Good luck!