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 (535)
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  
  Boomerang logic?  (Read 1415 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2011-08-10 13:35:01 »

I'm currently prototyping a 2d side scroller and I'm looking for some advice on how to plot the path of a boomerang projectile.

For the prototype, the projectile will move in a simple straight line away from and back to the thrower (I know not very realistic). I was thinking of using A* pathfinding to plot the route of the boomerang, but I though it could be possible to create a line object (not rendered) dividing that up equally into nodes and using that to plot the projectiles course.

I know both are possible, but I wondered which is easier to implement and relatively speaking, which is a better implementation?
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #1 - Posted 2011-08-10 15:10:41 »

I think it would be even easier to just forget about pathing the boomerang. Just shoot it out like a bullet and have a timer that will expire (or when it collides with a wall/enemy) and tell the boomerang to a "go back to player" state. In this state, it will stop moving forward and instead, track back towards the player (just like a follow missile).
Offline westloar

Junior Member





« Reply #2 - Posted 2011-08-10 16:43:50 »

so to track back it would have remembered the route it took and just backtrack that after it's max time out?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #3 - Posted 2011-08-10 16:54:08 »

so to track back it would have remembered the route it took and just backtrack that after it's max time out?


No, pathfinding is too expensive an operation for projectiles (especially if you have lots).

To backtrack to the source, you simply get position of source and the projectile and then move projectile closer towards source (at desired speed). You repeat this every frame until projectile reaches the source. To get a more smoother curvey move back just slowly subtract new direction from projectiles current direction.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-08-10 16:55:49 »

Instead of a timer you can check the distance from the boomerang to the player, and make it turn around accordingly.

Offline Z-Man
« Reply #5 - Posted 2011-08-10 17:02:33 »

Instead of a timer you can check the distance from the boomerang to the player, and make it turn around accordingly.
What if the player moves though? That would throw of the distance between the player and the thrown boomerang. Unless the player has to stand still while the boomerang is in the air.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2011-08-10 17:20:00 »

I'd probably go for steering behaviours for this ( http://www.red3d.com/cwr/steer/ ), assuming you always want the boomerang to return to the character. Just a simple one which start with a 'repel' and switches to an 'attract' after a certain amount of time would do it.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #7 - Posted 2011-08-10 17:24:14 »

Instead of a timer you can check the distance from the boomerang to the player, and make it turn around accordingly.
What if the player moves though? That would throw of the distance between the player and the thrown boomerang. Unless the player has to stand still while the boomerang is in the air.

Not the current position, but the one from when it was cast. It should navigate to the current position when it's returning though.

Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #8 - Posted 2011-08-10 17:25:53 »

so to track back it would have remembered the route it took and just backtrack that after it's max time out?


No, pathfinding is too expensive an operation for projectiles (especially if you have lots).

To backtrack to the source, you simply get position of source and the projectile and then move projectile closer towards source (at desired speed). You repeat this every frame until projectile reaches the source. To get a more smoother curvey move back just slowly subtract new direction from projectiles current direction.

Kappa is correct. You can google examples of follow missiles or homing missiles as they are the exact same logic you want.

Formula I found from google to go towards a target:
1  
2  
3  
4  
5  
6  
        int dy = targetY - y;
        int dx = targetX - x;
        double speed, sep;

        sep = Math.sqrt(dx * dx + dy * dy);
        speed = scale/sep;


There is a cheaper way than doing square roots but at this point, you shouldn't be worrying about optimization as small as that.
Offline westloar

Junior Member





« Reply #9 - Posted 2011-08-10 18:36:10 »


Kappa is correct. You can google examples of follow missiles or homing missiles as they are the exact same logic you want.

Formula I found from google to go towards a target:
1  
2  
3  
4  
5  
6  
        int dy = targetY - y;
        int dx = targetX - x;
        double speed, sep;

        sep = Math.sqrt(dx * dx + dy * dy);
        speed = scale/sep;


There is a cheaper way than doing square roots but at this point, you shouldn't be worrying about optimization as small as that.

Thanks for all the replies guys, you may have noticed I am new to programming in general, although I'm picking it up fast, so please bear with me Smiley !

Could you explain how the above formula works, I don't quite understand where some of the "x" and "y" 's come from.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #10 - Posted 2011-08-10 19:07:56 »

The x and y are the boomerang coordinates and the targetX and targetY would be your character's coordinates.

Try to get this working, it's simpler:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// when the boomerang is ahead of the character, move it backwards and move it
// forwards when it is behind the character.
if(boomerangX > characterX){
   boomerangX-=boomerangSpeed;
}
else{
   boomerangX+=boomerangSpeed;
}

// when the boomerang is below of the character, move it upwards and move it
// downwards when it is above the character.
if(boomerangY > characterY){
   boomerangY-=boomerangSpeed;
}
else{
   boomerangY+=boomerangSpeed;
}


Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #11 - Posted 2011-08-10 19:43:09 »


Kappa is correct. You can google examples of follow missiles or homing missiles as they are the exact same logic you want.

Formula I found from google to go towards a target:
1  
2  
3  
4  
5  
6  
        int dy = targetY - y;
        int dx = targetX - x;
        double speed, sep;

        sep = Math.sqrt(dx * dx + dy * dy);
        speed = scale/sep;


There is a cheaper way than doing square roots but at this point, you shouldn't be worrying about optimization as small as that.

Thanks for all the replies guys, you may have noticed I am new to programming in general, although I'm picking it up fast, so please bear with me Smiley !

Could you explain how the above formula works, I don't quite understand where some of the "x" and "y" 's come from.

Each object in play needs an on-screen position, hence the x/y. When doing these free-flying objects like you do now, an approach to just very basic physics is to assign a pair more of doubles of the veloctity of the object. Two, because one for velocity on the x-axis and one on the y-axis.
Then when you update position, you can do something like:
1  
player.setX(player.getX() + player.getXV());

This is provided that your x/y is also doubles (just cast them to integers when drawing).
Acceleration is as easy as player.setXV(player.getXV() + GameMain.SPEED_FACTOR); and be sure to decrease it as well if you want that Smiley

Offline westloar

Junior Member





« Reply #12 - Posted 2011-08-10 21:00:37 »

Thank you for the simplified formula kalkitus, much appreciated.

But my main point was that I literally didn't understand which variables were referring to which objects so I only needed clarification for that Smiley
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 (6 views)
2014-07-29 12:53:52

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

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

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

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

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

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

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

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
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!