In my game one crocodile is in water i have to guide that crocodile in water ,
it will follow my finger touch and will finish the path.
it should rotate when there is turn.
what approach should i use in libgdx...
Thanks
There is only one approach.
You capture the input (implement InputListener), unproject the pointer to your game world (Camera.unproject(...)), calculate the difference between the target vector and the position of the object that should follow it and move it in this direction, or use A* in case there are obstacles in between, calculate an appropriate path and then move along that path.
Related
I'm attempting to write a bouncing ball game using flame in flutter. To detect collisions the onCollision and onCollisionStart methods are provided. What I had hoped is that onCollisionStart would give a precise location when two objects first hit each other. However, instead it gives a list of positions indicating where the two objects overlap after the first game-tick when this happens (i.e. onCollisionStart is called at the same time as onCollision, but is not called a second time if the same two objects are still colliding on the next tick).
This is illustrated in the attached picture. The collision points are marked with red dots. If the ball were moving downwards, then the ball would have hit the top of the rectangle and so should bounce upwards. However, if the ball were moving horizontally, then its first point of contact would have been the top left corner of the box, and the ball would bounce upwards and to the left.
If I want to work out correct angle that the ball should fly off, then I would need to do some clever calculations to work out the point that the ball first started hitting the other object (those calculations would depend on the precise shape of the other object). Is there some way to work out the point at which the two objects first started colliding? Thanks
What you usually need for this is the normal of the collision, but unfortunately we don't have that for the collision detection system yet.
We do have it in the raytracing system though, so what you could do is send out a ray and see how it will bounce and then just bounce the ball in the same way.
If you don't want to use raytracing I suggest that you calculate the direction of the ball, which you might already have, but if you don't you can just store the last position and subtract it from the current position.
After that you need to find the normals of the edges where the intersection points are.
Let's say the ball direction vector is v, and the two normal vectors are n1 and n2.
Calculate the dot product (this is build in to the vector_math library) of the ball direction vector and each of the normal vectors:
dot1 = v.dot(n1)
dot2 = v.dot(n2)
Compare the results of the dot products:
If dot1 > 0, n1 is facing the ball.
If dot2 > 0, n2 is facing the ball.
After that you can use v.reflect(nx) to get the direction where your ball should be going (where nx is the normal facing the ball).
Hopefully we'll have this built-in to Flame soon!
Swift beginner struggling with moving a scene node in ARkit in response to the device motion.
What I want to achieve is: First detect the floor plane, then place a sphere on the floor. From that point onwards depending on the movement of the device, I want to move the sphere along its x and z axis to move it around the floor of the room. (The sphere once created needs to be in the center of the device screen and locked to that view)
So far I can detect the floor and place a node no problem. I can use device motion to obtain the device attitude (pitch, roll and yaw) but how to translate these values into meaningful x, y, z positions that I can update my node with?
Are there any formulas or methods that are used to calculate such information or is this the wrong approach? I would appreciate a link to some info or an explanation of how to go about this. Also I am unsure how to ensure the node would be always at the center of the device screen.
so, as far as I understood you want to have a following workflow:
Step 1. You create a sphere on a plane (which is already done)
Step 2. Move the sphere with respect to the camera's horizontal plane (i.e. along its x and z axis to move it around the floor of the room depending on the movement of the device)
Assuming that the Step 1 is done, what you can do:
Get the position of the camera and the sphere
This should be first called within the function that is invoked after sphere creation (be it a tapGestureRecognizer(), touchesBegan(), etc.).
You can do it by calling position property of SCNNode for sphere and for camera position and/or orientation by calling sceneView.session.currentFrame's .camera.transform which contains all necessary parameters about current position of the camera
Move the sphere as camera moves
Having the sphere position on the Scene and the transformation matrix of the camera, you can find the distance relation between them. Here you can find a good explanation of how exactly you can do it
After you get those things you should implement a proper logic within renderer(_:didUpdate:for:) to obtain continuous lock of the ball with respect to the camera position
If you are interested about the math behind it, you can kick off by reading more about transformation matrices which is a big part of Image Processing and many other areas
Hope that this will help!
I am doing a small game project named "puzzle game". Every piece of the puzzle should can be both moved and rotated. Whether I move the piece or roate the piece, I just do the same mouseEvent that move my mouse.
The differrence is that:
1) When the mousePoint is at the centre of piece , I will do pressMove;
2) When the mousePoint is at the edge of piece, if I move mouse, the piece will rotate according to the caculated angle.
But now, I don't exactly know how I do this.THANK YOU FOR ANY HELP.
For the roate, there is a example in this website.
(http://www.gamefromscratch.com/post/2012/11/18/GameDev-math-recipes...)
I want to do the same thing for rotating. And can move at the same time.
Consider I am pulling bus by finger touch and drag movement. Road is not straight. so if i am moving my finger along the road . the Bus should follow the finger along with some rotations that will be needed when there is turn.
First finding the distance between actor and touchpoint and if it is less then
I am simply setting the position of bus (Actor) to touch points location.
now it feel like i am dragging bus.
Now I have to handle the rotations.
can i apply force to actor towards touchpoint??
can i handle rotation of actor towards touchpoint ...
simple logic in my mind is dragging finger means drawing a line..now i have to match center line of the actor to dragged line..
please give me hint regarding handling the rotation of bus.
Thanks,
It sounds to me like you just want the bus to follow a path like a normal bus would follow a road.
You can rotate a body by applying torque to it. That means you will use applyForce(...) and not use the center of mass as the point to apply the force to.
But you don't want to apply a force and make it move towards a certain target point like that, because that would just look weird and you would need some special handling for realistic car physics (for topdown you can see that here http://www.iforce2d.net/b2dtut/top-down-car).
Better just calculate a path yourself and calculate the angle between different points on that path. Then use body.setTransform(...) and set the position and the rotation of the bus manually. That's how you would do it as well, if you wouldn't have any physics engine.
I need to animate an object along a path. I am doing so by creating an animation like:.
This works great but since my terrain is not flat it will be nice if I don't have to deal with the y component. In other words I want to move an object along the x-axis and z-axis and if there is a small slope increase then increase the object y position. same thing if there is a downward slope. Or maybe I have to create a script where it checks to see if the object is colliding with the terrain. if not then decrease its y position. I don't know if this will work meanwhile animating an object though.
I think the easiest way I can think of to solve this is for you to make the object a rigid body and use collision (probably a mesh or capsule collider if its a player character) to get it to sit on the floor.