Predicting circle-circle bounce vector when aiming one at another - java

I am making a game with libGDX (with box2d) in which you drag and aim one circle body at another then release to fire. I would like to be able to predict the path of the second circle based on the aim of the first. very like 8 ball pool when aiming your shot.
I have been playing around with various trig solutions but i just cant get it to work at all! I have also been reading about ray casting in which I would cast a ray from the center of the first circle body in the direction i am aiming. this would tell me where the ray intersects the second circle body (if it does so) but it would not be correct as the circle fixture would not actually collide where they ray does because it obviously has a radius.
Is the trig solution the correct path or is there a simpler way to do this?

Related

libGDX polygon rebound (bounce) angle

I am hoping someone could point me in the direction of a libGDX example that shows how the rebound angle is calculated when a polygon hits another (which is fixed) - The questions/answers I've found on StackOverflow have all been to general (abstract), I'm hoping for a more libGDX specific answer :)
Currently, I am using Intersector.MinimumTranslationVector to check whether the moving polygon has hit another fixed-stationary polygon and then using the resultant values to shift the objects back out of each other.
When the collision occurs virtually or horizontally, the bounce calculation is easy and currently works well. What I am having troubles with are the diagonal collisions - working out which face was actually hit and the direction in which to bounce.
The moving polygon uses a Vector2 object for it's velocity, so the question is how is the new velocity calculated? I'm assuming it can be done using the Intersector class, but how?

Java Game Hitbox Detection & Rounded Corners

