You are not logged in -
nap
CSDb User Forums
Forums
>
C64 Coding
>
Stopping a sprite when hitting background
2004-07-27
08:49
Richard
Registered: Dec 2001
Posts: 621
Stopping a sprite when hitting background
How do I stop a sprite from hitting the background, and let the sprite move around the screen, avoiding contact. This is mainly because my games usually make the player die when it touches the background, which I think gets too repetitive. Reason: I don't know how to stop the player from walking when touching a wall - without the player freezing when you try to move it away from the wall.
The collision routine, which I usually use is (As on Lemon64):
BGRCOL LDA $D01F
LSR A
BCC NOBGCOL
DEC STATUS
LDA STATUS
CMP #$00
BNE NOBGCOL
JMP GAMEOVER
NOBGCOL RTS
Please help.
2004-07-27
10:40
Krill
Registered: Apr 2002
Posts: 2980
using the hw collision detection on the c64 not a very good idea.
first, you can detect collisions only once every frame (afair), thus you'll get problems when having multiplexers.
then, you can only detect which sprites collided, if you have for example 3 bits set in the collision detection register, you dont know which sprite collided with which of the two other ones, or both.
generally i would always recommend to do complete software collision detection, even the most naive approaches with rectangular bounding boxes give sufficient results in most cases.
2004-07-27
10:46
Oswald
Registered: Apr 2002
Posts: 5094
richard: when you detect that the player collided with the wall automagically move it away from the wall within the same routine. So you will cancell the collision state, and you wont get into the infinite collide loop.
Maybe this simpler aproach is enough for what you want to do.
2004-07-27
11:01
Stingray
Account closed
Registered: Feb 2003
Posts: 117
Is this a trick question? not sure If I understood completely. You could give the sprite boundries. You could try a piece of code like this, it's off the top of my head so it could be wrong.
LDA $D000
CMP #$30
BCS SOMEWHERE
LDA #$30
STA $D000
This would stop Sprite 0 from moving any more left then #$30 but you could use a bit of code like this for any sprite in any direction if you know what I mean.
PS You probably know this but I think you could take the fellowing two lines out of the code your useing it should still work the same.
LDA STATUS
CMP #$00
2004-07-28
11:56
cadaver
Registered: Feb 2002
Posts: 1160
That's a horrible question, because the answer totally depends on your game's architecture, and it's not like we should be deciding that for you. Anyway, you could work out a routine to read a char from the screen based on sprite's X,Y coordinates.
When the sprite moves, save previous coordinates. Check using the new coordinates, if there's a collision-causing char under the sprite (for example, anything other than 32). If there is, revert back to saved coordinates.
2004-07-29
14:08
Kenho
Account closed
Registered: Jan 2003
Posts: 26
Sprite coordinates to char coordinates
This works when the sprite x coordinate only is within 0 to 255, additional stuff needs to be done if thats not the case.
(someone add code to that please)
Work out y coord:
the y coord has a formula of:
chary = (spritey-50)/8
to get to the char you use
x + y*40
lda spritey
cec
sbc #$32
why sub with $32 ? that is because the upper border is $32 pixels thick, you need to compensate.
lsr a ; a/2
lsr a ; a/2
lsr a ; a/2
tay
If you divide by two 3 times you have divided by 8. 8 is char width.
you know, on a screen you can have 40 chars. 40 * 8 is 320, ring a bell?
Now here is my solution for easy use of the y coord as i dont want to multiply by 40.
I have made two tables of bytes to be put in 2 consecutive zp adresses. These are screencodes.
As i am in bank 1 my screen starts at $4400. normally if no bankswitching, screen would start at $0400.
The y register now contains a value between 0 and 25 which i can use as an index in my table and put into the zp adresses like this
lda scrlo,y ; 0,28,50 etc
sta $70
lda scrhi,y ;44,44,45 etc
sta $71
Now to get to the x pos of the char you need to do almost the same thing again.
lda spritex
sec
sbc #$18 ; compensate for left border...
lsr a
lsr a
lsr a ;divide by 8
tay
now here comes the trick to get to the char you want:
lda ($70),y ; y in this is the x in x+y*40
cmp #32 ;was there a space char there?
bne hitt ;no space char, then hit
Explanation:
the lda($70),y takes the two consecutive bytes at $70 and $71 and makes them into an adress. for example.
lda #$28
sta $70
lda #$04
sta $71
ldy #3
lda ($70),y would give accum the char at the second row + 3
computer does (($71)*256 + ($70))+y
This are the tables:
scrhi.byte $44,$44,$44,$44,$44,$44,$44,$45,$45,$45,$45,$45
,$45,$46,$46,$46,$46,$46,$46,$46,$47,$47,$47,$47,$47,$47
scrlo .byte$0 ,$28,$50,$78,$a0,$c8,$f0,$18,$40,$68,$90,$b8,
$e0,$08,$30,$58,$80,$a8,$d0,$f8,$20,$48,$70,$98,$c0,$e0
Feel free to suggest other solutions or add something to get the whole spritex (including 9th bit)
Feel free to ask me if someting is unclear (should it be??)
Only my two cents..
2004-07-29
14:21
Kenho
Account closed
Registered: Jan 2003
Posts: 26
Just to add a question to rich about my code. where on the sprite does it hit the char?
2004-07-29
15:35
Richard
Registered: Dec 2001
Posts: 621
Don't know. The side of corner of the sprite perhaps?
2004-07-29
15:43
Oswald
Registered: Apr 2002
Posts: 5094
kenho:
to get the 9th bit of spriteX working simply do this:
lda $d000 ;x coord
lsr $d010 ;9th bit into -> CARRY
ror ;CARRY->into accu->
lsr
lsr
so ror is the same as LSR, just it pushes the CARRY into the MSB of the accumulator instead of 0.
this works only for sprite 0, but you can have a table to convert the actual sprites MSB from $d010, to an 1 or 0 in the Accumulator. Or without a table do more LSRs until the bit u need will get into carry.
the answer to your question:
as the sprites coordinates refer to their topleft corner, this aproach is very rude. Better to check the whole area under the sprite (3x3 char).
this would change your code to:
ldx #2
l1 lda ($70),y
cmp #32
beq hit
iny
cpy #3
bne l1
lda $70
clc
adc #$28
sta $70
bcc *+4
inc $71
dex
bpl l1
hit: (whatever)
2004-07-29
15:57
Richard
Registered: Dec 2001
Posts: 621
Quote:
Just to add a question to rich about my code. where on the sprite does it hit the char?
The top of the sprite hits the bottom of the charset and this is a sort of collision.
2004-07-29
16:51
Kenho
Account closed
Registered: Jan 2003
Posts: 26
Thanks for your additions Oswald!
Now its a really great collission routine :-)
edit: cannot get it to work correctly.... i´m lame :-)
... 8 posts hidden. Click
here
to view all posts....
Previous - 1 |
2
-
Next
Refresh
Subscribe to this thread:
You need to be logged in to post in the forum.
Search the forum:
Search
All forums
C64 Coding
C64 Composing
C64 Pixeling
C64 Productions
CSDb Bug Reports
CSDb Development
CSDb Discussions
CSDb Entries
CSDb Feedback
CSDb Info
CSDb moderators
CSDb Questions
Messages to moderators
Requests
for
in
Writer & text
Text
Writer
All times are CET.
Search CSDb
All
Releases
Groups
Sceners
Events
BBS
SIDs
-------
Forum
Comments
Advanced
Users Online
CreaMD/React
Guests online: 124
Top Demos
1
Next Level
(9.7)
2
13:37
(9.7)
3
Mojo
(9.7)
4
Coma Light 13
(9.6)
5
Edge of Disgrace
(9.6)
6
What Is The Matrix 2
(9.6)
7
The Demo Coder
(9.6)
8
Uncensored
(9.6)
9
Comaland 100%
(9.6)
10
Wonderland XIV
(9.6)
Top onefile Demos
1
No Listen
(9.6)
2
Layers
(9.6)
3
Cubic Dream
(9.6)
4
Party Elk 2
(9.6)
5
Copper Booze
(9.6)
6
X-Mas Demo 2024
(9.5)
7
Dawnfall V1.1
(9.5)
8
Rainbow Connection
(9.5)
9
Onscreen 5k
(9.5)
10
Morph
(9.5)
Top Groups
1
Performers
(9.3)
2
Booze Design
(9.3)
3
Oxyron
(9.3)
4
Censor Design
(9.3)
5
Triad
(9.3)
Top Graphicians
1
Mirage
(9.8)
2
Archmage
(9.7)
3
Pal
(9.6)
4
Carrion
(9.6)
5
Sulevi
(9.6)
Home
-
Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.063 sec.