Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 1164 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Junior Member


Projects: 1



« 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 Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


« 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

Junior Member


Projects: 1



« 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!
Try the Free Demo of Revenge of the Titans
Offline ctomni231

JGO Knight


Medals: 28
Projects: 1


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 Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


« 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

Junior Member


Projects: 1



« 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 Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


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

Here

Online matheus23

JGO Wizard


Medals: 71
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

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Offline HeroesGraveDev

JGO Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


« 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... ?

Online matheus23

JGO Wizard


Medals: 71
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)

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline HeroesGraveDev

JGO Wizard


Medals: 62
Projects: 8


Muahahahahahaha...


« 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!

Online matheus23

JGO Wizard


Medals: 71
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

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (42 views)
2013-05-17 21:29:12

alaslipknot (51 views)
2013-05-16 21:24:48

gouessej (79 views)
2013-05-16 00:53:38

gouessej (79 views)
2013-05-16 00:17:58

theagentd (87 views)
2013-05-15 15:01:13

theagentd (81 views)
2013-05-15 15:00:54

StreetDoggy (123 views)
2013-05-14 15:56:26

kutucuk (147 views)
2013-05-12 17:10:36

kutucuk (147 views)
2013-05-12 15:36:09

UnluckyDevil (157 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.164 seconds with 21 queries.