How do I give a Box2d Kinematic body a predefined curved path - cocos2d-iphone

I'm new to using Box2d and really can't find a consistent answer to my question so please excuse me.
I'm writing a game for the iPhone -- who isn't these days -- and I'm using Cocos2d and Box2D. I have objects that move around in the game and I would like to give them a more of a curved path. From my studies I know that I should use body of type b2_kinematicBody which allows me to change the linear velocity every tick and keeps them from colliding with my other flying bodies. I do understand that if I know my current position (x,y) and my next position in the step (x1,y1) I would be able to compute the vector for velocity. Does anybody have a good way to give an object a defined curved path? I'm thinking it has to be some kind of parametric equation.

Take a look at bezier curves. After curve defenition you can compute the derivative (it's easy for bezier) and use it as a velocity for your body

OH whats the game called? sounds like a good one. have you looked into maybe just doing that in version 2.0

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.

Tracing a bezier curve from an image for a cocos2d game

I was wondering if there is a tool that will allow me to construct/trace a closed bezier curve based on a background image?
Basically I have a background image that represents some 2d curve which could be of some weird shape like a race track and I want to place some items along this path.
I figured that if I can derive a quadratic bezier curve that will overlap the image I would be able to use the mathematical equations for this curve to compute individual points along its path..
Does anyone know if such tool exists? Is my approach reasonable or totally off and there is a much simpler solution?
Thank you in advance.
I suggest building it yourself. It shouldn't be too difficult to build a level creator where you add your own background image, place your bezier key points along where they need to be and export the points into a plist. It'll even give you room for extending it and customizing it for your game.
Also, if you're planning on tracing a path along a road for a racing game, consider constructing the background from smaller road/tree/grass sprites. This way you can give them specific properties (such as canDriveOn, canHit and so on) and based on customized behaviour defined for each one of them, your 'driveable' path would be derived implicitly.

How to find the Circle Collision Response

I'm currently making a 2D pool game where i have to use real physics simulation. I have done the circle collision and elastic collision so far, But I want a formula which can find the collision response velocity, which differs when a ball collides with other at different points thanks in advance.
There are many approaches, but if you can afford to make the balls overlap a bit (that is, to make part of the ball "enter" the other) you can turn it into a spring-damper system and solve with Hooke's law.
Since Hooke's law give you the force, you need to integrate it to find the momentum. Momentum divided by mass is the velocity you're looking for.
Take a look at this excellent intro of spring physics for game development (that also has a link for his intro on numerical integration).
edit: if you're looking for a practical solution I suggest the Box2D physics engine.
Pool collision can be considered as elastic collision throughout the board, with friction slowing down its movement.
Don;t think the collision response in terms of angles to the circle. Using vectors will ease the problem.
The circle-circle collision velocity response is easy:
1. When collision is detected
2. FInd the normal velocity of the balls acting toward the other ball.
3. Interchange the Normal velocity between the two balls
4. Resolve the velocity in x and y direction
A very helpful webiste to find the velocity response in terms of vectors: http://archive.ncsa.illinois.edu/Classes/MATH198/townsend/math.html

how to make a box2d body move along a Bezier curve/ arc path

I've searched extensively on how to make a Box2d body move along a Bezier curve or arc-like path with a start point, end point and possibly another control point. I know cocos2d objects can be moved around using ccBezier but how can it be done for Box2d bodies? Please help. Thanks in advance.
I have tried to move a box2d body using a curve (making a spider walk around an asteroid). I found that using SetTransform every frame worked, but it made the collision response in the engine fail.
I was able to find a much better solution by using prismatic joints. You create a joint starting at the position (p0) you want to start from and pointed towards the next waypoint (p1). Then you let the joint push the body from p0 to p1 using the motor. When the body gets very close to the next point, destroy the joint and form a new one for the next two points. The body will still have its velocity so the motion looks smooth.
I put a more complete post on this on Stack Overflow here.
Was this helpful?

GJK collision detection implementation from 2D to 3D

I apologize for the length of this question and give a pre-emptive thanks for anyone who reads through this!
So i've spent the last few days going over the GJK algorithm. I understand the general concepts behind it, and understand the most of the nitty gritties of its implementation in 2D thanks to the wonderful article by William Bittle at http://www.codezealot.org/archives/88 .
I've implemented his pseudo code (found at the end of the article) into my own c++ project, however i want to make a 3D implementation. My weakness comes into using the dot products to test the voronoi regions and the tripleProducts to get perpandicular lines. But im trying to read up more on that.
My problem comes down to the containsOrigin function. Im having trouble visualizing and accounting for the new voronoi regions that the z axis adds. I just can't seem to wrap my head around how to determine which regions contains the origin. I assume there is 4 I have to account for, each extending from the triangular planes that the comprise the 4 faces of the tetrahedron simplex. If the origin is not within any of those regions, then it is contained, and we have a collision.
How do i go about testing if it is contained in a particular voronoi region/ which triangular face is pointing in the direction of the origin?
The current 2D algorithm checks if a triangle is made, if not, then the simplex is a line and it finds the 3rd point. I assume the 3D algorithm with check if a tetrahedron is made, if not, then it will check for a triangle, if true then it will to find a 4th point to make a tetrahedron(how would i get this? using a normal in direction of origin?). If i trangle isnt made, it will find a 3rd point to make a triangle (do i still use triple product for this like in 2D?).
Any suggestions, outlines, resources, code augmentations, comments are much appretiated.
Depending on what result you expect from the GJK algorithm you might want to look at this nice tutorial from Molly Rocket: https://mollyrocket.com/849
Be aware though that his implementation only outputs intersection? yes/no. But it might be a nice start.