PDA

View Full Version : Problem with sprite engine. Not working right



seiferalmasy
11-02-2007, 06:29 PM
if (R5Direction='RIGHT')then
begin
X:=x+Speed1*dx;
end;

if (R5Direction='LEFT')then
begin
X:=X-Speed1*dx;
end;


if (X>=GameAreaX2-Image.width)then //hits right wall
begin
R5Direction:='LEFT'; //sets all 8 sprites to go left
end;

if &#40;X<=GameAreaX1&#41;then // hits left wall
begin
R5Direction&#58;='RIGHT';
end;

oK what we have here is a space invader type game. The 8 sprites bounce from wall to wall like the classic game does. If it hits the left wall, the global variable is set to RIGHT and vice versa for the other wall.

Problem. When first sprite hits left wall, instead of just turning right at same time as the other sprites, it jerks and then goes out of sync with the other sprites. After a while the gap between sprite 1 and sprite 2 becomes huge. The faster the sprite moves the larger the gap becomes each time the wall is hit.

The code dictates that all sprites should move left and right at the same time, but srpite 1 is being treated differently to sprite 2-8

Sprite 8 hits the Right wall but everything stays in sync. It only happens with sprite 1 on the left wall if the code is exactly as above.

cairnswm
12-02-2007, 10:29 AM
What seems to be happening is that the first sprite moves right and then encounters the wall. After sprite 1 has touched the wall all the other sprites move left (but sprite 1 has already moved right).

So you need to change the logic so that the global var can only be changed at the end of a move step and not in the middle of it. The easist way to do this is with a temp var that gets changed as above and then at the end of the frame set the global var to the value of the temp var.

seiferalmasy
12-02-2007, 05:12 PM
no that is the thing, sprite 1 doesn't move right (It moves left and encounters left wall), it actually jerks and ends up behind the other sprites, meaning it has moved left or simply stalled. Also, since I have put the movement portion of the code above the code that changes the global variable, shouldnt they all move together on the next tick?

Huehnerschaender
12-02-2007, 06:25 PM
I guess you process your sprites in a for loop...


for spritecount &#58;= 1 to 8 do
begin
sprite.process;
end;

in SpriteProcess I guess you do the code you posted...

Then I ask you:

What happens if sprite 1 hits the left wall?

1. it moves left
2. it sets direction to right

Now, sprite 1 is the first one in your processing, it hits the wall and sets direction for all sprites to right... but the sprite itself already wandered to the left... the following 7 in this loop will wander right!

So thats your problem I guess :)

This happens only with the first sprite (and not with the 8th), because the 8th does not affect the other sprites in this loop turn. The first does!

You have to check the wallhit BEFORE you set the new position... and then decide what to do. This way also the first sprite will be affected by the direction change....

Got the point?

seiferalmasy
12-02-2007, 06:26 PM
sprite 1 can't hit the right wall, unless all 7 other sprites are dead. As in space invaders, they are ina line. I will supply a demo and see what you make of it;)

Huehnerschaender
12-02-2007, 06:27 PM
Updated my post and corrected the directions... the idea is the same :)

seiferalmasy
12-02-2007, 06:29 PM
hmm but shouldn't sprite 1 hit left wall. set direction to right. then all other sprites move at same time as sprite 1 on the next loop, the direction is right and they all move at same time? The distance should not change

I will post the demo, if you can sort it somehow then, I will conceed this is my error 8) :oops:

seiferalmasy
12-02-2007, 06:43 PM
http://www.uploading.com/files/76IM16A3/movement_problem.rar.html

Ok download away:)

Huehnerschaender
12-02-2007, 07:25 PM
Sorry, I had feed my son.

Try this code. Cannot test it, because I don't have DelhiX installed. But it should work this way. It's exactly the problem I mentioned... (and William too :) )



procedure TRow5Sprite.DoMove&#40;MoveCount&#58; Integer&#41;;
begin
inherited DoMove&#40;MoveCount&#41;;
collision;

if &#40;R5Direction='RIGHT'&#41;then
begin
if &#40;X>GameAreaX2-Image.width&#41;then
begin
r5direction&#58;='LEFT';
X&#58;=X-Speed1*dx;
exit;
end
else
X&#58;=x+Speed1*dx;
end;

if &#40;R5Direction='LEFT'&#41;then
begin
if &#40;X<GameAreaX1&#41;then
begin
r5direction&#58;='RIGHT';
X&#58;=X+Speed1*dx;
end
else
X&#58;=X-Speed1*dx;
end;
end;


Basically, FIRST check the collision, then move...

seiferalmasy
12-02-2007, 07:40 PM
nice try but now sprite 8 is moving into sprite 7 instead lol :D

Huehnerschaender
12-02-2007, 07:50 PM
Yes, I see :)

Change it to this... then it should work.



if &#40;R5Direction='RIGHT'&#41;then
begin
X&#58;=x+Speed1*dx;
if &#40;X>GameAreaX2-Image.width&#41;then
r5direction&#58;='LEFT';
end;

seiferalmasy
12-02-2007, 07:53 PM
still , sprite 8 moves towards sprite 7;) :shock:

Huehnerschaender
12-02-2007, 07:55 PM
if &#40;R5Direction='RIGHT'&#41;then
begin
X&#58;=x+Speed1*dx;
if &#40;X>GameAreaX2-Image.width&#41;then
begin
r5direction&#58;='LEFT';
exit; // <---------------------- I forgot this &#58;&#41;
end;
end;

seiferalmasy
12-02-2007, 07:58 PM
seiferalmasy 0
Huehnerschaender 1

weldone dude;) Never wouldda got that, shall study it.

Thanks:)

what does exit do exactly?>

Huehnerschaender
12-02-2007, 07:59 PM
I'm glad that I was able to help :)

You're welcome...

Huehnerschaender
12-02-2007, 08:00 PM
what does exit do exactly?

Exit jumps out of the current procedure, while break jumps out of a current loop (e.g. for-loop).

seiferalmasy
12-02-2007, 08:02 PM
cheers again dude:)