Wich is the best way to achieve a side scrolling map in the sparrow's chipmunk wrapper?
We do not have a camera so we need to apply the same force to all the dynamic and static bodies to make them scroll togheter?
Thanks in advance,
Mik
Wich is the best way to achieve a side scrolling map in the sparrow's chipmunk wrapper?
We do not have a camera so we need to apply the same force to all the dynamic and static bodies to make them scroll togheter?
Thanks in advance,
Mik
Hmm, i understand what you mean (especially due to you other message on the forum about using images as basis for a chipmunk world
)
For example (think Mario game), when you have mario on your screen and the screen background and blocks move to the left the physics world should also move.
But your mario character stays within certain bounds of your screen and thus the X / Y of your mario character is always > 0 and < then the width / height of your screen.
Now due to the update mechanism of Chipmunk your mario character would 'fall' off the screen since it updates the sparrow sprite with the new x / y coordinate.
So a solution could be (maybe i am looking in the wrong direction but maybe this can help you):
Somewhere in your code you have the following:
[mSpace step:1.0f / 15.0f]; [mSpace updateShapes];
The updateShapes method updates the position off your mario character. Now what i think would be handy to implement your own updateShapes method which uses an offset (how much have i scrolled to the left / right) in calculating the new x / y position.
Now if you modify the methods like this:
- (void)updateShapes:(float)offset { // We can't pass float using pointers. float objFloat[1]; objFloat[0] = offset; cpSpaceHashEach(mCpSpace->activeShapes, &updateShape, &objFloat); } void updateShape(void *cpShapePtr, void* offsetWrapper) { float* offset = (float*)offsetWrapper; cpShape *shape = (cpShape*)cpShapePtr; if(shape == nil || shape->body == nil || shape->body->data == nil) { return; } if([shape->body->data isKindOfClass:[CMData class]]) { CMData *data = (CMData*)shape->body->data; SPDisplayObject *spObject = (SPDisplayObject*)[data data]; [spObject setX:shape->body->p.x - offset]; [spObject setY:shape->body->p.y]; [spObject setRotation:shape->body->a]; } }
This would of course only work for the x axis, if you also want to do the y axis you would have to add an extra value to the float array.
In this way you don't have to move all the objects in the physics world.
Hi Ronald and thanks for the quick support! I'll be able to test your code snippet only tomorrow, can't wait!
Hmm, i understand what you mean (especially due to you other message on the forum about using images as basis for a chipmunk world [:)] )
Yep! After i got that working sample i suddendly tried to load in it a very large map and i started to face the problem of the scrolling.
I started to work at my most important project, the one that brought me here on Mac, and chipmunk will be its spine so i'll do my best to do not stress you too much but i can't promise nothing atm... don't leave the country!
Hmmm,i'm afraid i'm not in the country
Lol!
I had to change the
[spObject setX:shape->body->p.x - offset];
to
[spObject setX:shape->body->p.x - offset[0]];
because it was giving an error of invalid operands to binary.
The error is vanished but nothing is scrolling, neither if i substitute the offset variable with a fixed float value as 5.0f
What i'm missing?
Hmmm, this is just an idea but what happens when you just scroll the view directly? all your coordinates should be still fine. For example:
Begin situation: Sparrow (viewport)
-------------------------------------
O ##
######## ##
#####################################
-------------------------------------
Begin situation: Chipmunk (whole level)
-------------------------------------
O ##
######## ## ##
#########################################
-------------------------------------
Where O is the mario character. So everything still lines up nicely in sparrow and chipmunk (same coordinates).
If the player would move to the right the effect could be:
Updates situation: Sparrow (viewport)
-------------------------------------
## O
####### ## ##
#####################################
-------------------------------------
Updated situation: Chipmunk (whole level)
-------------------------------------
## O
######## ## ##
########################################
-------------------------------------
So the player now has a new coordinate for which the X is larger, this still lines up with chipmunk. Since in chipmunk the player also moved to the right.
The # moved to the left which in return makes the X smaller, here we have to find a solution. Because in chipmunk it still has the same coordinate. One solution could be to not associate the CMBody with the SPSprite, in this way the body doesn't update the coordinate of the SPSprite which would mess up the screen. However, since the # is still in the correct position the collision would still occur in the correct spot. Since the player did move.
I haven't tried scrolling with chipmunk before but this is probably one way i would try to solve it.
The physics world is just a fixed world and the sparrow world is what you see, they don't have to line up perfectly as long as the coordinates and the relative positioning of the bodies are correct.
I think i got what you explained and i'm going to try in that way, i'll not stick sprites to their physic bodies.
I'm going to let the physic world live in it's fixed position and to make a visible copy of it wich render the sprites accordingly to the coordinates of the main physic actor in the physic world.
Thanks for the advices!!!
Luigi
Thanks to your advices we have a working sample!
Give it a look, it's not that bad at all: ChipmunkScrollingTerrain.zip
I think your wrapper is more than ready for production and i wish this little sample could help new developers like me to start easily to add physic to their projects... it has been hard for me and i would liked to find something similar two months ago^^'
Now i'm going to face the buoyancy effect that you kindly coded under request.
Tnx again Ronald!!!
Just a word of caution, i think i might have mentioned it before but the buoyancy effect can be tricky sometimes, especially when you want to apply dragging behavior using joints. Other then that good luck!
Yes i remember that note, i think i'll be able to avoid that behaviour (i hope)
Tnx again!
Mik
I tried the project and i'm... wel i'm.... this is really good...
Very well done!
And another side node, i am still making changes to the chipmunk wrapper to include some more features like distance between objects (center of objects) and such. Also some more convenience methods.
So there will be some more commits within the coming week.
As someone said i'm the ultimate cut & paster!
I use to check your github sources very often, i will not miss any update
You must log in to post.