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.
Related
The following problem im working on is for one of my favorite past-times: game development.
Problem:
We're in 3D space. I'm trying to determine if a line between two vectors in said space is passing through a circle; the latter of which consists of: center vector, radius, yaw & pitch.
In order to determine that, my aim is to convert the circle to a plane which can either be infinite or just have the diameter of the circle for all it's sides.
Should the line between the two vectors in fact pass through that plane, i am left with the simple task of determining wether that intersection point is within the radius of the circle, in which case i can return either true or false.
What's already working:
I have my circles set up and the general framework is there. The circles are appearing/rendered in the 3D space exactly as specified, great!
What was already tried:
Copied some github gist codes and tried to make them work for my purposes. I kinda worked, sometimes at least. Unfortunately due to the nature of how the code was written, i had no idea what it was doing and just scrapped all of that.
Researched the topic a lot, too. But due to me not really understanding the language people speak when talking about line/plane intersections, i could have read the answer without recognizing it as such.
Question:
I'm stuck at line intersections. No idea where to go and how it works logically! So, where do i go from here and how can one comprehend all of this?
Note:
I did tag this issue as "java", but i'm not looking for spoon-fed code. It's a logical issue i'm trying to get past. If explained well enough, i will make the code work with trial and error!
Say if your circle is a circle in the XY plane with its centre on (0,0,0) and radius 1. How would you solve that?
You would check the values of X and Y when Z is equal to zero. And X squared plus Y squared would be less than 1 (radius squared) if the line passes through the circle.
In other words, you could transform the 3D coordinates to a simpler reference frame. So I think you need to learn transformation of 3D coordinates, which is really not too hard to do. You need to rotate the 3D space around until the centre vector only has a Z component, and yaw and pitch are zero. And then offset the coordinates so the circle centre is in (0, 0, 0). Then apply the same transformation to the line. You could lastly scale by radius, but to be honest that is not so important since the circle math is easy.
I'm currently building a 2D game with Slick2D, and right now I'm implementing my first NPC.
I already have the sprites, I was able to draw him, did the collisions, and so on, but now I want to add the feature which makes the NPC look into the direction I am currently at, when I am entering a certain range around the NPC (Which would be 2 tiles left, diagonal, right, up and down, but because I want it to be exact, I'm using the x coordinates, so for every tile I would be checking 32x32 possibilities)
To get this a little bit more clear I drew a picture:
The numbers are the x and y coordinates, while my NPC is placed at 704,704.
The problem I have is that I don't know how to check all the coordinates effectively, without having to write 5000 lines of if's. The 2 left diagonal lines, which could both be interpreted as "NPC looks up" and "NPC looks left" would make him look left, same with the 2 right ones, which would make him look right.
Is there some clever method I can use, or should I just go back and divide the x and y by 32 so I only check if my player is on a certain tile? That would be less precise, though.
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
I am making a side scroller shooting game. I currently have my character shooting horizontally to the right. I would like to get him shoot anywhere on the screen.
I understand that I should use atan2 to figure what angle my bullet will be shot at but I am confuse how to implement it into my game.
My question is how do I call my coordinates of the touch on the screen into atan2? Do I place this in my touch command codes or the class for my projectile. Lastly do I need to do another atan2 for speed?
First you need the vector of your player
P = (a, b)
And the position of your shooting point
T = (x, y)
This then gives us a vector from P to T, (x - a, y - b). By taking
angle = atan2 (y-b, x-a)
From there, it depends how you are implementing your shooting.
To reiterate, all atan2 does is finds the angle, so
do I need to do another atan2 for speed
depends on whether you want to have the projectile moving at a different speed depending on angle. In fact, you only really need the angle if you are rotating your projectile, otherwise you can just move it along the vector (if it is a circle, doesn't matter what way it is facing!)
You don't need any trigonometry and you should not use it, as it is slow, inaccurate and full of sign (+/-) cases to get wrong.
You can probably do everything you need with linear algebra. Use vectors, not angles.
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.