textwidth and textheight?

BlitzMax Forums/BlitzMax Beginners Area/textwidth and textheight?

B(Posted 2011) [#1]
So exactly how does textwidth work?
is each character the same width and height?

I am using textwidth and textheight to make a collision box around a character ie x or s or D.

how would i find whether some objects x and y coordinates are within a box using the textheight and textwidth command, as well of course as the texts x.y coords itself.

heres a snippit of my code:


g.x and g.y are x,y coords of player character
b.x and b.y are x.y coords of enemy character
g.c$ and b.c$ are the chracters string which is a single letter
g.sx/g.sy and b.sx/b.sy are the setscale variables for each individual character

If b.d = 0
		If g.x + (TextWidth(g.c$)) > b.x And g.x < b.x + (TextWidth(b.c$)) And g.y + (TextHeight(g.c$)) > b.y And g.y < b.y + (TextHeight(b.c$))
			FlushKeys()
			battlepause = 1
			WaitTimer(CreateTimer(2))
			battle(g,b)
		EndIf
		SetScale 3,2
EndIf


also does using setscale for different size texts change the number given my textwidth?
I made an example code, but the number did not change, however i am getting different results in my actual code.

so would i do it like this?

If g.x + (TextWidth(g.c$)*g.sx) > b.x And g.x < b.x + (TextWidth(b.c$)*b.sx)


hope i explained my problem well enough.
thanks for the help! :)


xlsior(Posted 2011) [#2]
It returns the actual width and height of the text in question - unless you are using a monospaced font each character is NOT the same width.

I don't know if it has been changed or not, but when blitsmwx first came out the Netscape was ignored completely: so to get the ACTUAL sizes in pixels you'd need to multiply it by your current scale yourself.


B(Posted 2011) [#3]
i see. thanks for the reply.
i think ill just have to draw a box behind the character to get the width and height.

Also, is there any better way to check for collision? im just using strings for the character, not an image. i guess i could make images for all the different characters like a D for dragon and a g for goblin etc... and then use ImagesCollide. but id rather not do that if i dont have to, i wanna focus more on the actual game play.

ill upload my code if you want to see it to give me feedback.


ima747(Posted 2011) [#4]
Simple bounding box collision test, assuming your anchor is top left corner.

If (obj1.x + obj1.width >= obj2.x And obj1.x <= obj2.x + obj2.width And obj1.y + obj1.height >= obj2.y And obj1.y <= obj2.y + obj2.height)
	Print "Obj1 overlaps obj1"
EndIf



B(Posted 2011) [#5]
@ima747

I understand that, i just couldnt figure out if the textwidth and textheight gave me the actual width and height cuz I am scaling the text larger than normal.
have any ideas?


ima747(Posted 2011) [#6]
I don't believe the scale is factored into the text size commands, it does factor the size of the font you loaded but I don't believe it uses your draw scale as well. To test, get text width for something, then change scale, get text width again. if they're the same, you have to factor your own scale...

To factor your own scale, just get the text width/height and multiply it by the scale you've set.

Any other graphics you have are also not going to factor in the draw scale, so you might just want to include the draw scale factor into the collision test (... obj1.x + obj1.height * x_scale >= ...). Depends how you're laid out, if you change scales a lot (perhaps each object has it's own scale that needs to be factored?) etc.


B(Posted 2011) [#7]
I did do a test to try that and it showed the width to stay the same at all scales,
but its possible that i was only getting the original width/height and not updating it when i changed the scale.
ill try that real quick.
also, in my code above i do multiply the width/height by the scale,
and then i was getting funny results that were either much too big or too small.
I am missing something.


ima747(Posted 2011) [#8]
Sounds like you're using the wrong scale somewhere, or have a math operation reversed...

You could try making a visible collision box with a drawing primitive to get a better sense of what your numbers doing. Also make sure you're using the right anchors, the scale is being calculated appropriately for the drawing scale used if you're changing your scales, etc. Could be as simple as a typo.


TaskMaster(Posted 2011) [#9]
Just so you know...

Textheight returns the height of the font, not the height of the string you pass it.

In other words, if you send "Yy" and you send "a", textheight is going to return the same height, even though the "Yy" one is actually taller. So, you can just pass an empty string to textheight and get your value once and save it. No reason to call it multiple times in your program, unless the font changes.


B(Posted 2011) [#10]
thanks for the help guys.

@ima747
I did have a typo! I was using the attack of the character as the scale so the more you leveled up the larger your collision box got. haha. i just played my game for a while and found that out. still not working as well as i would like tho.

@taskmaster
i didnt know thats how it worked.
I think im going to just use numbers rather then the textheight/width. the hitbox around all the characters will be the same.

thanks again guys.