Get an offset behind player forward without target - math

I need a constant offest behind the player forward based on player rotation and player position. So if the player rotate or move this offset is always at a costant position behind the player forward. For example if this variable has the value 10 i get the player position 10 meters behind his current position. Any idea? Target info no needed. Thanks in advance.

Related

Collision Detection pygame

im currently making a small game by following youtube tutorials. i followed this tutorial by Clear code for the movement: https://www.youtube.com/watch?v=4aZe84vvE20&t=230s&ab_channel=ClearCode
i wanted to take my game to the next level and add a few blocks around the map which the user has to avoid. i want to make it so that if the car hits a block it stops and only moves when the user rotates it to face away from the block, into an empty space. right now, in the player class, ive created a collision attribute and have used pygame.sprite.spritecollide(self, blocks, True) to check for collision. the blocks variable is a group containing all the sprites for the blocks.
if there is a collision, i change the vector to (0,0), which stops the car, like intended. however, i cant figure out a way to change the vector again once ive rotated the car so that it faces away from the block, to get it going again. any help is appreciated. thanks in advance.
Do not change the motion vector to (0, 0). As soon as a collision is detected, change the position of the car so that the car only touches the object but does not intersect the object.
Something like (pseudo code):
if collide_at_left_side:
car.rect.left = obstacle.rect.right
A more general solution would be to correct the position along the normal vector of the collision.

JavaFX 2d game engine: How to move camera as player moves?

I am creating a 2d topdown game (with canvas size 720x480) where I want the camera to move once the player reaches a certain part of the screen. For example, once the player reaches 2/3 of the way to the right, the camera should then start scrolling to the right.
For reference, here is an image of the game world: game
In my code, I have every object implementing a "genericObj" class, which has a position, velocity, and dimensions. So this is what I am thinking of doing once the player reaches 2/3 of the way to the right and is continuing to move to the right:
set the player's velocity to half the original
update every object's velocity with the negative of half the player's velocity (object.velocity -= player.velocity)
check if objects are within the view of the camera
display the objects that are within the view, disregard others
The reason for using half the player's velocity for both the new player velocity and the objects is that, in my code, the player movement sprite is only updated when the player is moving. Therefore, I need the player to be moving as opposed to setting the player velocity to 0 and the velocity of every object to negative player.velocity.
Is this a good way of "moving" the camera? What are some better methods of moving the camera? Also, would performance be an issue if I used this method of moving the camera (for example if I had 50+ objects)?

Unity: Ballistic Vector

This is a first person shooter game where I am trying to launch a projectile from a moving player. The launch direction depends on where i click on the screen. The "launcher" has a fixed fire strength, which means that the projectile if fired more horizontally will travel further before hitting the ground, likewise a projectile fired in a more upwards direction will go higher but will have travelled less horizontally when it hits the ground due to gravity. The firing vector is determined by where the finger touches the screen, and then multipled by a public parameter "firingstrength". Make sense so far?
What I am confused about is how to get the position of the finger on the screen, which i use to calculate the "vector" to apply to the projectile.
I have imagined doing this (I am new to Unity) by the following:
Empty object (Player): Contains
movement scripts
Camera
Invisble inverted sphere which surrounds the camera, and I use this sphere to pick up the mouse clicks (i.e when i click on the screen the game should detect where on the inside of the sphere I have clicked on, in order to calculate a vector between the camera position and the sphere wall that i clicked on)
Once i have the vector, i just multiply it by a "firingstrength variable and apply it to a projectile that originates from camera position.
Does this make sense or is there a better way to do this?
Kevin
You do not need a sphere just use Input.mousePosition and construct a ray from it using ScreenPointToRay(). Than you just use ray.Direction to get Vector3 from your camera position.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 shootVector = ray.Diraction;
This should do the trick.
For touch interface:
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Vector3 shootVector = ray.Diraction;

TCP Movement Lag

