JAVA: 2D Game Adventure. Collision Detection problem - java

This is about animation in JAVA. I had success when using same dimension on all picture. But if i keep all picture dimension on same size(width and height) i get some bug which when player punch. Before player's hand touch enemy body, enemy died
But others with my case where idle, run, and punch has an different
dimension. Punching animation facing to the left became very strange.
Should his hand hit to the left but his body shifts to the right. This
is because I draw on the x & y are the same.
How can I fix it? Need instructions :D
I use png coz support transparent
I think this can fix with 2 option
1. Fix my collision detection
2. Fix of drawing position my image when some condition occur

Trying to picture your problem, hope this helps.
I am typeing directly from my head, so there are might errors in code
fixing coalision decection
i would try this
Image fist
Image enemy
//in paint
g2D.drawImage(fist,x,y,this);
g2D.drawImage(enemy,x1,y1,this);
Rectangle2D myFist = new Rectangle2D.Double(x,y,fist.getWidth(this),fist.getHeight(this));
Rectangle2D myEnemy = new Rectangle2D.Double(x1,y1,enemy.getWidth(this),enemy.getHeight(this));
if (myEnemy.contains(myFist){
//action u want to happend
}
I think something like this should fix coalision problems
I am seeing this as mario a game on sega
Fix of drawing position
//arm image can be the same image if u want
Image leftArm;
Image rightArm;
image headLegsAndTorsoLeft;
image headLegsAndTorsoRight;
//where am i looking in game if true i look to the leftside of user thats playing
boolean turnedLeft
//in paint
if(turnedLeft){
//this lets it look like he is turned to the left with his right arm in the visible behind his left.
//draw right arm
g2D.drawImage(rightArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoLeft,x-3,y,this);
// draw left arm a bit more in x coor
g2D.drawImage(leftArm,x-6,y,this);
}else{
//this lets it look like he is turned to the right with his left arm in the visible behind his right.
// draw left arm
g2D.drawImage(leftArm,x,y,this);
//draw body moved a bit in x coor
g2D.drawImage(headLegsAndTorsoRight,x-3,y,this);
//draw right arm a bit more in x coor
g2D.drawImage(rightArm,x-6,y,this);
}
same order for animation of arms, ultimatly i would use different methods animations for torso, leftarm, rightarm
something like keypressed leftarrow torso does walking animation left, hit left arm key moves left arm,hit right arm key moves right arm,thats 3 for lets say left arm, now u need another 3 for when ur char is moved to the right.
Thats how i would try to do things.

Related

BroadSword attached to a Player jumps ahead 2 pixels when moving player in libGDX

Here is the deal. I have a player that has a broadsword within a tiled game map. The broadsword is attached to the players location, and when the left mouse button is pressed, it rotates around the player to simulate a "swing". However, whenever I move the player when im attacking with the broadsword, the broadsword which is represented by a 5x5 blue dot, jumps ahead 2 pixels of the direction im heading in.
I have had this problem with the player, however it was easily fixed with using batch.setProjectionMatrix(camera.combined)
This time, no matter what I do using the projection matrix, the blue dot either jumps 2 pixels ahead, or jumps 10-20px ahead then becomes normal.
Here is the render method for the BroadSword class, where I have tested multiple scenarios, and I have included the scenario that is the closest to the non-bouncing:
...
public void render() {
if(!isSwinging)
return;
//Using a new Batch object, not the one from the Main class and
// setting the projection matrix to camera.combined after we draw the broadsword
// (a blue dot) is the only way that partially works.
batch.begin();
batch.draw(texture, broadSwordPos.x, broadSwordPos.y);
batch.setProjectionMatrix(camera.combined);
batch.disableBlending();
batch.end();
}
...
The method above is the only method that partially works (At the start when I start attacking, the broadsword jumps ahead 10 to 20 pixels ahead for a fraction of a second, then returns to the desired position)
Any other method, such as using the SpriteBatch object used in the Main class
(batch pasted to the gameworld, which is pasted to the player, which is then pasted to the broadsword)
or setting the projection matrix before the draw, dosen't work, and causes the bouncing 2px ahead problem.
What is wrong here? (Please let me know if I need to provide more code)

How to get the vertices of a rect() in Processing?

Using the OpenCV for Processing library by Greg Borenstein, I am able to detect a user's face within a webcam stream, and draw a rectangle around it. Here is the portion that draws the rectangle to the screen:
Rectangle[] faces = opencv.detect()
for (int i = 0; i < faces.length; i++)
{
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
As the user moves their face, the rectangle will move accordingly.
I want to draw a line on the screen where one point is set at predefined location and the other is always pinned to the bottom right hand vertex of the rectangle. This way the length and angle of the line will change with respect to the rectangle's location.
One thing I tried was to subtract some values from faces[i].x and faces[i].y until I reached the bottom right vertex, but I found that the depth of the face in the webcam made this method not work.
With that, how could I find the above mentioned vertex of the rectangle so that I can properly draw the line?
The x position of a rectangle gives you its left side. So x+width gives you its right side.
Similarly, the y position of a rectangle gives you its top side. So y+height gives you its bottom side.
float rectRight = faces[i].x + faces[i].width;
float rectBottom = faces[i].y + faces[i].height;
If that's still not what you're looking for, then please post a small example that we can actually run. Use hardcoded values instead of relying on opencv, so we can copy and paste your code and see the problem ourselves.

Rotating a Sprite in Java

While working on Projectiles I thought that it would be a good idea to rotate the sprite as well, to make it look nicer.
I am currently using a 1-Dimensional Array, and the sprite's width and height can and will vary, so it makes it a bit more difficult for me to figure out on how to do this correctly.
I will be honest and straight out say it: I have absolutely no idea on how to do this. There have been a few searches that I have done to try to find some stuff, and there were some things out there, but the best I found was this:
DreamInCode ~ Rotating a 1-dimensional Array of Pixels
This method works fine, but only for square Sprites. I would also like to apply this for non-square (rectangular) Sprites. How could I set it up so that rectangular sprites can be rotated?
Currently, I'm attempting to make a laser, and it would look much better if it didn't only go along a vertical or horizontal axis.
You need to recalculate the coordinate points of your image (take a look here). You've to do a matrix product of every point of your sprite (x, y) for the rotation matrix, to get the new point in the space x' and y'.
You can assume that the bottom left (or the bottom up, depends on your system coordinate orientation) of your sprite is at (x,y) = (0,0)
And you should recalculate the color too (because if you have a pure red pixel surrounded by blue pixel at (x,y)=(10,5) when you rotate it can move for example to (x, y)=(8.33, 7.1) that it's not a real pixel position because pixel haven't float coordinate. So the pixel at real position (x, y)=(8, 7) will be not anymore pure red, but a red with a small percentage of blue)... but one thing for time.
It's easier than you think: you only have to copy the original rectangular sprites centered into bigger square ones with transparent background. .png files have that option and I think you may use them.

Flipping horizontally sprites slick2d

I'm writing an arcade multi-level game with Slick2D and I'm writing the code of the entities. I'm trying to flip horizontally the sprite. The first time I designed 2 images for the same sprite, the first facing right and the second facing left.
Now I'm trying to flip horizontally the sprite with the getFlippedCopy() function, but I cannot rotate the entire animation, I can rotate only a frame.
How can I solve?
If you are drawing an animation, it can be best to utilize the getCurrentFrame() method, as that's what's about to be displayed anyway.
Do something like this:
animation.getCurrentFrame().getFlippedCopy(direction, false).draw(x, y);
With this in your render method, you set the orientation of the image based on the direction they are face. So if they are facing left, 'direction' could be true, so the current animation image will draw flipped at that x and y (which is what you want), and if facing right, direction would be false, in which no flipped orientation would occur and it would show the player facing right (depending on the image; if they are facing left in the original spritesheet, the opposite will be true).
If you are doing a bit more complex orientation changes, such as 4 directional movement:
anim.getCurrentFrame().setRotation(90*(direction));
anim.getCurrentFrame().draw(x, y);
where 'direction' is a North/East/South/West representative integer, 0-3. Such as 0 = North, 1 = East, etc etc.

Shape Rotation Issue - Java2d

I am developing one simple game in which i have encountered one small but important issue.
I have implemented absolute rotation in my logic.
When i start rotating an object when the object does not have any rotation , it works fine and i can rotate as in any direction without any problem as shown in the following link.
Initial Rotation Video
Now the problem arise when the object does have some rotation , and why i try to rotate in one of the direction , instead of being rotated in desire direction the rotation always starts from initial rotation as shown in the following link.
Rotation issue when shape has some rotation
I think the video shows everything , still if you have any questions please ask me.
I think the problem is , there should be a relative rotation in the direction of mouse pointer from whatever circle is selected .
Now about My Logic,
in mouse press event i just checked
Mouse Press
Whether the shape is selected on the canvas , if yes
if one of the four circles contains mouse point if yes
then initiateRotation
Mouse Drag
Using Vector Maths
I update the motion according to mouse points ,
calculate rotation angle according to the following method
Math.atan2(rotationVector.getY(), rotationVector.getX());
and apply rotation on this shape.
Rotation Vector i get from this class
Vector Rotation
I called above class startMotion in mouse press and updateMotion in mouse drag event.
What am i missing or doing anything wrong ?
We need to see some code to be able to help you out. It looks like you reset the rotation, whenever you initiateRotation, and then the object quickly rotates in place, according to your mouse position when you drag.

Categories