I am working on a simple 2D game with Java, swing and no framework. I have a rectangular player that the user can move around. On the map are few obstacles which the player should not be able to go through. I did this by making a new Rectangle Object for the player and each obstacle with their bounds. But I’m not really sure if this is the right way to do it. It works but the movements of the player are not really user friendly. If the player wants to pass two obstacles they must be on the perfect coordinates to pass.
Is it even a good idea to check for intersections between the player and the obstacle with a Rectangle object or should I do it another way?
Now for my 2nd question:
I’d like to replace the rectangular hitboxes with the same hitbox but with rounded corners so the player could pass more easily.
This is what the game looks like with hitboxes enabled.
The code that checks if the player and the obstacles have yet intersected:
for (Player p : this.getPlayerArray()) {
Rectangle recPlayer = p.playerBounds();
for (Obstacle kiste : obstacleArray) {
Rectangle recKiste = kiste.obstBounds();
if (recPlayer.intersects(recKiste)) {
p.setX(100); //Not actual code here
}
}
}
The function that returns the hitbox of the player / obstacle:
public Rectangle obstBounds() {
return new Rectangle(this.getX(),
this.getY(), image.getImage().getWidth(null),
image.getImage().getHeight(null));
}
Many years ago I wrote a Khepera simulator as part of my undergrads' final year project. I started by doing collision detection as you are doing, that is intersecting areas... My supervisor made me notice that since the shape of my robot was a circle I just could check if the center of the robot was inside another shape, if that was the case a collision occured.
Your case is even simpler since you move on tiles... so either you do (as suggested in the comments) maintain a set of "move/no move" tiles and check that, or you just check that the position of your player is within, or not, a 'forbidden' rectangle, i.e. an obstacle. If it is, then you have to reset the position of the character to be 'outside' of the obstacle (minus the character's bounding box, obviously)
I strongly suggest to do it the tiles way: allow only up/down/left/right movements and check against a 'forbidden' set of movements given a tile-position. If you really want 'freedom' of movements than go with circles (bounding boxes/circles) because they are easy to reason with, easy to do a position reset (in case of collisions) and perfect for your case (every tile can contain a circle, whether an obstacle or the player.)
There are many ways to go about collision checking, but I think a simple approach would do just fine for your use-case.
First from the looks of your screenshot, a tile is either an obstacle or passable, but never half of each. In that case it would be simplest to just check on which tile the center (or feet, choose what looks best) of the character are.
If its an obstacle, they cant go there, simple as that. Note that this will allow the player to move partially into an obstacle, but many games do it this way and players are certainly used to this behavior, especially in 2D games with graphics designed to look like an isometric projection (which I would yours class as). A variation of this would be to simply make the player collision rectangle smaller (say half a tile, again centered at the player sprites chest or feet).

Adding objects together

In a game that I am creating at the moment I want to make it so that the player holds a gun.
Right now I have the gun and the player as one image and it works but it would be alot better to have the gun and the player as seperate images because right now it looks like the player shots bullet from it's forehead and if I would have the gun as an seperate object it would be easier to make the bullets shot out of the gun. I will also implement a weapon switching system later on so then it will also be easier to have the guns as seperate objects.
The problem is that because I have used an AffineTransform and Vector2d to rotate the player to always face the mouse cursor I can't manage to make it so that it always looks like the player holds the gun. It's more that sometimes the gun is a little inside the player, sometimes it looks fine and sometimes the gun is floating in the air, either infront of the player or at the side of the player.
Is there any easy way that I could make the gun "stick" to a part of the player object?
You probably "just" need to use the right anchor, for example in rotate. To make this easier, imagine that the Gun bitmap (or Shape?) has the same size as the player.
I tried to illustrate that in this drawing:
The left image is the player, and the right image is the gun. Notice that the gun image has the same dimension as the player image, but is of course almost empty. The red point would be the anchor of the rotation - for example in the center of the image, even though that doesn't matter. If you superimpose the gun on the player and rotate both using the same anchor the gun will always be in the right place.
If you know your linear algebra you can of course also solve this analytically. But I would suggest first getting it to work the way you want, and then exploring more elegant solutions from there. A good quick read I found is this four part blog post. Part 3 and 4 touches on your question. If you want a more thorough introduction any linear algebra textbook will do, or you might like this free online course.
In our game we make the sword and shield stick to the appropiate position of the sprite of the player/enemy by having helpermethods like the ones below:
public int getPlayerLeftHand()
public int getPlayerRightHand()
And everytime the player is drawn (in our game he always has a shield and sword) his sword/shield is drawn at his right/left hand respectively by calling the above methods. The implementors (player/enemy) decide where their hands are so you don't have to worry at all about whether they are rotated or not, you know that every time you call the method the implementors take care of giving you the correct coordinates, in our case by checking what direction they are facing then displacing the coordinates appropriately.
Alternatively if you want a more OOP approach you could pass the player/enemy to the weapon and let them decide how and where they want to be rendered.
You solve this by accumulating transformation matrices.
First, you have a transformation matrix that positions the player. Next, you define a matrix that positions the gun in the hand of the untransformed player. Multiply the two matrices together, and you have one that positions the gun in the hand of the transformed player.
Finally, while you're at it, write a transform matrix that positions a bullet relative to the barrel of the gun. Multiply all three matrices together, and your bullet is where it belongs.
Your 3d graphics library will have routines to do all this for you.
I have to be blunt though: if you don't already know all this, you may have bitten off more than you can chew.

collision & touch + shoot methods for Android

I really need help. I am making a game app for my final year project. It is a simple game where you have to shoot a ball into a target by rebounding of walls or angled blocks. However i need help in 2 areas:
the shooting mechanism is similar to that of stupid zombies. There is a crosshairs where you touch on the screen to indicate which direction you want the ball to be shot at. On release the ball should move into that direction and hopefully gets into the target and if not gravity and friction causes it to come to a stop.
The problem is how do I code something like this?
I need the ball to rebound of the walls and I will have some blocks angled so that the ball has to hit a certain part to get to the target. The ball will eventually come to a stop if the target is not reached.
How can I make a method to create the collisions of the wall and blocks?
I have spent the last weeks trying to find tutorials to help me make the game but have not found much specific to the type of game I am making. It would be great if sample code or template could be provided as this is my first android app and it is for my final year project and i do not have much time left.
Thank you in advance
akkki
Your question is too generic for stack overflow no one is going to do your project for you. Assuming you have basic programming experience if not get books and learn that first.
Assuming you already chose Android because of your tag, and assuming 2d game as it is easier.
Pre requests:
Install java+eclipse+android sdk if you havent already.
Create a new project and use the lunar landar example, make sure it runs on your phone or emulator.
Starting sample:
The lunar landar has a game loop a seperate thread which constantly redraws the whole screen, it does this by constantly calling the doDraw function. You are then supposed to use the canvas to draw lines, circles, boxes, colours and bitmaps to resemble your game. (canvas.draw....) Lunar landar does not use openGL so its slower but much easier to use.
Stripping the sample:
You probably don't want keyevents or the lunar spaceship!
Delete everything in the onDraw function
Delete the onKeyUp, onKeyDown
Delete any errors what happen
Create a new
#Override
public boolean onTouchEvent(MotionEvent event){
return false;
}
Run it you should get a blank screen, this is your canvas to start making your game... You mentioned balls, break it down to what a ball is: A position and direction, create variables for the balls x,y direction_x and direction_y the touch event will want to change the balls direction, the draw event will want to move the ball (adding the direction x,y to the ball x,y) and draw the ball (canvas.drawCircle(x,y,radius,new Paint())) want more balls search and read about arrays. Most importantly start simple and experiment.
2 collisions
Collisions can be done in the dodraw function and broken down to: moving an object, checking if that object has passed where it is supposed to go and if so move it back before anyone notices.... There are many differently techniques of collision detection:
If your walls are all horizontal and vertical (easiest) then box collisions checks the balls new x,y+-radius against a walls x,y,width and height its one big if statement and google has billions of examples.
If your walls are angled then your need line collision detection, you basically have a line (vector) of where your ball is heading a vector of your wall create a function to check where two lines collide and check if that point is both on the wall and within the radius of your ball (google line intersection functions)
or you can use colour picking, you draw the scene knowing all your walls are red for example, then check if the dot where the new ball x,y is, is red and know you hit
Good luck, hope this helped a little, keep it simple and trial and error hopefully this gets you started and your next questions can be more specific.

Vertices selection and state of model after rotation

I'm currently writing an application that actually acts as a "cut" tool for 3D meshes. Well, I had some problems with it now which I am clueless on how to solve, since it is my first application.
I have loaded a model from an object file onto the canvas, then on the same canvas, I use the mouse drag event to draw lines to define the cutting point.
Let us say I want to cut a ball into half and I draw the line in the middle. How do I detect the vertices of the ball under the line.
Secondly, if I rotate/translate the ball, would all the the vertices information change?
Think of what you'd do in the real world: You can't cut a ball with a line, you must use a knife (a line has no volume). To cut the ball, you must move the knife through the ball.
So what you're looking after is a plane, not a line. To get such a plane, you must use some 3D math. What you have is the canvas orientation and the "side view" of the plane (which looks like a line).
So the plane you're looking for is perpendicular to the canvas. A simple way to get such a plane is to take the canvas orientation and create a plane which has the same orientation and then rotate the plane around the line by 90°.
After that, you can visit all edges of your model and determine on which side of the plane they are. For this, determine on which side of the plane the end points of the edge are. Use the cross product. If they are on the same side (both results of the cross products will have the same sign), you can ignore the edge. Otherwise, you need to determine the intersection point of the edge and plane. Create new edges and connect them accordingly.
See this page for some background on the math. But you should find some helper methods for all this in your opengl library.
if I rotate / translate the ball, would all the the vertices information change
Of course.
It's not going to be that easy.
I assume the line you are drawing induces a plane which then cuts the sphere.
To do so, you have to calculate the intersecting area of the sphere and the plane.
This is not a trivial task and I suggest using an existing framework for this or if you really want to do this yourself, read about basic intersection problems to get a feeling for this kind of problem. This paper offers a good introduction to various intersection tests.
In general boundary represended volumes, as in your case, are difficult to handle when it comes to more advanced manipulations. Cutting a sphere in half is easy compared to burring a small hole into it. Sometimes it's better to use a volume representation, like tetrahedral meshes or CSG.
Regarding your second question, you shouldn't rotate or translate the sphere, rotate and translate the camera.

Categories

Resources