I'm working on a small multiplayer 2D XNA Game using TCP. My issue is that when more than 2 people connect everyone starts to lag out. Currently for every new x,y position the player has it sends it as a packet to the server to broadacast to every other player connected on the network. This ultimately sends a massive amount of packets which I think causes the issue I'm having.
Is it normal to send every x,y coordinate to every player or am I doing something wrong?
Instead of sending every packet, you should only send packets when the player changes direction, by either jumping, or moving left/right. The clients should handle gravity and collision as normal, while the server validates it.
When they move, send a packet containing:
Their movement vector (This should be a small Vector2 or Point that is either -1, 0, or 1 on each axis. -1 X means they are moving left, 0 means no movement, and 1 means right movement)
You should implement this client side already if you have not (Don't just add X to the velocity when you move, assign it to a movement variable to use later.
Ex:
if (KeyState.IsKeyDown(Keys.Left) { Movement.X = -1 }
...
Velocity += Movement * Speed
Their current position. Send the players current position (The serverside and validated one) to all other players, to make sure their position matches (It may be off due to small changes)
Their current velocity. The velocity of the player should have changed when the movement changed, since they are now going a different direction. Send the velocity along with the position.
Overall, only when a player moves (Ex: Stops holding the left key or presses the jump key) and NOT every frame, send the movement (Direction you are going based on keyboard input), position, and velocity.
Once you have this completed, you may want to add interpolation, which will smooth the movements between updates and make it look less jerky.

Collision reaction in a 2D side-scroller game similar to "Mario"

This has been greatly bothering me in the past few weeks. In this time I've been researching online, even reading books in the Computers section at Borders to try to find an answer, but I haven't had much luck.
I programmed a 2D level editor for side-scroller video games. Now I want to turn it into a game where I have a player who can run and jump to explore the level, similar to "Mario".
The thing that is really giving me trouble is the collision response (not detection: I already know how to tell if two blocks are colliding). Here are some scenarios that I am going to illustrate so that you can see my problems (the shaded blocks are the ground, the arrow is the velocity vector of the player, the dashed lines are the projected path of the player).
See this collision response scenarios image:
http://dl.dropbox.com/u/12556943/collision_detection.jpg
Assume that the velocity vectors in scenarios (1) and (2) are equal (same direction and magnitude). Yet, in scenario (1), the player is hitting the side of the block, and in scenario (2), the player is landing on top of the block. This allows me to conclude that determining the collision response is dependent not only on the velocity vector of the player, but also the player's relative position to the colliding block. This leads to my first question: knowing the velocity vector and the relative position of the player, how can I determine from which direction (either left side, right side, top, or bottom) the player is colliding with the block?
Another problem that I'm having is how to determine the collision response if the player collides with multiple blocks in the same frame. For instance, assume that in scenario (3), the player collides with both of those blocks at the same time. I'm assuming that I'm going to have to loop through each block that the player is colliding with and adjust the reaction accordingly from each block. To sum it up, this is my second question: how do I handle collision response if the player collides with multiple blocks?
Notice that I never revealed the language that I'm programming in; this is because I'd prefer for you to not know (nothing personal, though :] ). I'm more interested in pseudo-code than to see language-specific code.
Thanks!
I think the way XNA's example platform game handles collisions could work well for you. I posted this answer to a very similar question elsewhere on Stack Overflow but will relay it here as well.
After applying movement, check for and resolve collisions.
Determine the tiles the player overlaps based on the player's bounding box.
Iterate through all of those tiles doing the following: (it's usually not very many unless your player is huge compared to your world tiles)
If the tile being checked isn't passable:
Determine how far on the X and Y axes the player is overlapping the non-passable tile
Resolve collision by moving the player out of that tile only on the shallow axis (whichever axis is least penetrated)
For example, if Y is the shallow axis and the collision is below, shift the player up to no longer overlap that tile.
Something like this: if(abs(overlap.y) < abs(overlap.x)) { position.y += overlap.y; } else { position.x += overlap.x; }
Update the bounding box's position based on the player's new position
Move on to the next tile...
If the tile being checked is passable, do nothing
If it's possible that resolving a collision could move the player into another collision, you may want to run through the above algorithm a second time. Or redesign your level.
The XNA version of this logic is in player.cs in the HandleCollisions() function if you are interested in grabbing their code to see what they specifically do there.
So what makes this a little more tricky is the constant force of gravity adjusting your players position. If your player jumps on top of a block they shouldn't bounce off they should land on top of the block and stay there. However, if the player hits a block on the left or right they shouldn't just stay there gravity must pull them down. I think that's roughly your question at a high level.
I think you'll want to separate the two forces of gravity and player velocity from collision detection/response algorithm. Using the velocity of the player if they collide with a block regardless of direction simply move the player's position to the edge of the collision, and subtract equal and opposite vector from the player's velocity since not doing this would cause them to collide yet again with the object. You will want to calculate the intersection point and place the player's position there on the block.
On a side note you could vary that really big force by what type of block the player collided with allowing for interesting responses like the player can break through the block if they are running fast enough (ie the player's velocity > than the force of the block)
Then continue to apply the constant force gravity to the player's position and continue doing your normal calculation to determine if the player has reached a floor.
I think by separating these two concepts you have a really simple straight forward collision response algorithm, and you have a fairly simple gravity-floor algorithm. That way you can vary gravity without having to redo your collision response algorithm. Say for example a water level, space level, etc and collision detection response is all the same.
I thought about this for a long time recently.
I am using the separating axis theorem, and so if I detected a collision I proceeded to project the object onto the normalized velocity vector and move the object by that distance in the direction of the negative velocity vector. Assuming the object came from a safe place this solution will position the object in a safe place post collision.
May not be the answer you're looking to get, but hopefully it'll point you in the right direction?

Resources