So i have a complex body which is combined from a triangle and a rectangle (think this as a fish)
When my fish bounces of a border of my pool i want my fish to turn towards direction of movement (because fish can't swim backwards - duh!)
I've tried "body:setAngle(-body:getAngle())" on collision but no luck there as always
I probably need to get it's moving direction and then rotate it on Enter Frame event.
unfortunately i have no idea about formula
Comments
You probably provide a vector for setLinearVelocity right?
So if you have xVect and yVect for direction, then you can calculate the angle as
secondbody:applyLinearImpulse( 2*math.cos( secondbody:getAngle()-1.5 ) ,2*math.sin( secondbody:getAngle()-1.5 ) ,0,0)
And it seems like it's not working with your code, shoul i try different "bounceback" formulas?
I hate to do this but i've been working on this problem since this morning and i can't figure it out. I believe my trigonometry is really bad
Sadly as you can see in attachment i've got some fish in a fishtank. And sadly i need some working code from you guys. If someone can give me proper functions to
1- bounce the fish (calculate from original angle etc.)
2- Set rotation of body as direction of movement.
I'm using this as a solution to my first problem; secondbody:applyLinearImpulse( 2*math.cos( secondbody:getAngle()-1.5 ) ,2*math.sin( secondbody:getAngle()-1.5 ) ,0,0)
It works, but not perfect. I think there's something wrong with it too...
And @ar2sawseen's solution to my second problem seems not to work.
So thanks in advance. I know i'm asking for a lot.
Believe me i tried :]
Also, what didn't work about @ar2rsawseen's code?
Well basicly it's not setting the direction correctly. ie. my body goes directly left. and the
"angle" reports 180 degrees. I need it to be -180. and i put a "-" before it. but my body doesnt budge. and keeps his head just the opposite way.
I think this is maybe where magic happens, but i couldn't fix it.
if(xVect > 0) then
angle = -angle
end
Firstly I agree that your bounce function seems kind of "interesting", but I don't have any better to suggest right now
Secondly there can be multiple problems, but I'd need to know what happens. Does your object has incorrect direction of image, or image keeps rotating, or something else?
You see, you apply impulse to 0 0 world coordinates, which in most cases is outside of your object, which in most cases would result in rotation of body.
So firstly you could try applying impulse to the center of object as
Thirdly you might be applying rotation to image, whereas you need to apply it to body.
Fourthly body may still be rotating, so you could apply setAngularVelocity(0) before changing angle.
Basically lots of things can be wrong.
So provide anything you can, description of what happens, screenshots, videos using http://screenr.com or even code blocks, and we will try to help you
http://www.screenr.com/pOz7
Is the default state of your fish image is fish looking to the right?
well my fish "image" state is "up"
but my fish "body" state is looking "left" i believe. i could change them to be the same. but i wanna be sure before mess with my physic body.
Do you think thats causing my problem?
EDIT: I've tried every position for my fish "image" and it still don't set "true angle" .
I've attached example project to this post
And here's an example video:
http://www.screenr.com/4cz7
Likes: kontinyu
My fish swims happily in the pond now.
There is only one problem left, when they bounce back, fish "image" needs to flip on y-axis because fish can't swim upside down too. I think i can manage that. but if you have any ideas out of your head, please shoot out
Anyways, thank you :]
So other solution would be using flipped image as second one.
So for example fish.png is your normal image and fish2.png is your flipped image, then simply modify code as: