I'm working on a game and I need to detect collisions between a line and a polygon. After much googling, it seems that cocos2d does not support polygon collisions?
I'm able to perform line and rect collision detection, but not polygon.
Do I have to move to Box2d? Any advice is much appreciated.
Cocos2D doesn't offer any kind of collision detection. What you want is a ray vs polygon intersection test, that's something you can do with Box2D or Chipmunk. Both are included in Cocos2D (for iPhone). Box2D and Chipmunk allow for implementation of the game physics like gravity, wind, collision detection etc.
If you require to do a 'sort of' detection in plain Cocos2D then I recommend the use of CGRectContainsCGRect or CGRectContainsCGPoint or CGRectIntersectsCGRrect functions to do the same. This may help you achieve what you want but it won't be as nice as the use of Box2D or Chipmunk game physics.
cocos2d offers just simple collision detection like CCRect::rectContainsPoint(in cocos2d-x) or rectIntersectsRect.
If your polygon is a rectangle then you could make the detection by judging if there is a point of the line contained by that rectangle.
Related
I'm currently in the process of developing a very basic 3D OpenGL game in C++ as part of a small college project. We don't get a lot of insight from the teachers however, and only very limited documentation, as well as a small timeframe, so I'm kind of a little lost here at the moment.
My game is a tank battle on an orthogonal plane that pretty much looks exactly like the image I sketched below. Each tank (A and B) can be controlled by a different player, and each one can shoot projectiles, which are supposed to influence the other tank's score upon collision.
My question is, what would be the simplest way of effectively implementing collisions for the tanks? (Tank vs tank, tank vs map boundaries and tank vs any kind of parallelepipedic object like the one in the center of the picture - and the same thing but applied to the projectiles shot from the tank turrets).
Ideally, without the need of using an external physics engine, but also accepted if the implementation can be done easily. At the moment, I'm solely using the GLUT library.
Download and integrate Box2D (http://box2d.org) into your project.
Unless your project is to implement a physics engine, then don't bother doing it yourself. Your time will be much better spent learning how to integratate libraries and how proper physics engines work.
You can then easily use a box collider for your tanks, circle for projectiles and 4 lines for your perimeter. You can create callbacks to notify you when a projectile has collided with another tank.
You will have to use forces and torques to move and rotate your tanks, rather than just updating their positions. But you would probably have to do that anyway if you were going to implement the physics yourself.
I'm using Cocos2D, or more specifically Cocos2D-x to make a platformer-shooter game. I already have the platformer part, but I want to make my character shoot. I've seen in many ios games characters are able to shoot wherever they want and I wonder, do they make tons of sprites for this, because they can also fun while aiming at other sides, or do they sort of bend their sprites. How can I achieve this effect? Thanks
This SO question deals with rotation.
I believe it's feasible to compose a shooting figure from a sprite featuring the body and another sprite, which rotates on top of a joint and is composed of arm and gun. Does it look good? There's definitely some artistic skills required to make the joint look natural. An alpha channel could be enough though -- or some people deliberately make the heroes wear futuristic armors with spherical joints.
all! I'm creating a game based on Box2d and Cocos2d. I want to make up a rope, which will be able to contact with all of the physical objects. Something like this:
http://rghost.ru/35987917.view
I created a rope by using a rectangles joined through a revolute joint, but creating more than 3 ropes reduces fps from 60 down to 30. I must use at least 5 ropes, water and particles in Cocos2d, but in this way fps will be near 5-10, how to avoid this?
Use a box2d rope joint and verlet rope from this tutorial:
http://www.cocos2d-iphone.org/archives/1112
There shouldn't be any performance issues with 5 ropes with this method.
You may have to update your box2d version though because b2RopeJoint is relatively new.
I have a sprite using spriteWithFile.
I have collision detection on this sprite which currently only detects on the middle of the image. The middle of the image is currently detecting collision, so half half the image will be over the collision point when the detection is made.
How do I change this detection point on the character sprite?
Thanks
What about using some physics engine? Box2D for example. It's coming with cocos2d, it's fast, it's easy to use.
I'm planning to build a basic rebound iOS game in Cocos2D. Will Box2D be best suited for the following scenario?
The layout will consist of a target at the top of the screen, with obsticles in the middle of the screen, blocking direct view of the target from the bottom. The user will shoot a ball from the bottom of the screen by rebounding it of the sides and around the obsticles, aspiring to hit the target. Similar to a breakout style of game.
As the game levels progress, the obsticles will be moving dynamically, left to right and back, up and down etc..
I understand that the collision detection can be achieved using Cocos2D alone. Can the rebounding and trajectory of the ball off obsticles and walls also be achieved without using a physics engine?
I will have no need for gravity in the game scenario. A reduction in the velocity/speed of the ball will be essential.
Please note, I am new to iOS dev, coming from a background in front-end web dev.
Advise and help much appreciated.
Thanks
You certainly can do all that without using an existing physics engine. Once you start doing collisions with moving objects and objects of different shapes, though, it starts to become advantageous to use an out of the box solution.
It's easy to setup box2d without gravity and it will give you all of the collision calculations and velocity stuff