I'm developing an Android game somewhat similar to Star Control.
In this game, planet gravity plays a huge role, and so the player's ship is affected by various planets. In this game, there are multiple planets which move (orbit).
My main issue at the moment is handing collision response. When the player hits a planet, i want the player to have a small "bounce".
I used this: https://www.youtube.com/watch?v=ymgbDdO8hKI as a source to program this, and it works great for planets which don't move.
However, when the planet does move, the algorithm works great when the planet is moving away from the player, but if the player is in the planet's path (i.e., the planet is moving towards the player), the planet sort'v "eats" the player.
I know I need to compensate somehow for the planet's velocity. I've tried different variations, such as add the planet's vector to the overall resulting vector of the bounce, but nothing seems to give a good result... the planet always seems to eat up the player when the planet moves towards the player.
If you guys would like me to post code samples, let me know, although I'm looking for more of a "concept" solution, like the video linked above provides.
Thanks in advance!
For applying directly the concepts in that video, you can switch to a reference frame where the planet does not move at the time of the collision. For that, and considering just classical physics, you just need to change the velocity of the player subtracting the speed of the planet (2D vector subtraction). Then apply the formula in the video, and finally switch back to your original reference frame by adding the speed of the planet (2D addition).
In a notation similar to the one in the video, being v_before the velocity of the player before bouncing, v_planet the velocity of the planet, n the normal of the planet surface where the collision occurs and C the restitution coefficient,
v_before_2 = v_before - v_planet // change of reference frame
v_after_2 = v_before_2 - (1 + C) * (v_before_2 . n) * n // simple wall bouncing
v_after = v_after_2 + v_planet // change to original reference frame
Related
I'm working on a 2D sidescroller game.
Description:
Im using libGDX and the AI extension. The game will be released on android (the AI should not be heavy resurce consuming). My terrain is not grid based, it's a procedural generated polygonal height map (without caves).
There are 3 types of enemies (NPC) - near-, distance- (bullets) and combined-combat.
Entities have 3 ways to move - left, right and jump. Allso some can hover over the ground like on the picture. I tougth to make nodes with an y offset from the terrain should work but if a player jumps, a near-combat enemy couldn't have a path (I didn't test it but i assume it).
I seen many examples on grid based games but non for my scenario.
Excuse my less knowlege, I just jumed in AI devolepment a few days ago.
Questions:
Is the AI (from libGDX) compatible with an (almost) infinite world?
How should I setup nodes?
Can the AI be used to calculate bullet directions to hit a player?
First, I do not know libGDX but if you want to have an infinite world you have to program it. That means you generate the terrain randomly and destroy it in the memory if it is not needed any more. If you want infinitely go to the left and the right you have to store efficiently your world on your hard drive if the memory is full. For your polygons, you just have to store the nodes and some numbers as references to surrounding elements if you want to go back with your avatar.
Second, because your game is two-dimensional the possible paths are that simple that you need no path finding algorithms. So you do not need nodes or things like that. What you need is something like hit-boxes so that your enemies know when they hit each other. The AI needs only to know if your avatar is left or right and if it is reachable directly. That are simple geometry calculations. If it is possible to jump over other enemies it is as if they can go through them for the path.
Finally, the bullet direction is calculated simply with linear algebra to get the direct line from your avatar to your enemy. You need only to calculate if it intersects with other enemies or the terrain to know if your avatar can be hit.
The only AI aspect here is to determine the behaviour of the enemies. That can be done with a state machine, where the enemies have states like aiming, waiting, following or shooting. Depending on how far away your avatar is or how reachable it is the states change.
I am making a game, and I got the player to move, but when the player turns at a diagonal (Up, then up-right, for example), it's not as smooth as I want it to be.
Here's a picture showing the issue:
The player lines indicate the player's path.
How can I achieve this effect?
I'm thinking of maybe generating a bezier curve for the player to follow, but I don't know how I would get the player to follow it.
You are correct in your assumption of using a bezier curve. I would suggest (if you don't have it already) writing an Update method which you use to control the player movement on a frame-by-frame basis. In other words; a method which runs in a separate thread and triggers position updates for the player you want to move along the curve. It would act as a queue, only executing movements etc when a given amount of time has elapsed.
As for how to get the player to move along the curve, I would calculate multiple points along the curve, let's say 25 per curve. Have each point, point to the next point. These will act like waypoints - the player is given the instruction to move to the next waypoint over a short amount of time. So if you wanted to move your player along the whole curve within 5 seconds, the player would need to 'jump' from one waypoint to another every 0.2 seconds.
If you wanted the movement to be less 'jerking', you would need to write linear interpolation which would in effect further calculate smaller divisions of waypoints between each of the curve waypoints.
You can find many helpful methods for the items described above, here
In the image below, the blue blobs are waypoints.
Alternative (Simpler) Curve Approach
An alternative approach to using a bezier curve is to do a very simple smooth curve: In the case of turning right; start off with two values- Xfactor = 0, and Yfactor = 10. For this example, I assume that the speed of movement around the curve is 10 units (it could be pixels for example). For each frame, you add Xfactor and Yfactor to the players current position. You also subtract a constant (let's say 0.5 units) from the Yfactor and add it to the Xfactor. You keep going until the Xfactor equals 10 and Yfactor equals 0. This should cause the player to 'move' through 90 degrees. In order to get the player to move through 45 degrees, you would keep going until the Xfactor equals 5, as this would be half of 10. To keep the player moving in that direction, you would need to keep adding the Xfactor and Yfactor to the player position on each frame/update. Again, to make the player turn, you change the Xfactor and Yfactor over time.
Right now, my game simply draws the vision cone of characters through walls. I want the arc to be interrupted when it reaches a wall, but continue in points where it does not reach a wall.
Here is a quick example of what I want to happen.
I am fairly new to programming, I have been programming for about 6 months, but I am very competent in math so please be simple in programming terminology, thank you.
Here is my GitHub if you want a better feel for my game.
You can figure out how far your character can see in any given direction by casting a ray from the character in that direction and stopping when it hits an obstacle. The details of how to do that for your particular case are going to vary based on how objects are represented in your game.
In the article you link to, it looks like walls are made from lines. If that's the case in your game, you could find the intersection of a ray and each line segment of a wall. If they intersect, then the point at which they intersect is the farthest you can see in that direction.
If, instead, you represent objects as sprites at a given location, you could start by casting the ray and seeing if it intersects the bounding box of a sprite. (The bounding box is just a rectangle around the sprite that completely contains it, as tightly as possible.) Once you know your ray intersects the bounding box, you can look more closely at that object to see if the ray actually hits the sprite.
I have made threads in the past about similar questions but because of my lack of detail the answers have not really been related to what I needed so I am going to try explain my question in as much detail as I can and hopefully it will be easier for you to understand what I require.
I watched Bucky's slick game tutorials on youtube and made a 2D Java game, the game is basically a 2D player viewed from above (birds eye view) can move around a 2D map with user key input (up, down, left, right). The map the player moves around is very small so that meant boundaries had to be set so that the player could not walk off of the map, to give you a better idea of how this was done, here is the tutorial for setting up the voundries:
http://www.youtube.com/watch?v=FgGRHId8Fn8
The video will also show you exactly what the game is like.
The problem is, these boundaries only require one axis meaning that if the player is walking down you say something like "if player gets to the coordinate (number) on the X axis change the player movement to the opposite direction so that he can not go any further."
Now this creates a problem for me because this only requires one axis so it easy to set up and understand but if you look on the video, on the map there is a house and I want my player not to be able to walk over that also but this deals with 2 dimensions, I have looked at things like rectangle collisions and have seen things relating to them in the other posts but I get confused because I am new to Java and havent really done much with it at the moment apart from watching Bucky's tutorials.
My code at the moment for my game class has got the following methods: init, render and update. So to sum it up I really just want to set up a way of not letting my player walk through the house, I will mention also (I should have mentioned it in my other threads) as I am very new to Java, could you please take a step by step method of showing me how to set up the collisions, I mean even the basics of things like making the rectangle if required.
If my code is required please tell me and I will post it as soon as possible.
Thank you in advance.
You can set up the board as a 2x2 grid of a class that has has property such as 'isBlocked'. By default the edges of the board would have this property set to true to prevent the character from walking off the edge. When you add other obstacles such as a house or a wall the grid position(s) the object occupies would also have the property set to true. Then when moving a character you just check if the grid position the character moves to has the property set to false to see if it's an allowable move. This also makes it quite trivial to save the level data so you can just load them from disk later on.
Two possible options:
Extend Shape or Rectangle or the relevant Slick objects (they should exist IMO) and just check for intersect()
Look for (x1,y1) and (x2,y2) values such that it starts outside and ends up inside.
Assuming you have intersect() methods:
//Grab the previous position
prevPosX = movingobject.X;
prevPosY = movingobject.Y;
//Update the position
movingobject.update();
//Test for collision
if(movingobject.intersects(targetobj)) {
//If it collided, move it back
movingobject.X = prevPosX;
movingobject.Y = prevPosY;
//And reverse the direction
//(might want to do other stuff - e.g. just stop the object)
movingobject.speed *= -1; //Reverse the speed
}
in this case your update class should also add one more condition to look for the house. let say the cooridnates of house(assuming rectanglular house here for other shape just change x and y values) are (x1,y1)(x1,y2)(x2,y2)(x3,y1) you have to add a condition to make sure
x value is not between x1 and x2 and at the same time y value cannot between y1 and y2.
So I have a 2d game which normally just has gravity and "flat" levels however I have added in "planets" which have their own gravity.
I have a function called addForce(float xForce, float yForce) that I use to move my character. So say if I called player.addForce(1, -1); the player would move up and to the right(albeit slightly). This worked fine on the levels with regular downward gravity, however with planets it is not so.
There is another float called earthAngle which is:
atan2(player.getY()-earth.getY(), player.getX()-earth.getX());
What I did for the jumping code on the planets is:
player.addForce(cos(earthAngle)*1500, sin(earthAngle)*1500);
which works well. However I am stuck on how to make the character walk around the planet.
Currently for the movement code I have:
player.addForce(25*x_*cos(earthAngle), 25+x_*sin(earthAngle));
which only works on some parts and works in reverse on the bottom as well as being stronger/weaker on some parts, x_ can be either -1(left) or 1(right). I'm guessing their is a really elegant solution I am just overlooking. Thanks.
Since you already have the vector from the center of the planet to the player eg (x,y), you can use a vector perpendicular to that (-y,x) as the direction for the walking force.