Box2D, libGDX slope collision - libgdx

I have created a Box2D based map for my platformer using ChainShape which has different angle slopes (no more than 45°).
My problem is the slope collision with my player. The player' body has 2 fixtures, a rectangle and a circle below it. Whenever I move the player (the method doesn't matter the results are the same) and I stop on the middle of a slope I slowly glide down. I managed to temporarily fix that but the main reason for this post is the fact that whenever I leave a slope l I shoot out because I still have some impulse left from the previous movement. Same thing happens when I enter a downwards slope, or when I stop on a slope and I start moving again.
Entering / leaving the slope:
(The red lines are the desired movement, the black is the movement I have right now)
Start moving on a slope:
(The red lines are the desired movement, the black is the movement I have right now)
Could you please help me out with this problem? I don't mind if your answer is detailed either.

I've found the easiest way to stop the character sliding on a slope is to set the bodies linear dampening to Infinity when they player releases the move button and the body is grounded, which I'm using ray tracing to test for.
As for your issues with the character not following the slopes, this is simply due to momentum. When you reach the top of the slope, your body still has a some upward momentum which it has to clear. Same as when you're approaching a downward slope - your body still has some horizontal momentum to clear. I've had some success with ray tracing to find the normal of the slope below the player's position, and manually setting the body's velocity to match the tangent, however this not be applicable in your application.
As for your second issue, with starting movement on a slope, I expect you are applying a direct horizontal impulse, which is throwing the body off the slope. Again this could be rectified by finding the gradient of the slope where the player is standing, and applying the force at the appropriate angle.

Related

How to have accurate collisions in Flash CS6 (AS3)

i am creating an endless runner game and the player as to jump over an obstacle, and if he touches it he looses health. The current collision code is this:
if (man_mc.hitTestObject(crate_mc)) {
health--;
health_txt.text=health.toString();
which is inside a loop. But the problem is that my obstacles are triangles, so when you jump over them if you touch the 'hit box' or dimensions of the triangle you loose health. So how do i make it so the collision is only true if my player is touching the triangle. (and the triangle is a png with transparent background)
EDIT: I found that i could use hitTestPoint instead of hitTestObject but how do i know what co ordinates to put into the parameters?
I would abandon the use of hitTestObject() (or even hitTestPoint()) for collision detection. Instead, I'd use another algorithm for collision detection in your game
I'll assume your player is a (pretty) skinny box and you said that your obstacles are triangles. I'd first try using a basic method of checking if any of the triangle's points are within the player's bounding box. If it is, then you have a collision.
You could also go the other way and check if a player's points in the bounding box are within any triangle, but you may end up with an odd case where a triangle is poking in to the player, but none of the player's points are within the triangle (which can happen if a player gets a triangle right up between the legs >.> ). Also, by going triangle first, there are less points to check per player and triangle pair.
Note that using this simple method won't tell you how much intersection there is, but I'm guessing you may not need that, since you were using the hitTestObject() method before.
Following my comment
I won't write the code for you line by line, but it's pretty easy to figure it out. So I'll describe one method:
Let's say your player is a rectangle that is 50px wide by 100px tall and the center of the player rectangle is set as the player's position. Half the width of the player is 25px and half the height is 50px.
So if the player is at say 240, 370 (a random spot I made up on screen), then to check if a point falls within you'd check if the x point is within 215 to 265 (240 +/- 25px) and if it is, then you'd check if the y point is within 320 and 420 (370 +/- 50px). If both these conditions are true, then the point is either touching or within the player box (so there is a collision). If any of them are false then the point is not (you could stop checking if the first condition is false, saving you a extra check operation).
Once you have that working (a point vs box test), then you'd just run a triangle's points through it to see if the triangle is intersecting the player box. How you get your triangle's points would vary based on your triangles.

AS3: having problems implementing correct rotation with atan2

I have the following scenario: player is in the middle of the screen and never moves from those coordinates. If I click with the mouse on the far right of the screen, the A* script kicks in and moves the background the player is on according with its own walkable/not walkable criteria, so for example if there is an obstacle in between the center of the screen where the player always is in the far right of the screen, the background correctly moves in the opposite direction And when needed moves around the obstacle, so you have the illusion of player walking, but instead only the background moves.
my problem is that I don't know how to rotate my player to simulate that he is following the path that the actual background is making. For example, in the beginning, the player is facing down. If I click on the right side of the screen, the player should face right. When an obstacle is present, the player should face the direction of the path around the obstacle and so on
but again technically my player never moves, so I don't know what coordinates should I have the atan2 use. In my mind I thought that the player should rotate toward the center of the next best hop in the array of best path created during the A* script, but for some reason I can't figure out the correct coordinates y and x for the atan2 command
I bet this is a simple thing I overlooked, but apparently my mind is in shutdown mode, so I can use a new fresh perspective :-)
thanks!
First, do not code in brain shutdown mode, this can save you lots of brainhurt time.
You don't need atan2() in this scenario, since your player is technically on a grid instead of on the screen. Give the player internal coordinates on the grid that will be updated as the player walks around the world, and use them in your A* script. The A* script generates a sequence of tiles the player should walk, thus you derive player's facing from current and next tile, for example, if the next tile to walk is adjacent upwards, you make the player face up and continue moving.

How to render a circular/radial progress bar in Libgdx?

