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.

2D All the way! / Targeting a location and moving to and through it.

Author
Message
Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 2nd Oct 2008 09:55
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
flashing snall
18
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 2nd Oct 2008 22:52 Edited at: 2nd Oct 2008 22:55
Hmm. Im not really sure I fully understand your code, But I will try to help any how.

Im assuming (ass outta you and me) that you want the enemy to track the player, but only update his direction every so often so the player can get away.

This code will have the green box be controlled by the mouse. The red box will go after it every 1000 loops.


EDIT:
And this code will have the red box update his direction when he gets to the edge of the screen.
sync on
sync rate 100

box 0,0,100,100
get image 1,0,0,20,20,1
cls


player = 1;
playerX = 0; playerY = 0;
playerImage = 1;


enemy = 2;
enemyX = screen width()/2;enemyY = screen height()/2;
enemyImage = 1;

enemyUpdate = 1;
enemySpeed = 5;
enemyAngle = 0;

sprite enemy,enemyX,enemyY,enemyImage
set sprite diffuse enemy,100,0,0
offset sprite enemy,sprite width(enemy)/2,sprite height(enemy)/2


do
playerX = mouseX()
playerY = mouseY()
sprite player, playerX,playerY,playerImage
set sprite diffuse player, 0,100,0
offset sprite player,sprite width(player)/2,sprite height(player)/2


if enemyUpdate = 1
enemyUpdate = 0
RotateToPoint(enemy,sprite X(enemy),sprite Y(enemy),sprite X(player),sprite Y(player))
endif
move sprite enemy,enemySpeed

if (sprite X(enemy) > screen width()) OR (sprite X(enemy) < 0) OR (sprite Y(enemy) < 0) OR (sprite Y(enemy) > screen height())
enemyUpdate = 1
endif

sync
loop


Function RotateToPoint(SpriteNum,Xo,Yo,Xd,Yd)

X = Xo - Xd
Y = Yo - Yd;

spriteAngle = -atanfull(X,Y)
rotate sprite spriteNum,spriteAngle

Endfunction


This is my WIP, not even ready for a WIP thread yet though.http://smallgroupproductions.com/
Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 3rd Oct 2008 09:49
I appreciate it, but this actually causes the enemy sprite to shoot off in a 45 degree angle in the opposite direction. 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:37
Did you really need to post this on multiple boards?


http://forum.thegamecreators.com/?m=forum_view&t=137826&b=1


Visioneer
20
Years of Service
User Offline
Joined: 19th Aug 2003
Location:
Posted: 3rd Oct 2008 17:15
Yeah, sorry about that. I first posted to this one, then saw another post that suggested programming posts just be made to the DarkBasic Pro Newcomers board, so I attempted to post my issue there instead.

I wasn't expecting anyone to reply to the post here, but when I got a response, I couldn't just leave this one hanging.
flashing snall
18
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 3rd Oct 2008 23:33
My code makes them go the wrong way?!
I tested it on mine and they were fine... That is a strange one.


This is my WIP, not even ready for a WIP thread yet though.http://smallgroupproductions.com/

Login to post a reply

Server time is: 2024-05-02 17:00:05
Your offset time is: 2024-05-02 17:00:05