Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 698 times)
0 Members and 1 Guest are viewing this topic.
Offline PRW56

Senior Newbie





« Posted 2012-11-27 06: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: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2012-11-27 08: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 08: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!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2012-11-27 09: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 05: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 Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #5 - Posted 2012-11-30 08: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  
 
 
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.

BurntPizza (21 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (87 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!