how to restrict the bugs to move over the obstacle in andengine? - andengine

Hi i m using pathmodifier, to find the path of the sprite when it has reached its previous point.i want to move arround my sprite to different shapes of obstacle.. can anyone suggest me how to do it.. i tried with color pixel but it slow down my game performance.
For example
i drawed line arround the triangle , using the below formula i can find out the intersection point of bugs with this line
double d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
double xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
double yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
.. but not working
can any one suggest me how to do it?
thanks in advance

Related

Make one sprite follow another off centre, including rotation

I'm working on a car drift game, which I want to make skid marks with using the pen.
I created a circle, radius 25 around the car that I am using as a reference for where the tires would be, and I need to get the tire sprite to follow the back end of car according to its rotation. I'm trying to adapt the formulas x'= xcosθ−ysinθ, y'= xsinθ+ycosθ but I don't know how to do it.
I set the initial x value as 0 and the y value as -25, but I know this is wrong because it doesn't stay the same when the car is moved and rotated.
Here is my project:
https://scratch.mit.edu/projects/535410396/
I got the solution from a user of the Scratch Forums. For the x value:
CarX-25*sin(CarRotation)
For the y value:
CarY-25*cos(CarRotation)

Determine the direction an object is actually moving

In unity I am trying to compare the players actual direction with the direction they are facing and wish to move in but having major issues trying to find the actual movement direction.
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
But I cannot figure out how to get the objects movement direction so that I can compare. I have tried:
transform.InverseTransformDirection(rb.velocity);
I would expect this to be equal to 0,90,0 when I move to the right however it is equal to 0,0,0 (although jumps when there is acceleration).
How can I determine the direction an object is moving in?
I can determine the facing direction very easily with:
wishDir = transform.localEulerAngles;
This is already quite odd to me. localEulerAngles is a rotation in Euler space notation in degrees per axis x,y,z .. this is no "direction".
Usually if you want the direction you are looking in you would rather use transform.forward
wishDir = transform.forward;
And then
transform.InverseTransformDirection(rb.velocity);
should indeed return the direction in local space.
Note that the Debug.Log beautifies (rounds) this value to make it more human readable. If you want the exact values you could try and log e.g.
var relative = transform.InverseTransformDirection(rb.velocity);
Debug.Log(relative.ToString(F4));
which should print the values always with 4 digits after the decimal point.

Unity3D - Relative position on y-axis independent of rotation?

I have an object (A) with another object (B) next to it. I am trying to calculate the "height" of object B, so that i can position another object at that height relative to the position of object A. I know this sounds like gibberish (i'm a bit tired) so i have put diagrams to try and explain.
So in the left image the yellow line represents what i am trying to calculate. I have an position (orange) on the surface of a cylinder (grey) (calculated position using mesh data) which i am trying to use to calculate the radius of the object (black line). To do this i need a position at the center of the object (grey) at the same height (red dot) so i can calculate the direction from one to the other and use the length (.magnitude) as the radius.
My problem is i can't work out, how i can calculate the height (yellow line) without rotation having any effect.
I currently use projectOnPlane however if i rotate the object as seen in the second image, the radius decreases significantly when it should be consistent as the object is not changing size.
Vector3 RadDirection = (Vector3.ProjectOnPlane(orangePoint, grey.transform.up) - Vector3.ProjectOnPlane(grey.transform.position, grey.transform.up));
float radius = RadDirection.magnitude;
Any help would be much appreciated, thanks.
**UPDATE: The grey block in the diagram is a vector3 position rather than a game object. The radius calculation i am trying to do happens during runtime so i can't parent an object to the grey and review the inspector.
**UPDATE 2: Sorry, something i should have mentioned. The object i'm doing this on will not always be a perfect cylinder, it could be something such as a wine glass, where i need to calculate the radius of the glass not the stem. Another example could be a chemistry beaker which normally tapers to a point, so i would need to calculate the radius at the height of the orange point. Sorry i should have put that in the question.
Here's a diagram to illustrate what i mean in update 2. Again the orange dot is acting as a visual representation of a Vector3 position on the surface of the object's (in this case a beaker) mesh.
**Update 3: I appear to have solved the issue and so i have posted my answer below but at the time of writing i can't accept it (have to wait 2 days) and so cant close/answer the question. I would like to thank everyone that contributed and tried to help me solve this problem. I hope i can help you all someday :)
You can use transform.TransformDirection() or transform.InverseTransformDirection()
To get the height take the result of the subtraction and set the X, Z coordinate to zero:
Vector3 height = orangeBox.position - greenPoint.position;
height.x = 0;
height.z = 0;
Complete solution:
Vector3 direction = orangeBox.position - greenPoint.position;
direction = greyBox.transform.InverseTransformDirection(direction);
direction.x = 0;
direction.z = 0;
height = direction.magnitude;
redPoint.position = greenPoint.position + greyBox.transform.up * height;
I appear to have solved my problem using the following:
public static Vector3 ProjectPointOnLine(Vector3 linePoint, Vector3 lineVec, Vector3 point)
{
//get vector from point on line to point in space
Vector3 linePointToPoint = point - linePoint;
float t = Vector3.Dot(linePointToPoint, lineVec);
return linePoint + lineVec * t;
}
I found this here, it has lot's of other useful looking functions:
http://wiki.unity3d.com/index.php/3d_Math_functions
I basically pass in the origin of the beaker (green point) as the "linePoint", the upwards direction of the beaker "lineVec" with beaker.transform.up and finally i pass in the world vector3 point on the surface of the beaker mesh (orange point) it returns back a point in the middle of the beaker at the same height as my orange dot. I then just subtract the one from the other and take the magnitude as the radius. The radius value calculated is correct in that's it the value i was expecting and only changes after the sixth decimal place during rotation which gives plenty of accuracy as i only need three or four decimal places.
I'm happy to provide further details or help if anyone else needs help doing this.

how to orbit or Angularvelocity andengine?

I'm working on a project using Andengine GLES2-Anchorcenter
I have a dynamic body and I want to make it move around a circle like earth moving around the sun !
the problem with angular velocity that it rotates around it's center , I want to rotate it around the center of the circle ?
I tried many ways , I tried joints and more but It didn't work
can anyone help !
Move the body along the (circular) path using setVelocity so that every step the body moves further on the path. (You have to calculate the needed velocity to reach a certain point). It's actually quite easy: sqrt((destination.x - position.x)^2 + (destination.y - position.y)^2) / timestep. You should avoid using setTransform which will break the physical behaviour... Any luck?

transform.Translate not working properly in Unity

I am developing a game in which I want infinity road, So I created 40 instances of the road which I want to repeat again and again.
I am writing the following code to change the position of road that is not visible in the camera now and can be repeated
roadPaths[currentRoad].transform.Translate(Vector3(0,0,startPositionValue));
But I am getting the following log
In the above log you can see when the startPositionValue is 42, currentRoad position becomes 43. Same issue is with other values as well.
A help will be highly appreciated.
Translate is a relative move of the GameObject in three-dimensional space. It literally translates it from the current position by a relative value to a new position that is an offset from the original.
Explicitly setting the Position vector of the GameObject's Transform will set it to an absolute position within the world.
Finally I am able to solve the issue.
The following code work
roadPaths[currentRoad].transform.position=Vector3(0,0,startPositionValue);
I think this is very simple:
transform.Translate(X, Y, Z);
for exemple:
transform.Translate(5 * Time.deltaTime, 0, 0);
//GameObject moves smooth at the positive direction of the X position