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 (406)
games submitted by our members
Games in WIP (293)
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  
  Enemy rotation and following wonky...  (Read 396 times)
0 Members and 1 Guest are viewing this topic.
Offline PRW56

Senior Newbie





« Posted 2012-11-27 07:51:04 »

Attempting to make enemies turn and follow player, but they all seem to... I'm not sure, they rotate in the general direction of the player, but at times they go off on their own. The way I organized my classes is horrendous, but I wanted to throw something together to test and see if I could do something like this... apparently not.

I'm Posting the whole code here:
 http://pastebin.java-gaming.org/b5fd1362b3f

but I know it has to be something wrong with the rotation I use, here is where I update the enemies:
1  
2  
3  
4  
5  
6  
7  
for(Move sube: enemies)
        {
            //set rotation
           sube.setRot(Math.atan2(playerCenterY - sube.getCenterY(), playerCenterX - sube.getCenterY()));
            sube.setXloc(sube.getX() + (ENEMY_SPEED * Math.cos(sube.getRot())));
            sube.setYloc(sube.getY() + (ENEMY_SPEED * Math.sin(sube.getRot())));
        }


Am I calculating the rotation wrong? any help is appreciated
Offline moogie

JGO Knight


Medals: 7
Projects: 5


Java games rock!


« Reply #1 - Posted 2012-11-27 09:03:05 »

Looks like you are using the y component twice instead of the x component in your setrot method call
Offline PRW56

Senior Newbie





« Reply #2 - Posted 2012-11-27 09:53:53 »

I need to brand STUPID in big bold letters on my forehead, ty for finding that. While I'm here though do you know of a way to rotate the rectangles points, because it is only rotated when its drawn and this doesn't work for hit detection, heck even if i knew how to rotate the 4 corners of the rectangle I wouldn't know of a method to check whether or not something is within that area...
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline moogie

JGO Knight


Medals: 7
Projects: 5


Java games rock!


« Reply #3 - Posted 2012-11-27 10:37:09 »

well a simple but probably horribly inefficient way is to have an Area for each rectangle and then perform an Area.intersect(other area) see http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Area.html

Offline PRW56

Senior Newbie





« Reply #4 - Posted 2012-11-30 06:51:16 »

I get how to do that, I usually do something along these lines:

if(subb.getCenterX()>=sube.getX()&&subb.getCenterX()<=(sube.getX()+sube.getWidth())&&subb.getCenterY()>=sube.getY()&&subb.getCenterY()<=(sube.getY()+sube.getHeight()))

which is just checking whether or not a point is within a rectangular area, but I know methods that check whether or not, for example, a circle and rectangle intersect. I have no idea how they check within the area of a circle like that as well as other irregular shapes.
Offline namrog84

JGO Knight


Medals: 23
Projects: 2


Keep programming!


« Reply #5 - Posted 2012-11-30 09:04:08 »

I get how to do that, I usually do something along these lines:

if(subb.getCenterX()>=sube.getX()&&subb.getCenterX()<=(sube.getX()+sube.getWidth())&&subb.getCenterY()>=sube.getY()&&subb.getCenterY()<=(sube.getY()+sube.getHeight()))

which is just checking whether or not a point is within a rectangular area, but I know methods that check whether or not, for example, a circle and rectangle intersect. I have no idea how they check within the area of a circle like that as well as other irregular shapes.

circles are usually just distance checks or radius checks from the center point

shapes other than basic primitives(circle/aabb) usually require more complicated approaches, such as Separating Axis Theorem for example

"Experience is what you get when you did not get what you wanted"
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 (78 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

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

UnluckyDevil (186 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.117 seconds with 20 queries.