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

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)?

Related

Get an offset behind player forward without target

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.

Aframe Physics system: how to stop a dynamic-body at a point

My use-case is to let a ball bounce and come towards the camera (been able to do that with a simple dynamic-body sphere on a static-body grid). However, rather than it rolling down to a position where it loses its velocity (or momentum), is there a way to stop it at a desired point? I tried placing a (invisible) hurdle object but it rolls back. I would like it to remain stationary once it reaches the desired point. Thanks
You can stop the dynamic-body by zeroing a few body attributes
let body = el.body // el = aframe entity
body.velocity.set(0,0,0);
body.angularVelocity.set(0,0,0);
body.vlambda.set(0,0,0);
body.wlambda.set(0,0,0);
Working example here.
However IF your ball is on a slope, the physics engine will, properly, slowly accelerate it.
If you want it to stop no matter what (defying the laws of CANNON.js physics), then either remove the dynamic-body component, or swap it with a static-body.

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;

2d game rotation interpolation over network

I am working on a simple 2d multiplayer game with players that have a turret that faces a certain direction (0-360 degrees)
The turret is slow at changing its direction and thus has two values:
heading, which is the direction it is currently facing
desiredHeading, which is the direction it wants to face (player input)
The function moveTurret() is called once per frame and increases/decreases the heading by a certain value, so it matches the desiredHeading after some time. (--> turret moves slowly into desired direction)
Since my client receives updates every X milliseconds via the network, I have a hard time to render those rotations smoothly!
My current approach is as follows:
client periodically receives the heading for each other player
client sets the received values as desiredHeading for the respective players
moveTurret() is called for those players every frame
I thought the client would then approximate the rotation and would smoothly render the turret into its right direction, however this doesn't work at all and the animation is wobbling back and forth.
Does anyone know a simple approach for this problem? I have found some quaternion interpolation and matrix based algorithms, but none seemed to fit for my approach, since I only have a heading-value and my game does not use matrix calculations.

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