Having problem while moving sprite

hi.
I have a problem with moving sprite using buttons.

I used this code for moving sprite by holding buttons (left and right button for moving left and right).

and person is an object that have these fuctions (right movement is also the same).

the problem is when person collide with another object (both of them have physics) then part of person will going into part of object and after that the person will pushing object with his direction.I want to know how should i solve this problem (I use cocos2dx version 3.6).

thanks for your attention

Are you using bitmasks on your physics objects?
http://cocos2d-x.org/programmersguide/12/index.html#collision

1 Like

First thanks for your answering …

Yes…but the problem is when they collide just part of person will pass throw the object like the image below :

in this picture the bigger square is person we talked about and the little one is object
and while you holding left button nothing happens because the object is between person and the wall and if you release the left button the person will move a little to right (for it’s physics) and again, if you push left button the person will move a little to left (like above picture) i don’t know what to do for this maybe rewrite moving function or something else …

thanks for your attention

I’d guess that the issue is that you are using a moving action that doesn’t properly take into account the physics (I don’t use the inbuilt physics or actions much, so I’m guessing here)

Whenever using physics I try to move everything via forces - so moving right would apply a suitable force to push the object to the right, and not change its position directly; that way tunnelling rarely if ever causes an issue.

1 Like

yeah i think it can be done by impulse either but the problem is i want the friction to be 1 that objects won’t reflect when collide each other… i don’t have any idea what should i do. i will be thankful for any idea.
thanks in advance for your answer and for your attention.

To prevent objects bouncing, the thing to set is not a high friction but a low restitution (i.e. 0) then objects won’t bounce but just stop when they collide (assuming one is static)

It’s difficult to judge exactly what you are doing - but if you are just using physics for collision detection this can be done efficiently using Sensors rather than ‘physical objects’ - so you control all the movement, jsut detecting when a sensor touches something.

1 Like

for example i want to make a game that has a main character, we name it person and has object we name it, box. the game is very simple that person move around the map and when he collide with box he will naturally push the box what should i do for this?

I guess it depends on how ‘realistic’ you want it to be.

Using a sensor will only tell you that your sensor is touching something - so you would have to calculate new postions for your box as your person moves (but that’s not hard - Box->location = player->location + Vec2(playerWidth, 0) when they are touching - but you also have to handle not moving the box if it is touching something else.

Using physics simulation will mean your collisions and movement need to be handled by the physics simulator - which means applying forces rather than changing locations of physical objects.

From what you describe I would go with just the sensor to detect things are touching, and handle all the movement yourself - or you can even just handle all the collision detection yourself by comparing your sprite’s positions against one another (this is what we had to do back in the day when physics simulation was just a dream that processors just couldn’t cope with!

1 Like