APOLOGIES TO LOG IN FOR THE (NOW FIXED) BAD EXPLANATION
First off, is this pseudo code?
I'm never seen com, only cmp.
Second off, what you've described in the if statements is more or less how you do it, except your check for X should be > not <.
This is what happens when it's ballx < paddlex.
Code: Select all
| = Paddle
* = ball
|
* |
|
The ball's x position is less than the paddle's x position
The ball's y position is greater than the paddle's top y position
The ball's y position is less than the paddle's bottom y position
So the ball bounces left even though the ball is nowhere near the paddle.
Something similar would happen with the reversed condition with the ball on the right, so it might be worth doing two x checks as well if you don't want the player to hit the ball after it has already passed the paddle on the right.
That said, your actual 6502 code is not doing the checks you laid out at all. See comments.
Code: Select all
checkpaddlecollision
lda ballx
cmp paddlex
beq paddlerange;If ball's x position is equal to paddle's x position branch to paddlerange
; You're not skipping anything here. You branch to paddlerange
;But if you didn't branch the except same
;thing would happen because no code is in between the branch
;and the label.
;Did you mean bcc instead of beq, since you
;want to do the y check only if the ball's x position is to the right of the paddle?
;Did you mean to branch to paddlerangedone, which would skip
;the y collision check so a bounce can't occur in the case?
paddlerange:
lda bally
cmp paddleytop
bcc paddlerangedone;if ball y < paddleytop branch to paddlerangedone
;You also need to do the second y check you described in the if statement, or the ball will bounce whenever the ball is below the paddle's top.
;Even if it's also below the paddle's bottom.
lda #$00;Do this stuff;
sta ballright;
lda #$01
sta ballleft
paddlerangedone:
So basically, your current code doesn't check X at all. It bounces the ball whenever the ball's position is greater than or equal to the paddle's top. Meaning this is a hit:
So is this:
Here is how the flags are set after a compare:
Code: Select all
+-------------------------+---------------------+
| | N Z C |
+-------------------------+---------------------+
| A, X, or Y < Memory | 1 0 0 |
| A, X, or Y = Memory | 0 1 1 |
| A, X, or Y > Memory | 0 0 1 |
+-----------------------------------------------+
You have to do the checks you described (with my fix for X), and branch passed the ball bounce code any time one of them isn't true. That way if all of them are true, the ball will bounce.
Edit: For those curious about my error, I said log in's y branch was wrong when it is not. I also suggested changing his x beq to bcs when it should be bcc. I always get confused when mixing if statements with assembly because
Code: Select all
if(whatever == whatever){;If equal run the code below
//code
}
but
Code: Select all
lda whatever
cmp whatever
beq label;If equal skip the code below
;code
label:
That's really no excuse, though. Sorry, log in.
You just need to fix the x branch and add the second y check, not reverse the condition of the existing y check
and add a y check.