Zushii
Senior Newbie 
|
 |
«
Posted
2011-03-30 19:06:39 » |
|
Hello  I'm having some problems with my gun intervall timer. It was all fine until I added switchable weapons. Now the intervall is read from the Weapon object and now my swing timer won't work anymore. Do you have any tips? Maybe a method to calculate the time between shots via elapsed time (i have that variable)?
|
|
|
|
|
ra4king
|
 |
«
Reply #1 - Posted
2011-03-30 19:46:21 » |
|
Could you give more information on what exactly you are doing?
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #2 - Posted
2011-03-30 19:53:31 » |
|
Okay.
The player can shoot a gun, the gun - a Object - has a int which determines how fast the gun can shoot.
I used a Timer with a fixed rate. But now I have a few different guns, ie smg, shotgun, pistol with different rates of fire.
I can't use the timer because, the timer will initilalise before the weapon object is added to the players list. So it will always be zero, resulting in uncontrollable rapidfire.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
aazimon
|
 |
«
Reply #3 - Posted
2011-03-30 20:14:52 » |
|
Not knowing how your code is set up. You should call and update to the current gun of the player for each tick of the timer. With the static frame rate, you know how much time between ticks. The gun object would need to keep track of the ticks, and when it reaches the fire rate, it resets the tracking of ticks in the gun object.
|
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #4 - Posted
2011-03-30 20:38:54 » |
|
The Problem ist doing it that way is that when I stop shooting the timer stays where it was. and when I press fire again it will take some time until it shoots. Which is bad. It's supposed to fire immediatly and then only every so-and-so seconds. 1 2 3 4 5 6 7 8 9 10 11 12
| if (firing && weap.getAmmo() > 0) { timer += elapsedTime; if (timer >= weap.getFPS()) { shoot(); weap.decreaseAmmo(); weap.PlaySound(); timer = 0; } } |
|
|
|
|
|
aazimon
|
 |
«
Reply #5 - Posted
2011-03-30 23:00:05 » |
|
Sounds like the gun doesn't know it stop firing and doesn't reset. When you stop pressing the button, can you reset the gun's timer?
|
|
|
|
|
ra4king
|
 |
«
Reply #6 - Posted
2011-03-31 01:51:05 » |
|
From what I understand, you manually change the delay of the Swing Timer to the delay specified by the gun? I suggest having the timer run in a continuous loop, calling an update(long deltaTime) method in the gun, where it keeps track of the firing.
|
|
|
|
philfrei
|
 |
«
Reply #7 - Posted
2011-03-31 04:07:17 » |
|
Just an idea. I haven't ever tried to implement this situation before but maybe the following is not too inefficient.
Have a datetime variable stored with your Object/Gun that holds the "next" time it is OK to fire. Then, with each shot, update that variable by that Object's refire interval.
For example, if you want to only allow 5 shots a second, when you shoot, add 200ms to "Now" and store it. Then consult this time variable (compared to "Now") before firing again.
Maybe?
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #8 - Posted
2011-03-31 16:55:57 » |
|
Thank for the tips. by reseting the timer when he stops shooting, I fixed the problem. Now I have a different question. I want the player to aim via mouse and therefor I need to calculate the angle of the player to the mouse. 1 2 3 4 5 6 7 8 9
| public double getAngleOfTwoPoints(double x1, double y1,double x2, double y2) { double DeltaX = x2-x1; double DeltaY = y2-y1; double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)+ 89.65f); return angle; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public void mouseMoved(MouseEvent e) { mx = e.getX(); my = e.getY(); angle = vk.getAngleOfTwoPoints(x, y, mx, my); e.consume(); } public void mouseDragged( MouseEvent e ) { mx = e.getX(); my = e.getY(); angle = vk.getAngleOfTwoPoints(x, y, mx, my); firing = true; e.consume(); } public void mouseReleased( MouseEvent e ) { firing = false; e.consume(); } public void mousePressed( MouseEvent e ) { mx = e.getX(); my = e.getY(); angle = vk.getAngleOfTwoPoints(x, y, mx, my); firing = true; e.consume(); } |
thats how I calculate it. But for some reason there is a huge offset to my Mouse X,Y pos and the angle he shoots at. Heres a pic to illustrate my problem: 
|
|
|
|
|
kevglass
|
 |
«
Reply #9 - Posted
2011-03-31 17:06:02 » |
|
1
| double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)+ 89.65f); |
Looks a bit dodgy. Is the 89.65 meant to be 90 degrees? More over shouldn't it be outside the brackets? 1
| double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)) + 90; |
Just guessing. Kev
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #10 - Posted
2011-03-31 17:44:38 » |
|
Thanks that helped, but there is still a minor offset.
|
|
|
|
|
kevglass
|
 |
