How to attach a weapon to players hand and implement dynamic aiming? - libgdx

I wanted the weapon to be pointing in the direction of wherever the mouse is,[![This is my player texture][1]][1]
what i want is, if a weapon is in players hand, than the weapon along with the hand, being in perfect sync with the body points in the direction where ever the mouse is
I have no idea how to implement this mechanism, please help

First off it completely depends on how the character is rigged. Are you using Spriter or something to that effect to produce a 2d skeletal animation rig? If so then the IK from that should take care of a weapon if you just attach it to the handbone. If not then simple trig is going to be your friend. Get the angle of the mouse to the center of the character or character's hand and then cos with be your X position for the weapon and sin will be your Y position.

Related

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.

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.

Collision and bounce detection from array of points

I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside.
Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically.
I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not.
My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.
Check the corners of your bouncing ball with four hitTestPoints. If it succeeds, then do hitTestPoints from the fence with shapeflag set to true.
There may be better solutions as I do not know the performance impact of shapeflag, but combined with the corners optimization I think it will be good, but I'm also interested if there is a better way.
Math will be your friend here. Do a quick search for circle-line, or point-line collision (here's one: Circle line-segment collision detection algorithm?).
What you do is run through your array of points, creating lines. So line 1 will be points[0] and points[1], and line 2 will be points[1] and points[2]. After that you check each line against your ball (if you want proper collision that will work no matter the frame rate, then you create a ball line, which is the line that the ball has travelled along between the last frame and this one). Do your collision detection against the ball line and each line made from your points (there's tons of line-line collision detection algos on the web). What you'll get out of an algorithm like that is the time the collision takes place in the current time step, as well as the normal of the colliding line, which will give you the reflection angle.
If you don't know Vector math, then learn it, it'll make your life a ton easier. Again, there are tons of implementations of a Vector2 class on the net.
You can arbitrarily remove parts of the wall as needed by just ignoring those points in your check.
Another lazy solution would be to use a physics engine like Box2D http://box2dflash.sourceforge.net/ or Nape: http://code.google.com/p/nape/ - it might be overkill for what you want for your game, but hey, it's easy.
For bonus points, another technique which might be easier for you is the Separating Axis Theorem, which is used in the flash game N: http://www.metanetsoftware.com/technique.html

Sailboat Animation: Turn tiller to left with mouse, boat turns to right & vice versa

I am trying to create an animation that will show the basics of steering a sailboat. The idea is that there will be a top view of the boat with the tiller and rudder very pronounced. I want the learner to be able to click and drag the tiller one direction or the other, while the tiller is dragging I want the rudder and the boat to rotate the proper direction. Example, I want the boat to go left I will push the tiller to starboard and the rudder will rotate clockwise and the boat will turn left. There doesn't need to be forward motion by the boat in my head, I think that as long as the boat rotates it would be fine. Any help or resources would be appreciated.
I think what you really need to think about is modeling. Create a non-UI representation of a sailboat. Build your code such that moving the tiller (without the mouse... just through the interface of your model) causes the model of your boat to move. Actors and attributes of the model might include:
Direction and Force of the wind vector
Direction of the tiller
Direction of the sail
Direction of the tiller
Direction and Force of the boat vector
Size/Shape of the sail
Length of the boat
Coefficient of friction between the boat and the water
Mass of the boat
Size of the waves
Of course, the the further you go in the factors of your model refine its behavior more and more. I recommend adding unit tests to drive this behavior. Stop at whatever level of refinement makes most sense for the fidelity of your model.
Once you have a good model in place, it becomes much more simple and straight-fowrward to hook it up to graphical assets. For instance, you can rotate the boat or tiller graphic based on the angles you have exposed in your model using a rotation transform. The same would be true of the angle of sail, or the speed that you move your water animation.
This approach allows you to design the animations separate from the behavior of the boat.
Good luck!
Well - as long as you know the basics of programmatically transforming displayObjects, this shouldn't be too tough to do
I might go about it like this:
Create the graphics for your different elements. You are essentially going to be controlling the rotation property of these objects, so your translation points need to be set correctly at the point of rotation.
Once that is set up, its as simple as adding a mouse down event listener to the tiller, and testing for the delta change in mouseX until mouse up is registered.
During the animation cycle (while the mouse is down), just apply varying degrees of rotation (based on mouseX delta change) on your three objects. IE: If the mouse is dragged left, rotate the tiller to match, the rudder in the opposite direction, and the parent (the boat) also to the left.
This, obviously is very rudimentary, but should create a reasonable analogue of your intent...
Hope that helps

Animation Library

I'm new to programming games, so I'll make it short and sweet:
Is there any standard practice for Flash/AS3 for organizing sets of game Sprites, especially related to movement. For Example, if I am moving left, and then down, there should be a fill in animation...if we want that fill-in animation, do we need to create an animation for every permutation of all 8 directions our character can move? Also, if we want to have dynamic shadows for our 2d sprites, do we have to draw a light source for every one of those 8 directions for all 8 directions?
No, there is no practice like that.
However, here are some tips from my personal experience:
fill-ins:
if your sprite is small, just don't do any
other - SOME filling - for example, just from one direction to the one near it, and whenever the sprite rotates more than just on direction, play the fill-ins in a row
if you want to make ALL fill-ins, that would be a lot of work, I would say too much for something as small as a fill-in between two directions - better spend the time for something else
light sources:
again, matters HOW visible this sprite will be - if it is something very small, just mirror and / or don't make any lighting at all
other - I am used to always make one sprite (fully redrawn, or just light redrawn - does not mater) - for all directions - it adds a nice touch to visible things like player sprite that are always going to be on the screen