For a game we develop with libgdx, we need to show the user the remaining time for making a move.
How can we render a circular progress bar which shows the remaining seconds?
In the following answer there is a javascript solution :
Circular / radial progress bar
Thanks.
First create a texture (region) with the full circular progress bar. Next deform a shape (mesh) made up of one or more triangles to fit the actual area which should be shown.
For this, think of a line from the center of the image to the bounds of the image. Calculate the intersection point (and "wall", i.e. left, right, top or bottom of the image) of the line at the given angle. This will give you the portion of the texture (region) that needs to be drawn.
For example, if the line at rest (angle is zero) crosses the bottom wall of the image at the middle, your image is square and you want to rotate clockwise, then everything between 45 and 135 degrees hits the left wall, between 135 and 225 degrees hits the top wall, and so on.
Now you have the shape, you'll need to express it in triangle. Or to maintain compatibility with libgdx's spritebatch, an even number of triangles. Make sure to update the U and V values according to the location of the vertex.
A simple implementation of this can be found over here: https://github.com/xoppa/world/blob/master/src/com/xoppa/android/misc/RadialSprite.java
You could use an OpenGL shader to generate a circular progress bar. Here is an animated timer that would be a good place to start: http://glsl.heroku.com/e#10201.0
However, I don't think there is any good Libgdx infrastructure for dropping in shaders like this. You would have to use the OpenGL APIs (see https://code.google.com/p/libgdx/wiki/OpenGLShader).

Flash AS3 - Centering camera on player when scaling in and out (zooming)

I am building a survival horror like game and am hoping to make a very nice camera system to compliment the mechanics. Part of this is the fact that you will be able to crouch down and cover your face. The camera work I want to do with this is to zoom in to the character in order to constrain the view for the player as well.
The current MC structure that I have is:
GameMaster
>
Spawner (this is for the player and all enemies)
>
Player
The issue I'm having is that scaling the GameMaster (which is where side scrolling and other global game effects are happening) causes the centering of the camera to offset based on how far away the player is from 0,0.
You can see the issue clearly in this video. The red arrows point to the 0,0.
On this stackoverflow question the answer says to make a container for everything and center the containers 0,0 over the target that you want to zoom around. This poses a challenge for me because I would then have to get proper coordinates for an object nested 4 MC's in. I'm also unsure what that will then do for my current side scrolling camera.
Is there a way that I could mathematically figure out the offset when the character ducks? It seems like a viable option because you can't move until you let go of crouch and the camera zooms out.
If not, is the container MC a good option or is it just one of those "you gotta do what you gotta do." type situations?
[Added]
I also see something about Transform Matrices or something. Is that something that would work? I know NOTHING about them but I assume they are CPU heavy and wouldn't be a good option for a mechanic prevalent throughout the whole game.
[Added 2]
ALSO, I want to do a rotation camera effect that suffers from the same 0,0 issue. Blatantly showing up as the player and level rotating around some far off pivot point.
If a Transform Matrix can swiftly and functionally offset the 0,0 to the players location so that I can do all the camera effects and alterations. I think that may be the best way to go.
----Close to Conclusion----
In regards to Vespers answer. Would I then be able to tween the resulting transform?
If so then that completely answers my entire problem. If not, I have no clue how to get the result I want.
I think the container is the cleanest solution. Since it'll be centered on the player, rotations and scaling will work normally. You mention getting the coordinates for the nested MC is hard, but there is a built-in function to do exactly that: localToGlobal()
To get the player position in global coordinates, just do player.localToGlobal(new Point(0, 0)). This should return the Player's center in global coordinates. If your main container is not in global coordinates (because it's nested inside another transformed MC, for example), you can use the opposite function on the container to convert from global to local:
container.globalToLocal(player.localToGlobal(new Point(0, 0)))
Now you just need to center the container. That could also be used to simulate the camera movement. If you update the container position at every frame, it'll give the effect of the camera following the player.

Smoothing Free drawing in Html5 canvas

I am implementing free drawing with HTML5 canvas. Currently every thing is working fine. I am using moveTo() and lineTo() for every mousemove. I have to fine tune the drawing;
when I draw some curve like lines with rapid movements, the drawing will be drawn like joints of straight lines. Is there any alternative way for drawing, to make the drawing smoother?
I dont think there is a better way for the drawing itself, however, if you put the draw functions directly onto the mouse move event handlers, then that would slow it down,
To speed that up you could just save the coordinates in an array in the event handlers and wait for the mouse to stop moving before walking trough the array and drawing.
The advantage would be that the events are called more reapidly, making smoother curves, the disadvantge would be that there is a 'lag' if you move the mouse alot.
An alternative would be to try and detect when the user curves and use the appropiate curve drawing method.
I actually did the same thing:
ctx.beginPath();
ctx.moveTo(lp.x-.5, lp.y-.5); // Last recorded position.
ctx.lineTo(cp.x-.5, cp.y-.5); // Current position at time of call.
ctx.stroke();
Bezier Curves are great for pen-like (paths) functionality, but I've ended up with unintended results with that as well, namely the curve between P0 and P2 being too distant from P1... This can be handled by adding extra points against which to evaluate the function (taking it to higher degrees, which seems to be how adobe does it).
I've spent two days answering this question, doing a lot of research of the best examples, tearing through code where available. There are essentially two responses:
1.) Apply a filter – a box- or gaussian- blur will smooth the rough edges a little, making it look less angular.
2.) Apply a Bezier Curve – Between the mousedown and mouseup events, log an array of the points and apply the curve. The longer the line, the slower the re-rendering.(Muro - deviantArt's canvas app appears to do this). [NB: If the idea is to create an artistic web app for people to draw on, show them the original line until the smooth rendering is complete.]
I like somewhere in between, personally. A slight blur tends to soften things, especially near corners, and makes slowly placed (thus frequent, shorter lines) much softer).
Something I'll add, which may be completely obvious so I apologize: Make sure you've set your cap style to 'round' –– ctx.lineCap = 'round'