«
Reply #11 - Posted
2011-03-31 17:46:00 » |
|
Can you show the code how you:
a) Draw the line b) Move the bullets
and how that relates to "angle" and mx and my.
Kev
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #12 - Posted
2011-03-31 18:02:59 » |
|
1
| missiles.add(new Missile(x+width/2,y+height/2,angle)); |
The missile is added this way. 1 2 3 4 5 6
| public void move() { x += Math.sin(Math.toRadians(angle)) * MISSILE_SPEED; y -= Math.cos(Math.toRadians(angle)) * MISSILE_SPEED; } |
This is how they are moved. and the line was just drawn via paint^^ but to give you the idea: 1
| g.drawLine(mx, my, (int)player.getCenterX(), (int)player.getCenterY()); |
I calculate the angle from the players center to the mouse position. and then give the angle calculated to the missile object.
|
|
|
|
|
kevglass
|
 |
«
Reply #13 - Posted
2011-03-31 18:56:11 » |
|
Ah ha! Then I suspect your player sprite isn't quite central on the line. Try replacing the player sprite termporarily with a straight line down the middle of a sprite. It'll probably match that - you'll need to adjust for the shape of sprite... maybe!  Kev
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #14 - Posted
2011-03-31 19:03:47 » |
|
nope. Its lined up correctly.
weird. It seems that at certain angles its fine, but at some others it has a small offest of about 10-20pixels or so.
|
|
|
|
|
kevglass
|
 |
«
Reply #15 - Posted
2011-03-31 19:07:47 » |
|
Tis strange, is the X/Y of the player always right? You might want to fill a circle at that location to check?
Kev
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #16 - Posted
2011-03-31 19:17:22 » |
|
EDIT:
I actually noticed that the missile does not leave the player correctly sometimes. I will look into this.
|
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #17 - Posted
2011-03-31 20:13:59 » |
|
The center of the player seems to be slightly not the same all the time. Though this irritates me since the player is 40x40 px so there is no weird center when rotating. But it seems that when you rotate the player via mouse. the center is not the same center as the players center. Heres pic for reference:  as you can see the missile is displaced from the beginning. The oval represents the center of the player. (the center of the oval would be the exact center of the player.)
|
|
|
|
|
kevglass
|
 |
«
Reply #18 - Posted
2011-03-31 20:18:00 » |
|
Whats code for drawing your player and missle?
Kev
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #19 - Posted
2011-03-31 20:41:59 » |
|
1 2 3 4 5 6 7
| Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics(); if (player.isvisible()) { gRV.rotate(Math.toRadians(player.getAngle()),player.getX()+player.getImage().getWidth(null)/2,player.getY()+player.getImage().getHeight(null)/2); gRV.drawImage(player.getImage(),(int)player.getX(),(int)player.getY(), canvas); } gRV.dispose(); |
1 2 3 4 5 6 7
| for (int i=0; i<mPlayer.size(); i++) { Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics(); Missile m = (Missile)mPlayer.get(i); gRV.rotate(Math.toRadians(m.getAngle()),m.getCenterX(),m.getCenterY()); gRV.drawImage(m.getImage(),(int)m.getX(),(int)m.getY(),canvas); gRV.dispose(); } |
|
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #20 - Posted
2011-04-01 18:07:38 » |
|
Can nobody help? Haven't fixed the problem yet. 
|
|
|
|
|
philfrei
|
 |
«
Reply #21 - Posted
2011-04-01 21:32:53 » |
|
Poor boo!  Things I would check or verify: 1) that the graphic's center matches the point you actually want to shoot from (e.g., if the graphic is asymmetrical); 2) that the translational rotation is doing exactly what you think it is (you could be rotating around a different point than you are assuming); 3) that the 'context' of the values used for drawing the shooter matches the 'context' used for the raygun path draw (the [x,y] invoked before an affine transform doesn't necessarily match the position on the screen after the transform). There are lots of ways to screw up, here. Verify all the steps.
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
ra4king
|
 |
«
Reply #22 - Posted
2011-04-01 21:36:48 » |
|
Ah you stole those points right from my keyboard 
|
|
|
|
Zushii
Senior Newbie 
|
 |
«
Reply #23 - Posted
2011-04-01 22:28:56 » |
|
You actually were right with the second one. Ive got so much code , that I totally didn't see that I was using the x,y pos of the player and not the center, to calculate the angle. Thanks  So so tired.
|
|
|
|
|
|