Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Use drawLine to render a bullet(Slick2D).  (Read 2312 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Member


Projects: 3



« Posted 2012-12-28 22:11:06 »

I have the coordinates for the weapon object, which is fire at the target object, which I also have the coordinates to.
I am trying to create a bullet effect.
I tried some stuff out, but they didnt work as expected ingame.
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2012-12-28 22:13:30 »

Well??
What do you want us to do about it?

We can't do anything about it unless you tell us what you want and give us the neccessary information.

Offline P0jahn

Senior Member


Projects: 3



« Reply #2 - Posted 2012-12-28 22:23:50 »

A gun fire at target. The projectile is traveling towards the target. I want to create a bullet effect, which would start at the projectiles x and y coordinate, and end... exactly where? I cant just use the projectiles x and y + 10(which would be the length of the bullet) because then would the bullet always be a vertical line rather than a rotated line.

I cant figure out how to get a correctly rotated line.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #3 - Posted 2012-12-28 22:27:07 »

You're probably going to have to use the Math functions to accomplish this. But, you still didn't give enough information on what you are looking for to help further.

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2012-12-28 22:27:26 »

1  
2  
   float nx = (float) ((x * Math.cos(Math.toRadians(rotation))) - (y * Math.sin(Math.toRadians(rotation))));
   float ny = (float) ((x * Math.sin(Math.toRadians(rotation))) + (y * Math.cos(Math.toRadians(rotation))));


Just translate before rotation and translate back after.

If you don't know how to translate the coordinates, google it.

Offline P0jahn

Senior Member


Projects: 3



« Reply #5 - Posted 2012-12-28 22:40:10 »

I know how to use translate. May I ask how I calculate the rotation variable?
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2012-12-28 22:44:31 »

Here

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2012-12-28 23:08:26 »

Basically, I'm here to sum it up:

Let's assume you have a player position and a mouse - or better - target position:
1  
2  
Vec2 playerPos;
Vec2 targetPos;

Vec2
's simply store
public float x, y;
s.

To get the degree angle between those two vectors:
1  
2  
// atan2 is a magical function. It was invented by god... or probably a mathematician, but I'm not sure 'bout that...
float degrees = Math.toDegrees(Math.atan2(targetPos.x - playerPos.x, targetPos.y - playerPos.y));


You can then use the degrees to translate the x and y position of the bullet:
1  
2  
3  
4  
5  
6  
// assume this:
Vec2 bulletPos;
// Then we have this update code:
float deltaX = Math.cos(Math.toRadians(degrees)) * speed;
float deltaY = Math.sin(Math.toRadians(degrees)) * speed;
// speed is the speed. If speed is 1 and degrees is 0, then the bullet travels directly to the right. With step size 1 at a time.


Finally we can optimize it by removing the
Math.toDegrees
at the atan2 and the
Math.toRandians
from the sine and cosine.
Now instead of converting from
delta vector -> direction (radians) -> direction (degrees) -> direction (radians) -> delta*, we now do
delta vector -> direction (radians) -> delta*.

Marvel at the mighty code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// Get direction between two positions:
public float getDirectionRad(Vec2 v0, Vec2 v1) {
   return Math.atan2(v1.x - v0.x, v1.y - v0.y);
}
// How to use:
float directionInRadians = getDirectionRad(playerPos, targetPos);
// Get the deltaX and deltaY translation for a given direction:
float deltaX = Math.cos(directionInRadians);
float deltaY = Math.sin(directionInRadians);
// Translating the bullet's position:
bulletPos.x += deltaX;
bulletPos.y += deltaY;

I hope I summed it up so people understand it Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2012-12-28 23:15:01 »

Doesn't
atan2()
only return values that (when converted to degrees) are between 0.0000000000000...001 & 89.99999... ?

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-12-28 23:20:44 »

Doesn't
atan2()
only return values that (when converted to degrees) are between 0.0000000000000...001 & 89.99999... ?
?
I use it in WorldOfCube (see signature) and it works perfectly, as you can probably see there. (Arm rotation)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #10 - Posted 2012-12-28 23:28:15 »

Doesn't
atan2()
only return values that (when converted to degrees) are between 0.0000000000000...001 & 89.99999... ?
?
I use it in WorldOfCube (see signature) and it works perfectly, as you can probably see there. (Arm rotation)

Must be my bad usage.

Never mind, I was trying to calculate the angle from one vector (a velocity vector).
Maybe if I tried again with the proper math...

(Off topic: Notice how I changed my avatar to reflect my temporary ranking  Wink)

Offline theagentd
« Reply #11 - Posted 2012-12-29 04:52:02 »

...

You guys are overcomplicating this. No need for angles here, you have a velocity vector! Just draw a line between (x, y) and (x - velocityX, y - velocityY)!

Myomyomyo.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #12 - Posted 2012-12-29 11:17:37 »

But but the angles, we need to use the radians stat or the cosines will get lonely!

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2012-12-29 11:31:09 »

...

You guys are overcomplicating this. No need for angles here, you have a velocity vector! Just draw a line between (x, y) and (x - velocityX, y - velocityY)!

Oh. I thought he'd have a sprite :X

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #14 - Posted 2012-12-29 15:17:22 »

Ifhe has a bullet, he knows where it's going.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!