Bullets trajectory - cocos2d-iphone

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!

Related

How to handle a collision and move player back to original position? [c++, sfml]

So, I'm creating a game and I just started working on the collision system.
The detection is finished, but I can't figure out how to move the player back to his last position (before he collided).
Just doing player.x-- won't work since he could come from the other side of the x axis and so on (pretty obvious), so how would I make the player move back 1 pixel, no matter "where" he collides with a entity?
Hopefully this made sense, my math and English isn't very good. :P
Store the last position and then use it.

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.

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.

Box2D small pocket hole creation

I have a simple question about a game I am making using box2D and cocos2D. I started using the physics engine yesterday so I am rather inexperienced with its usage and capabilities. My game involves rolling a ball around the screen using the accelerometer. I want to add holes to the ground that if the ball rolls into, it will then require a greater acceleration via the accelerometer to escape the hole pocket. I've toyed with friction, linear damping, modifying the accelerometer's gravity vector, and tried adding attractive forces but I haven't had too much success and some of it doesn't really simulate well what I want happening. Basically I just want to create some sensors and give them the properties of a small pocket a ball can fall in to. Any tips and advice is much appreciated. Thanks
I suggest you to use Level Helper . Its an awesome tool to create physic based games.
You can find it here
You could take the y position of the ball every frame and if it is below a certain threshold, then it is in a hole. Based off on this, if the ball is in a hole, reduce the sensitivity. When the ball exits the hole, put the sensitivity back to normal.
As for creating the holes, use Vertex Helper to create bodies that will correspond to your sprites.
If you need more explanation, feel free to ask.
Elaborated:
Now, basically in the picture, I am depicting what I tried to describe earlier. All you really have to do is to change the tilt sensitivity if the player's Y position is below a certain point. I just used 50 as an example.
Some pseudocode:
- (void)update:(ccTime)dt
{
if (player.position.y >= 50) { //If the player's y position is above or equal to 50
if (sensitivity != normalSensitivity) { //We don't need to set it every frame, so lets check
sensitivity = normalSensitivity;
}
}
if (player.position.y < 50) { //If player's position is below our threshold of 50
if (sensitivity != limitedSensitivity) { //Check so we don't set the sensitivity every frame
sensitivity = limitedSensitivity;
}
}
}
Now, as far as Vertex Helper is concerned, it is an open source tool (I believe) that helps you define vertices for custom shapes that can then be copied and pasted directly into your box2d or chipmunk cocos2d project. It can be fond here.
I suggest googling around for tutorials on how this is used. It is very simple, but you may need a quick reference to get you started.
Finally, something to remember, is that box2d can only work with convex shapes, not concave.
A convex shape is a shape where in is impossible to draw a line from any vertex to another without passing through the shape itself. Basically something that has no indentations in it.
I hope this helped. I'm not sure I can elaborate anymore than I have, but if you have more specific questions, feel free to ask.

Cocos2d Joystick

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.