Getting accurate coordinates

Started by DrewCCU, Sat 12/06/2010 05:52:31

Previous topic - Next topic

DrewCCU

I have cPlayerA "shooting" cPlayerB.  cPlayerB (the enemy) is moving randomly around the screen. 

The way I'm doing this is i have the projectile as an invisible character that's following the player.
(Note: I'm not using the FollowCHaracter command for this. Rather, I have it simply cProjectile.x = cPlayerA.x (same goes for the y)

Anyway, I have it set up so that when the "Fire" button is pressed the cProjectile becomes visible and cProjectile.Walk (cPlayerB.x, cPlayerB.y);

So to finish up this little action I need to have it when CProjectile reaches the enemy then it resets (becomes fully transparent again and goes back to following cPlayerA) right?

Well all that is good and well but the problem lies here:

cPlayerB is moving so fast that the part of the script that says "if cProjectile is colliding with cPlayerB then do all the resetting stuff" isn't being triggered because by the time cProjectile gets to the cooridnates he is no longer close enough to cPlayerB to trigger a collision.  cProjectile is where cPlayerB was a sec ago.

Does all this make sense? If not, let me know what i need to clear up.

So, can someone tell me a better way of doing this or how to fix this problem?
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Wyz

Are you making a homing projectile or something that moves in a straight line and would miss in this case?
Life is like an adventure without the pixel hunts.

DrewCCU

Quote from: Wyz on Sat 12/06/2010 13:04:46
Are you making a homing projectile or something that moves in a straight line and would miss in this case?

right.  and it's not supposed to miss its supposed to hit.  the reason is because it's not actually acting like homing device.  its going to the last known coordinates of cPlayerB but by the time it gets there cPlayerB has moved on.  A way to make it act like a homing device would be to have it follow cPlayerB but doing it that way has given me some bugs so that the second part of the code that says if projectile is touching playerb isn't activating because the projectile still isn't ALWAYS going to touch playerb.

I know people have pulled things like this off successfully and i want to know how it can be done.
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Crimson Wizard

I have this suggestion: do not use Walk function at all. Increment X or Y coords of cProjectile respectively every tick, using determined speed. You will need to use vector algebra:

Code: ags

float dx = IntToFloat(cTarget.x - cProjectile.x);
float dy = IntToFloat(cTarget.y - cProjectile.y);

float distance = Maths.Sqrt(dx*dx + dy*dy);

float IncX = dx * ProjectileSpeed / distance;
float IncY = dy * ProjectileSpeed / distance;

cProjectile.x = cProjectile.x + FloatToInt(IncX);
cProjectile.y = cProjectile.y + FloatToInt(IncY);


DrewCCU

#4
Quote from: Crimson Wizard on Sat 12/06/2010 19:02:02
I have this suggestion: do not use Walk function at all. Increment X or Y coords of cProjectile respectively every tick, using determined speed. You will need to use vector algebra:

Code: ags

float dx = IntToFloat(cTarget.x - cProjectile.x);
float dy = IntToFloat(cTarget.y - cProjectile.y);

float distance = Maths.Sqrt(dx*dx + dy*dy);

float IncX = dx * ProjectileSpeed / distance;
float IncY = dy * ProjectileSpeed / distance;

cProjectile.x = cProjectile.x + FloatToInt(IncX);
cProjectile.y = cProjectile.y + FloatToInt(IncY);



So i would replace "ProjectileSpeed" with a number that represents how fast i want it to be traveling?

Edit:
I tried this and I got an error with the line that holds: "float IncX = dx * ProjectileSpeed / distance;"
It says:
Type mismatch: cannot convert "float" to "int"
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Crimson Wizard

Quote from: DrewCCU on Sat 12/06/2010 19:25:56
Edit:
I tried this and I got an error with the line that holds: "float IncX = dx * ProjectileSpeed / distance;"
It says:
Type mismatch: cannot convert "float" to "int"

If ProjectileSpeed is of type int, it should be converted to float here, obviously:
float IncX = dx * IntToFloat(ProjectileSpeed) / distance;

DrewCCU

Ok cool. Thanks Crimson Wizard.  That seems to be working better. But there is still one issue i'm not happy with.  The projectile moves to the Target but it touches the base of the target instead of the center and then resets. (because of the PPCollision code)

It would look more realistic if the projectile could hit the center of the target before resetting.

i.e. if the projectile is being shot at a sprite of a man, whats happening is the projectile is moving from the bottom of the screen to the mans feet - touching his feet and then resetting.  I want the projectile to move from the bottom of the screen hit the man in the chest and then reset.  If that makes sense?
"So much of what we do is ephemeral and quickly forgotten, even by ourselves, so it's gratifying to have something you have done linger in people's memories."
-John Williams

Crimson Wizard

Well, if you can get target sprite's height value, you can use (Target.y + Height/2) expression instead of just Target.y.

SMF spam blocked by CleanTalk