Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 2D Question - Targetting a location, moving to, then through it in a straight line.

Author
Message
Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 2nd Oct 2008 19:51
Okay, I feel a little stupid here. I've forgotten most of my Highschool math, which is a shame.

Here's the rundown: I am attempting to get a sprite to pick up the location of the player's sprite, and move to that location, then keep on a steady line and straight through it. Since I have no diagrams to show, I'll help you draw a mental picture.

EnemySprite is at location (x1, y1). PlayerSprite is at location (x2, y2). I want EnemySprite to rotate to PlayerSprite's angle, then move quickly to, then through it.

The code I am using is:

function enemymove(CurrentSprite, AttackX, AttackY)
If AttackX = 0 and AttackY = 0
AttackX = Sprite X(EnemySprite) - Sprite X(CurrentSprite)
AttackY = Sprite Y(EnemySprite) - Sprite Y(CurrentSprite)
Rotate Sprite EnemySprite, ATAN(AttackY/AttackX)
endif
Move Sprite EnemySprite, 20
endfunction

In theory, this should work without an issue. However, it seems every time the function is called, AttackX and AttackY are reset to 0, causing the EnemySprite to dart about erratically.

Additionally (as silly as this sounds,) I get an error when the PlayerSprite and EnemySprite are on the same axis. I know why this is, however, because AttackX or Attack Y = 0.

Can anybody help me? I know I'm not offering alot of information, but any help would be greatly appreciated. Also, the EnemySprite does not have to rotate, I just did that to make the movement easier on myself. Thanks in advance,

Jeremy R. Mahon
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd Oct 2008 20:20
Use ATANFULL, not ATAN
ATAN will only work for a single quadrant, ATANFULL will work for any angle.

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 2nd Oct 2008 20:26
Yeah, I did try ATANFULL, but to no avail. The movement issue is still the same. That's the problem, though. The problem is in the function. The EnemySprite still moves erratically.
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 2nd Oct 2008 21:13
Personally I wouldn't use move sprite - I'd just use the sprite command to keep re-positioning it.

The optomist's right, The pessimist's right.
Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 2nd Oct 2008 21:41
If that were the case, using the sprite command, then I'd need to create the algorithm for a straight line to be "drawn" from the EnemySprite to the PlayerSprite, then to the edge of the screen so the sprite can move off screen.

Unfortunately, I can't remember how to do that. That's why I thought the angle and move sprite method would be a little easier to deal with.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Oct 2008 22:31 Edited at: 2nd Oct 2008 22:34
Quote: "With SPRITE, you would have to do all the math yourself"


It's not that complicated if he knows the angle with atanfull.



Should probably make those floats. And only recalculate the angle when you want to change the direction the enemy is headed.


Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 3rd Oct 2008 09:55 Edited at: 3rd Oct 2008 09:56
I appreciate it, but this actually causes the enemy sprite to shoot off erratically in seemingly random directions. Lol. So, here is something I have been considering:

Basic trig/geometry, can't remember which, states that the slope of a ray (a straight line with a single endpoint) is determined with the equation slope = Ay - By / Ax - Bx. The equation for "y" at any point on the line is: y = slope*x + y-intercept. The intercept is at which point the line meets the y-axiz. This can be determined using the equation: intercept = y - slope*x. Now, in a two dimensional drawing test, this equation worked nearly flawlessly. However, when I plugged it into my program, my EnemySprite was stuck moving along the X axis, with the Y axis remaining unchanged. Here's my test code.

HIDE MOUSE
SYNC ON

OriginX = 480
OriginY = 80
TargetX = 320
TargetY = 240
NextX = OriginX

REPEAT
DOT OriginX, OriginY
DOT TargetX, TargetY

EndX = (OriginX - TargetX)
EndY = (OriginY - TargetY)
slope = EndY/EndX
intercept = OriginY - (slope*OriginX)

NextY = (slope*NextX)+intercept

IF EndX > 0 THEN NextX = NextX-1
If EndX < 0 THEN NextX = NextX+1
DOT NextX,NextY

SYNC

UNTIL MOUSECLICK()=1 OR ESCAPEKEY()=1
SHOW MOUSE
END
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Oct 2008 10:36
Quote: "I appreciate it, but this actually causes the enemy sprite to shoot off erratically in seemingly random directions"


Don't see how, I tested it and it worked fine for me.





Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 8th Oct 2008 09:14
I appreciate your help, Phaelax, and I was able to get it somewhat working, but I need your help to tighten it up. Unfortunately, When the degree is negative, the sprite is sometimes more than slightly off. Would you mind checking out my source?

-Jeremy

Attachments

Login to view attachments
Pillarofire
20
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, &lt;looks around.&gt;
Posted: 20th Dec 2008 04:51
Change these lines:




To this:



And see if that works.

Login to post a reply

Server time is: 2024-05-23 01:12:05
Your offset time is: 2024-05-23 01:12:05